Count of sub-arrays with odd product

• Difficulty Level : Medium
• Last Updated : 26 Apr, 2021

Given an integer array arr[] of size N, the task is to count the number of sub-arrays that have an odd product.
Examples:

Input : arr[] = {5, 1, 2, 3, 4}
Output :
Explanation: The sub-arrays with odd product are-
{5}, {1}, {3}, {5, 1}. Hence the count is 4.
Input : arr[] = {12, 15, 7, 3, 25, 6, 2, 1, 1, 7}
Output : 16

Naive Approach: A simple solution is to calculate the product of every sub-array and check whether it is odd or not and calculate the count accordingly.
Time Complexity: O(N2)
Efficient Approach: An odd product is possible only by the product of odd numbers. Thus, for every K continuous odd numbers in the array, the count of sub-arrays with the odd product increases by K*(K+1)/2. One way of counting continuous odd numbers is to calculate the difference between the indexes of every two consecutive even numbers and subtract it by 1. For the calculation, -1 and N are considered as indexes of even numbers.
Below is the implementation of the above approach:

C++

 // C++ program to find the count of// sub-arrays with odd product#include using namespace std; // Function that returns the count of// sub-arrays with odd productint countSubArrayWithOddProduct(int* A, int N){    // Initialize the count variable    int count = 0;     // Initialize variable to store the    // last index with even number    int last = -1;     // Initialize variable to store    // count of continuous odd numbers    int K = 0;     // Loop through the array    for (int i = 0; i < N; i++) {        // Check if the number        // is even or not        if (A[i] % 2 == 0) {            // Calculate count of continuous            // odd numbers            K = (i - last - 1);             // Increase the count of sub-arrays            // with odd product            count += (K * (K + 1) / 2);             // Store the index of last            // even number            last = i;        }    }     // N considered as index of    // even number    K = (N - last - 1);     count += (K * (K + 1) / 2);     return count;} // Driver Codeint main(){    int arr[] = { 12, 15, 7, 3, 25,                  6, 2, 1, 1, 7 };     int n = sizeof(arr) / sizeof(arr);     // Function call    cout << countSubArrayWithOddProduct(arr, n);     return 0;}

Java

 // Java program to find the count of// sub-arrays with odd productclass GFG { // Function that returns the count of// sub-arrays with odd productstatic int countSubArrayWithOddProduct(int A[],                                       int N){         // Initialize the count variable    int count = 0;     // Initialize variable to store the    // last index with even number    int last = -1;     // Initialize variable to store    // count of continuous odd numbers    int K = 0;     // Loop through the array    for(int i = 0; i < N; i++)    {        // Check if the number       // is even or not       if (A[i] % 2 == 0)       {            // Calculate count of continuous           // odd numbers           K = (i - last - 1);                       // Increase the count of sub-arrays           // with odd product           count += (K * (K + 1) / 2);                       // Store the index of last           // even number           last = i;       }    }     // N considered as index of    // even number    K = (N - last - 1);    count += (K * (K + 1) / 2);         return count;} // Driver Codepublic static void main(String args[]){    int arr[] = { 12, 15, 7, 3, 25, 6, 2, 1, 1, 7 };    int n = arr.length;     // Function call    System.out.println(countSubArrayWithOddProduct(arr, n));}} // This code is contributed by rutvik_56

Python3

 # Python3 program to find the count of# sub-arrays with odd product # Function that returns the count of# sub-arrays with odd productdef countSubArrayWithOddProduct(A, N):         # Initialize the count variable    count = 0     # Initialize variable to store the    # last index with even number    last = -1     # Initialize variable to store    # count of continuous odd numbers    K = 0     # Loop through the array    for i in range(N):                 # Check if the number        # is even or not        if (A[i] % 2 == 0):                         # Calculate count of continuous            # odd numbers            K = (i - last - 1)             # Increase the count of sub-arrays            # with odd product            count += (K * (K + 1) / 2)             # Store the index of last            # even number            last = i     # N considered as index of    # even number    K = (N - last - 1)     count += (K * (K + 1) / 2)    return count # Driver Codeif __name__ == '__main__':         arr = [ 12, 15, 7, 3, 25, 6, 2, 1, 1, 7 ]    n = len(arr)     # Function call    print(int(countSubArrayWithOddProduct(arr, n))) # This code is contributed by Bhupendra_Singh

C#

 // C# program to find the count of// sub-arrays with odd productusing System;class GFG{ // Function that returns the count of// sub-arrays with odd product    static int countSubArrayWithOddProduct(int[] A,                                       int N){             // Initialize the count variable    int count = 0;         // Initialize variable to store the    // last index with even number    int last = -1;         // Initialize variable to store    // count of continuous odd numbers    int K = 0;         // Loop through the array    for(int i = 0; i < N; i++)    {               // Check if the number       // is even or not       if (A[i] % 2 == 0)       {                       // Calculate count of continuous           // odd numbers           K = (i - last - 1);                       // Increase the count of sub-arrays           // with odd product           count += (K * (K + 1) / 2);                       // Store the index of last           // even number           last = i;       }    }         // N considered as index of    // even number    K = (N - last - 1);    count += (K * (K + 1) / 2);         return count;} // Driver codestatic void Main(){    int[] arr = { 12, 15, 7, 3, 25,                  6, 2, 1, 1, 7 };    int n = arr.Length;         // Function call    Console.WriteLine(countSubArrayWithOddProduct(arr, n));}} // This code is contributed by divyeshrabadiya07

Javascript


Output:
16

Time Complexity: O(N)
Auxiliary Space: O(1)

My Personal Notes arrow_drop_up