Count subarrays with same even and odd elements in C++


We are given an array of positive integers. The goal is to find the subarrays of numbers in an array such that each subarray has the same number of even and odd elements in it. If the array is { 1,2,3,4 }. Then subarrays will be {1,2}, {2,3}, {3,4}, {1,2,3,4}. Count of such subarrays is 4.

Let us understand with examples

Input − arr[] = {1,3,5,7,8,3,2 };

Output − Count of subarrays with same even and odd elements are − 4

Explanation − Subarrays will be − { 7,8 }, {8,3} {3,2}, {7,8,3,2}

Input − arr[] = {2,4,6 };

Output − Count of subarrays with same even and odd elements are − 0

Explanation − All elements are even.

The approach used in the below program is as follows

In this approach, we will use a variable temp as the difference between array elements (initially 0) and increment it by 1 if arr[i] is odd and decrement it by 1 if arr[i] is even. When the value of temp repeats itself then there must exist a subarray with the same number of even-odd numbers between these indexes. The temp can be positive as well as negative. Taking two hash arrays arr_1[size+1] for the frequency of positive differences and arr_2[size+1] for frequency of negative differences.

For each difference temp<0 add frequency from arr_1[-temp] to count. [ -(-temp) ] gives a positive index.

For each difference temp>0 add frequency from arr_2[temp] to count. As all occurrences of the same difference value will be part of subarrays. Update these frequencies by 1.

Arr[] = { 1,3,5,7,8,3,2 }

Temp values starting from 0 −

1,2,3,4,3,4,3
arr_1[] { 1,1,1,3,2,0,0,0 } //all differences are positive
arr_2[] { 0,0,0,0,0,0,0,0 } //no difference is negative
Adding arr_1[temp] to count in each iteration gives count=4.

Subarrays are − { 7,8 }, {8,3} {3,2}, {7,8,3,2}

  • Take the initial array as arr[].

  • Function Sub_even_odd(int arr[], int size) takes the array and its length and returns the count of subarrays with the same even and odd elements.

  • Take the initial count as 0 and variable temp as a variable which increments when an odd value is encountered and decrements when an even value is encountered.

  • Take two arrays arr_1[] and arr_2[] to store frequencies of temp. arr_1[] for positive values of temp which can be up to size+1 in case the whole array is even.

    arr_2[] for negative values of temp which can be up to size+1 in case the whole array is even.

  • Traverse arr[] using for loop.

  • If arr[i] & 1==1 that means arr[i] is odd. Increment temp. Else decrement temp.

  • If temp<0 then add corresponding frequency from arr_2[] to count. Increment that frequency by 1.

  • If temp>0 then add corresponding frequency from arr_1[] to count. Increment that frequency by 1.

  • In the end, we have counted as the number of subarrays in arr[] that have the same number of even and odd elements in them

  • Return count as result.

Example

 Live Demo

#include <bits/stdc++.h>
using namespace std;
int Sub_even_odd(int arr[], int size){
   int count = 0;
   int temp = 0;
   int arr_1[size + 1] = {0};
   int arr_2[size + 1] = {0};
   arr_1[0] = 1;
   for (int i = 0; i < size; i++){
      if(arr[i] & 1 == 1)
         { temp++; }
      else
         { temp--; }
      if (temp < 0){
         count += arr_2[-temp];
         arr_2[-temp]++;
      }
      else{
         count += arr_1[temp];
         arr_1[temp]++;
      }
   }
   return count;
}
int main(){
   int arr[] = {3, 4, 6, 1, 2, 4, 10, 42};
   int size = sizeof(arr) / sizeof(arr[0]);
   cout<<"Count of subarrays with same even and odd elements are: "<<Sub_even_odd(arr,
size);
   return 0;
}

Output

If we run the above code it will generate the following output −

Count of subarrays with same even and odd elements are: 4

Updated on: 01-Dec-2020

335 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements