C++ program to find length of non empty substring whose sum is even

Suppose we have an array A with n elements. We have to find the length of a non-empty subset of its elements such that their sum is even or return -1 when there is no such subset.

So, if the input is like A = [1, 3, 7], then the output will be 2, because sum of [1, 3] is 4.

Steps

To solve this, we will follow these steps −

n := size of A
for initialize i := 0, when i 

Example

Let us see the following implementation to get better understanding −

#include 
using namespace std;

int solve(vector A) {
   long n = A.size(), k = 0;
   for (long i = 0; i  A = { 1, 3, 7 };
   cout 

Input

{ 1, 3, 7 }

Output

2
Updated on: 2022-03-04T06:38:37+05:30

171 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements