Count of sub-arrays with odd product

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++

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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 product
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 Code
int 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;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to find the count of 
// sub-arrays with odd product 
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 Code
public 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

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 program to find the count of
# sub-arrays with odd product
  
# Function that returns the count of
# sub-arrays with odd product
def 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 Code
if __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

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to find the count of 
// sub-arrays with odd product 
using 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 code
static 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

chevron_right


Output:

16

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

competitive-programming-img




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.