1

For the given question I have come up with the following code. But it doesn't seem to solve the problem. Please have a look and suggest changes.

#include <iostream>
#include <limits.h>
using namespace std;

int main()
{
    int n;
    cin>>n;
    
    int arr[n];
    for(int i=0;i<n;i++)
    {
        cin>>arr[i];
    }
    
    int maxim=INT_MIN;
    for(int i=0;i<n;i++)
    {
        int sum=0;
        for(int j=i;j<n;j++)
        {
            sum+=arr[j];
            if (sum%2==0){
                int len=n-i;
                maxim=max(maxim,len);
            }
        }
    }
    cout<<maxim;
    return 0;
}
Diksha Nasa
  • 135
  • 3
  • 13
  • I’m voting to close this question because it asks for general debugging help. – Sreeraj Chundayil Aug 04 '21 at 07:17
  • You can take a look [`kadane's algorithm`](https://en.wikipedia.org/wiki/Maximum_subarray_problem)/[`kadane's algo YT video`](https://www.youtube.com/watch?v=86CQq3pKSUw) which finds the longest subarray with max sum. You can tweak it to find the longest even sum subarray. – Ch3steR Aug 04 '21 at 07:18
  • @Ch3steR, Would you please show me how to highlight the text in the gray color ? Thanks. – Job_September_2020 Aug 04 '21 at 07:37
  • Wrap your code between two `\`` character(found under `esc` key usually) – Ch3steR Aug 04 '21 at 10:44

2 Answers2

1

I think there is a bug with this original line " int len=n-i " in the second for loop because you miscalculate the len here.

Maybe, you should calculate the len with this new formula " int len= j - i + 1 ", which seems to be the accurate len.

#include <iostream>
#include <limits.h>
using namespace std;

int main()
{
    int n;
    cin>>n;
    
    int arr[n];
    for(int i=0;i<n;i++)
    {
        cin>>arr[i];
    }
    
    int maxim=INT_MIN;
    for(int i=0;i<n;i++)
    {
        int sum=0;
        for(int j=i;j<n;j++)
        {
            sum+=arr[j];
            if (sum%2==0){

                int len= j - i + 1;  // Calculate len here

                maxim=max(maxim,len);
            }
        }
    }
    cout<<maxim;
    return 0;
}

Note : Your algorithm above has the time complexity of O(n^2). There are definitely other better algorithms that have the time complexity of O(n) to solve this problem.

Job_September_2020
  • 858
  • 2
  • 10
  • 25
  • 1
    Thank you for the solution, definitely helped me understand what I was missing. Yes there could be another more efficient solution, but since I am a little new to the subarray concept I wanted to try my brute force approach. – Diksha Nasa Aug 04 '21 at 08:02
0

First check if the total sum of the array is even. If the total sum of the array is even then the answer will be N. If the total sum of the array is not even, means it is ODD. So, the idea is to find an odd element from the array such that excluding that element and comparing the length of both parts of the array we can obtain the max length of the subarray with even sum.

int maxLength(int a[], int n){
    int sum = 0, len = 0;
 
    // Check if sum of complete array is even
    for (int i = 0; i < n; i++)
        sum += a[i];
 
    if (sum % 2 == 0) // total sum is already even
        return n;
 
    // Find an index i such the a[i] is odd
    // and compare length of both halfs excluding
    // a[i] to find max length subarray
    for (int i = 0; i < n; i++) {
        if (a[i] % 2 == 1)
            len = max(len, max(n - i - 1, i));
    }
 
    return len;
}
xskxzr
  • 12,442
  • 12
  • 37
  • 77
Devanshi Mishra
  • 487
  • 5
  • 15
  • You only need to find the first odd element from left to right and the one from right to left. – xskxzr Aug 04 '21 at 07:32