Skip to content
Programmingoneonone
Programmingoneonone

LEARN EVERYTHING ABOUT PROGRAMMING

  • Home
  • CS Subjects
    • IoT ? Internet of Things
    • Digital Communication
    • Human Values
  • Programming Tutorials
    • C Programming
    • Data structures and Algorithms
    • 100+ Java Programs
    • 100+ C Programs
  • HackerRank Solutions
    • HackerRank Algorithms Solutions
    • HackerRank C problems solutions
    • HackerRank C++ problems solutions
    • HackerRank Java problems solutions
    • HackerRank Python problems solutions
Programmingoneonone

LEARN EVERYTHING ABOUT PROGRAMMING

HackerEarth Pepper and Contiguous Even Subarray Debugging solution

YASH PAL, 31 July 2024
In this HackerEarth Pepper and Contiguous Even Subarray Debugging problem solution, This is a DEBUGGING question. You have an array of length N. A subarray is called Interesting if it contains only even numbers. You have to find the maximum length of such an Interesting subarray.
You are given a code that does the given task but has some bugs (errors). You have to make changes in the given function such that the code handles all the cases for errors and generates correct output every time.
HackerEarth Pepper and Contiguous Even Subarray <Debugging> problem solution

Topics we are covering

Toggle
  • HackerEarth Pepper and Contiguous Even Subarray Debugging problem solution.
    • Second solution

HackerEarth Pepper and Contiguous Even Subarray Debugging problem solution.

#include<bits/stdc++.h>
#define ll long long
#define ld long double
#define mp make_pair
#define pb push_back
#define si(x) scanf("%d",&x)
#define pi(x) printf("%dn",x)
#define s(x) scanf("%lld",&x)
#define p(x) printf("%lldn",x)
#define sc(x) scanf("%s",x)
#define pc(x) printf("%s",x)
#define pii pair<int,int>
#define pll pair<ll,ll>
#define F first
#define S second
#define inf 4e18
#define prec(x) fixed<<setprecision(15)<<x
#define all(x) x.begin(),x.end()
#define rall(x) x.rbegin(),x.rend()
#define mem(x,y) memset(x,y,sizeof(x))
#define PQG priority_queue< int,std::vector<int>,std::greater<int> >
#define PQL priority_queue< int,std::vector<int>,std::less<int> >
#define PQPL priority_queue<pii ,vector< pii >, less< pii > >
#define PQPG priority_queue<pii ,vector< pii >, greater< pii > >
#define fast_io ios_base::sync_with_stdio(false);cin.tie(NULL)

using namespace std;

int main() {
#ifndef ONLINE_JUDGE
freopen("in04.txt","r",stdin);
freopen("out04.txt","w",stdout);
#endif
fast_io;
int t;
cin>>t;
assert(t>=1 && t<=10);
while(t--) {
int n;
cin>>n;
assert(n>=1 && n<=100000);
int a[n];
for(int i=0;i<n;i++) {
cin>>a[i];
assert(a[i]>=1 && a[i]<=1000000);
}
int ans=0;
int cnt=0;
for(int i=0;i<n;i++) {
if(a[i]%2==0) cnt++;
else {
ans=max(ans,cnt);
cnt=0;
}
}
ans=max(ans,cnt);
if(!ans) ans=-1;
cout<<ans<<endl;
}
return 0;
}

Second solution

import java.io.*;
import java.util.*;


public class TestClass {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
PrintWriter wr = new PrintWriter(System.out);
int T = Integer.parseInt(br.readLine().trim());
for(int t_i=0; t_i<T; t_i++)
{
int N = Integer.parseInt(br.readLine().trim());
String[] inp = br.readLine().split(" ");
int[] A=new int[N];
for(int i=0;i<N;i++)
{
A[i] = Integer.parseInt(inp[i]);
}
int ans = solve(N, A);
System.out.println(ans);
}

wr.close();
br.close();
}
static int solve(int N, int[] A) {
int ans=0;
int cnt=0;
for(int i=0;i<N;i++) {
if(A[i]%2==0) cnt++;
else {
ans=Math.max(ans,cnt);
cnt=0;
}
}
if(ans==0 && cnt==0)ans=cnt=-1;
return Math.max(ans,cnt);
}
}
coding problems solutions

Post navigation

Previous post
Next post
  • Automating Image Format Conversion with Python: A Complete Guide
  • HackerRank Separate the Numbers solution
  • How AI Is Revolutionizing Personalized Learning in Schools
  • GTA 5 is the Game of the Year for 2024 and 2025
  • Hackerrank Day 5 loops 30 days of code solution
How to download udemy paid courses for free

Pages

  • About US
  • Contact US
  • Privacy Policy

Programing Practice

  • C Programs
  • java Programs

HackerRank Solutions

  • C
  • C++
  • Java
  • Python
  • Algorithm

Other

  • Leetcode Solutions
  • Interview Preparation

Programming Tutorials

  • DSA
  • C

CS Subjects

  • Digital Communication
  • Human Values
  • Internet Of Things
  • YouTube
  • LinkedIn
  • Facebook
  • Pinterest
  • Instagram
©2025 Programmingoneonone | WordPress Theme by SuperbThemes