Header Ad

HackerEarth Pepper and Contiguous Even Subarray Debugging solution

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


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("%d\n",x)
#define s(x) scanf("%lld",&x)
#define p(x) printf("%lld\n",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);
}
}

Post a Comment

0 Comments