Find the Number of Subarrays with m Odd Numbers using C++

C++Server Side ProgrammingProgramming

If you have ever used C ++, you must know what subarrays are and how useful they are. As we know that, in C++, we can solve multiple mathematical problems easily. So in this article, we will explain the complete information on how we can find M odd numbers with the help of these subarrays in C++.

In this problem, we need to find many subarrays formed with the given array and integer m where each subarray contains exactly m odd numbers. So here is the simple example of this approach −

Input : array = { 6,3,5,8,9 }, m = 2
Output : 5
Explanation : Subarrays with exactly 2 odd numbers are
{ 3,5 }, { 6,3,5 }, { 3,5,8 }, { 5,8,9 }, { 6,3,5,8 }, { 3,5,8,9 }

Input : array = { 1,6,3,2,5,4 }, m = 2
Output : 6
Explanation : Subarrays with exactly 2 odd numbers are
{ 1,6,3 }, { 3,2,5 }, { 1,6,3,2 }, { 6,3,2,5 }, { 3,2,5,4 }, { 6,3,2,5,4 }

First Approach

In this approach, all possible subarrays are generated from a given array, and each subarray is checked for exactly m odd number. It is a simple generate and find approach, and time complexity of this approach is O(n2).

Example

#include <bits/stdc++.h>
using namespace std;
int main (){
    int a[] = { 1, 6, 3, 2, 5, 4 };
    int n = 6, m = 2, count = 0; // n is size of array, m numbers to be find in subarrays,
                              // count is number of subarray with m odd numbers
    for (int i = 0; i < n; i++){ // outer loop to process each element.
        int odd = 0;
        for (int j = i; j < n; j++) {// inner loop to find subarray with m number
            if (a[j] % 2)
                odd++;
            if (odd == m) // if odd numbers become equals to m.
                count++;
        }
    }
    cout << "Number of subarrays with n numbers are: " << count;
    return 0;
}

Output

Number of subarrays with n numbers are: 6

Explanation of the Above Code

In this code, we are using nested loops to find subarrays with m odd numbers, and the outer loop is used to increment "i", which will be used to process each element in an array.

The inner loop is used to find subarray and process elements until the odd counter reaches m, increasing the result counter count for each subarray found, and finally printing the result stored in the count variable.

Second Approach

Another approach is to create an array for storing the number of prefixes with "i" odd numbers, process every element, and increase the number of odd numbers for every odd number found.

When the count of odd numbers exceeds or becomes equal to m, add the number at (odd - m ) position in the prefix array.

When odd becomes greater than or equal to m, we calculate the number of subarrays formed till the index and "odd - m "numbers are added to the count variable. The result is stored in the count variable after every element is processed.

Example

#include <bits/stdc++.h>
using namespace std;
int main (){
    int array[ ] = { 1, 6, 3, 2, 5, 4 };
    int n = 6, m = 2, count = 0, odd = 0, i;
    int prefix_array[n + 1] = { 0 };
    // outer loop to process every element of array
    for (i = 0; i < n; i++){
        prefix_array[odd] = prefix_array[odd] + 1;    // implementing value at odd index in prefix_array[ ]
        // if array element is odd then increment odd variable
        if (array[i] % 2 == 0)
            odd++;
        // if Number of odd element becomes equal or greater than m
        //  then find the number of possible subarrays that can be formed till the index.
        if (odd >= m)
            count += prefix_array[odd - m];
    }
    cout << "Number of subarrays with n numbers are: " << count;
    return 0;
}

Output

Number of subarrays with n numbers are: 6

Explanation of the Above Code

Initializing array and variables with starting values −

int array[ 6 ] = { 1, 6, 3, 2, 5, 4 };
int n = 6, m = 2, count = 0, odd = 0, i;
int prefix_array[n + 1] = { 0 };

In this, we are initializing variable n with the size of the array, m with a number of odd numbers to find, count with 0 to keep count of possible subarrays, odd with 0, and prefix_array of size n + 1 with 0.

Understanding the Loop

for (i = 0; i < n; i++){
   prefix_array[odd] = prefix_array[odd] + 1;
   if (array[i] % 2 == 0)
      odd++;
      if (odd >= m)
         count += prefix_array[odd - m];
}

In this loop, we are implementing values at the odd index in prefix_array[ ], then incrementing odd variables if an odd number is found. We find the number of subarrays can be formed till the index when an odd variable becomes equal to or greater than m.

Finally, we print a number of subarrays with m odd numbers stored in count variables and getting the output.

Conclusion

In this article, we understand the approach to find the number of subarrays with m odd numbers from two approaches −

  • Generating every subarray and checking for m odd numbers in it, and incrementing the count for each subarray found. The time complexity of this code is O(n2).

  • Efficient approach, which is going through each element of the array and creating a prefix array, and finding the result with the help of a prefix array. The time complexity of this code is O(n).

Hope you find this article helpful in understanding the problem and solution.

raja
Updated on 25-Nov-2021 05:45:28

Advertisements