Find the Number of Subarrays with Odd Sum using C++


Subarrays are the contiguous part of an array. For example, we consider an array [5, 6, 7, 8], then there are ten non-empty subarrays like (5), (6), (7), (8), (5, 6), (6,7), (7,8), (5,6,7), (6,7,8) and (5,6,7,8).

In this guide, we will explain every possible information to find the number of subarrays with odd sums in C++. For finding the number of subarrays with the odd sum, we can use different approaches, so here is a simple example for it −

Input : array = {9,8,7,6,5}
Output : 9

Explanation :
Sum of subarray -
{9} = 9
{7} = 7
{5} = 5
{9,8} = 17
{8,7} = 15
{7,6} = 13
{6,5} = 11
{8,7,6} = 21
{9,8,7,6,5} = 35

Brute Force Approach

With this approach we can simply check whether the sum of elements in all subarrays is even or odd, If it is even we will reject that subarray and will count subarrays with sum odd, It is not an efficient approach as complexity of this code is O(n2).

Example

#include <bits/stdc++.h>
using namespace std;
int main(){
    int n=5, temp = 0;
    int a[n-1] = { 9,8,7,6,5 } ; // declaring our array.
    int cnt = 0; // counter variable.
    for(int i = 0; i < n; i++){
        temp = 0; // refreshing our temp sum.
        for(int j = i; j < n; j++){ // this loop will make our subarrays starting from i till n-1.
            temp = temp + a[j];
            if( temp % 2 == 1 )
                cnt++;
        }
    }
    cout << "Number of subarrays with odd sum : " << cnt << "\n";
    return 0;
}

Output

Number of subarrays with odd sum : 9

Explanation of the Above Code

Nested loops are used in this code where the outer loop is used to increment the value of I, which is pointing at each value of the array from starting; the inner loop is used to find subarray starting from the position " i " having odd sum.

Efficient Approach

In this approach, we are processing every element from the 0th position in the array. If the current element is odd, increase an odd counter and increase an even counter for every even number. If we found an odd number, then swap the values of even and odd because adding an odd number to the subarray will change its parity and finally add a count to the result. The complexity of this code is O(n), as we are processing every element.

Example

 
#include <bits/stdc++.h>
using namespace std;
int main(){
    int odd = 0, even = 0,  result = 0,n=5,i,temp;
    int arr[ n-1 ] = { 9,8,7,6,5}; // initialising the array
     // for loop for processing every element of array
    for ( i = 0 ; i < n ; i ++ )  {
        if ( arr[ i ] % 2 == 0 ) {
            even++;
        } else {
          // swapping even odd values
            temp = even;
            even = odd;
            odd = temp + 1;
        }
        result += odd;
    }
    cout << "Number of subarrays with odd sum : " << result;
}

Output

Number of subarrays with odd sum : 9

Explanation of the Above Code

In this code, we check every element for even/odd and increment even counter for even number and odd counter for an odd number. Also, we are swapping odd-even counter values if an odd number is found; otherwise, it will change the parity of the subarray. Then adding the value of the odd counter to the result variable after every iteration.

Conclusion

In this article, we explained how to find the number of subarrays with sum odd from the Brute force approach, which is generating every subarray with sum odd and incrementing the count. The time complexity of this code is O(n2). An efficient approach is going through each element of the array and increment odd/even counter variables with every odd/even number found and swapping counters if an odd number is found; the time complexity of this code is O(n). Hope you find this article helpful in understanding the problem and solution.

Updated on: 25-Nov-2021

341 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements