My issue
this code failed on the following test case
1
4
8745 8393 3835 4874
but this works perfectly on
1
4
5 3 5 4
which is similar to the first test case.
please help me where is the error in the code.
I have tries to use nested loops to check for every sub-array and find the greatest length of even sub-array.
My code
#include <bits/stdc++.h>
using namespace std;
int main() {
int t;
cin >> t;
while(t--){
int n;
cin >> n;
int arr[n];
for(int i =0 ; i < n ; i++){
cin >> arr[i];
}
int sum =0;
int checksum= -1;
int index;
int addindex;
for(int i =0 ; i < n ; i++){
for(int j =0 ; j < n - i ; j++){
sum += arr[i+j];
if(sum % 2 == 0 && sum >= checksum){
checksum = sum;
index = i+j;
addindex = i;
}
}
sum = 0;
}
if(checksum == -1){
cout << 0 << "\n";
}
else{
cout << index + 1 - addindex << "\n";
}
}
}
Problem Link: Even Sum Subarray Practice Coding Problem