Input: ‘N’ = 3, ‘ARR’ = {2, 5, 8}
Output: 3
In this case, there are ‘3’ possible even sequences i.e. [2], [8], and [2, 8].
The first line will contain the integer ‘T’, the number of test cases.
Each test case consists of two lines.
The first line of input contains one integer, ‘N’, the length of the array ‘ARR’.
Followed by a line containing space-separated ‘N’ positive integers, denoting the elements of the array.
For each test case, print the number of non-empty even sequences that can be formed by using the elements of the given array.
You don't need to print anything. It has already been taken care of. Just implement the given function.
1 <= ‘T’ <= 10
1 <= ‘N’ <= 10^5
1 <= ‘ARR[i]’ <= 10^9
It is guaranteed that sum of ‘N’ over all test cases is <= 10^5
Time Limit: 1 sec
The idea for this approach is to iterate through each possible sequence with an even number and keep track of their count and in the end, return it.
To do this we can use a recursive approach, to find all the even sequences possible for the given array ‘ARR’ and increment the count of answers by one if we find a sequence that is not empty, as in our recursion we will never pick an odd element if at last, the sequence is not empty it will always have even elements.
Algorithm:
// This function will generate all possible sequences possible for the given array.
Void getSubsets(i, SUBSET, ANS, N, ARR)
// The function will return the count of non-empty even sequences of the given array ‘ARR’.
Int evenSubsets(N, ARR)
The idea for this approach is to first find the count of even elements of the array ‘ARR’ and using that we can calculate the even sequences using the formula of ‘2count of even - 1’.
Now the above formula can be derived in the following way:
Let’s suppose there are ‘X’ even elements, then for each even element, there is the possibility that either it is included in the sequence or not, thus it leads to the ‘2’ possibility for each of the elements.
Hence the total possibilities become ‘2X’. But that will also include the possibility where none of the elements is picked thus it should be removed from the total count. Thus the formula for all possible non-empty even sequences is ‘2X - 1’.
Algorithm:
// The function will return the count of non-empty even sequences of the given array ‘ARR’.
Int evenSubsets(N, ARR)