Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
Selected Reading
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 iExample
Let us see the following implementation to get better understanding −
#includeusing 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
Advertisements
