Count of sub-arrays with odd product - GeeksforGeeks

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 : 4 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++
Java
Python3
C#
Javascript
// C++ program to find the count of// sub-arrays with odd product#include <bits/stdc++.h>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[0]);     // Function call    cout << countSubArrayWithOddProduct(arr, n);     return 0;}
// 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 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# 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
<script>// Javascript program to find the count of// sub-arrays with odd product // Function that returns the count of// sub-arrays with odd productfunction countSubArrayWithOddProduct(A, N){     // Initialize the count variable    var count = 0;     // Initialize variable to store the    // last index with even number    var last = -1;     // Initialize variable to store    // count of continuous odd numbers    var K = 0;     // Loop through the array    for (var 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 Codevar arr = [ 12, 15, 7, 3, 25,              6, 2, 1, 1, 7 ];var n = arr.length; // Function calldocument.write( countSubArrayWithOddProduct(arr, n)); // This code is contributed by rrrtnx.</script>
16
 
Time Complexity: O(N) Auxiliary Space: O(1) 
bgangwar59
rutvik_56
divyeshrabadiya07
rrrtnx
Maths
subarray
Arrays
Competitive Programming
Mathematical
Arrays
Mathematical
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Stack Data Structure (Introduction and Program)
Top 50 Array Coding Problems for Interviews
Multidimensional Arrays in Java
Introduction to Arrays
Linear Search
Practice for cracking any coding interview
Competitive Programming - A Complete Guide
Arrow operator -> in C/C++ with Examples
Modulo 10^9+7 (1000000007)
Top 10 Algorithms and Data Structures for Competitive Programming