Count of sub-arrays with odd product
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 : 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++
#include <bits/stdc++.h>
using namespace std;
int countSubArrayWithOddProduct( int * A, int N)
{
int count = 0;
int last = -1;
int K = 0;
for ( int i = 0; i < N; i++) {
if (A[i] % 2 == 0) {
K = (i - last - 1);
count += (K * (K + 1) / 2);
last = i;
}
}
K = (N - last - 1);
count += (K * (K + 1) / 2);
return count;
}
int main()
{
int arr[] = { 12, 15, 7, 3, 25,
6, 2, 1, 1, 7 };
int n = sizeof (arr) / sizeof (arr[0]);
cout << countSubArrayWithOddProduct(arr, n);
return 0;
}
|
Java
class GFG {
static int countSubArrayWithOddProduct( int A[],
int N)
{
int count = 0 ;
int last = - 1 ;
int K = 0 ;
for ( int i = 0 ; i < N; i++)
{
if (A[i] % 2 == 0 )
{
K = (i - last - 1 );
count += (K * (K + 1 ) / 2 );
last = i;
}
}
K = (N - last - 1 );
count += (K * (K + 1 ) / 2 );
return count;
}
public static void main(String args[])
{
int arr[] = { 12 , 15 , 7 , 3 , 25 , 6 , 2 , 1 , 1 , 7 };
int n = arr.length;
System.out.println(countSubArrayWithOddProduct(arr, n));
}
}
|
Python3
def countSubArrayWithOddProduct(A, N):
count = 0
last = - 1
K = 0
for i in range (N):
if (A[i] % 2 = = 0 ):
K = (i - last - 1 )
count + = (K * (K + 1 ) / 2 )
last = i
K = (N - last - 1 )
count + = (K * (K + 1 ) / 2 )
return count
if __name__ = = '__main__' :
arr = [ 12 , 15 , 7 , 3 , 25 , 6 , 2 , 1 , 1 , 7 ]
n = len (arr)
print ( int (countSubArrayWithOddProduct(arr, n)))
|
C#
using System;
class GFG{
static int countSubArrayWithOddProduct( int [] A,
int N)
{
int count = 0;
int last = -1;
int K = 0;
for ( int i = 0; i < N; i++)
{
if (A[i] % 2 == 0)
{
K = (i - last - 1);
count += (K * (K + 1) / 2);
last = i;
}
}
K = (N - last - 1);
count += (K * (K + 1) / 2);
return count;
}
static void Main()
{
int [] arr = { 12, 15, 7, 3, 25,
6, 2, 1, 1, 7 };
int n = arr.Length;
Console.WriteLine(countSubArrayWithOddProduct(arr, n));
}
}
|
Javascript
<script>
function countSubArrayWithOddProduct(A, N)
{
var count = 0;
var last = -1;
var K = 0;
for ( var i = 0; i < N; i++)
{
if (A[i] % 2 == 0)
{
K = (i - last - 1);
count += (K * (K + 1) / 2);
last = i;
}
}
K = (N - last - 1);
count += (K * (K + 1) / 2);
return count;
}
var arr = [ 12, 15, 7, 3, 25,
6, 2, 1, 1, 7 ];
var n = arr.length;
document.write( countSubArrayWithOddProduct(arr, n));
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...