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++
// 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;
}
Java
// 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
Python3
# 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
C#
// 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
JavaScript
<script>
// Javascript program to find the count of
// sub-arrays with odd product
// Function that returns the count of
// sub-arrays with odd product
function 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 Code
var arr = [ 12, 15, 7, 3, 25,
6, 2, 1, 1, 7 ];
var n = arr.length;
// Function call
document.write( countSubArrayWithOddProduct(arr, n));
// This code is contributed by rrrtnx.
</script>
Time Complexity: O(N)
Auxiliary Space: O(1)
Similar Reads
Number of sub arrays with odd sum Given an array, find the number of subarrays whose sum is odd. Examples: Input : arr[] = {5, 4, 4, 5, 1, 3} Output : 12There are possible subarrays with oddsum. The subarrays are 1) {5} Sum = 5 (At index 0) 2) {5, 4} Sum = 9 3) {5, 4, 4} Sum = 13 4) {5, 4, 4, 5, 1} Sum = 19 5) {4, 4, 5} Sum = 13 6)
15+ min read
Count sub-arrays whose product is divisible by k Given an integer K and an array arr[], the task is to count all the sub-arrays whose product is divisible by K.Examples: Input: arr[] = {6, 2, 8}, K = 4 Output: 4 Required sub-arrays are {6, 2}, {6, 2, 8}, {2, 8}and {8}.Input: arr[] = {9, 1, 14}, K = 6 Output: 1 Naive approach: Run nested loops and
15+ min read
Count of triplets in an Array with odd sum Given an array arr[] with N integers, find the number of triplets of i, j and k such that 1<= i < j < k <= N and arr[i] + arr[j] + arr[k] is odd. Example: Input: arr[] = {1, 2, 3, 4, 5}Output: 4Explanation: The given array contains 4 triplets with an odd sum. They are {1, 2, 4}, {1, 3, 5
6 min read
Count Pairs from two arrays with even sum Given two arrays A[] and B[] of N and M integers respectively. The task is to count the number of unordered pairs formed by choosing an element from array A[] and other from array B[] in such a way that their sum is an even number. Note that an element will only be a part of a single pair.Examples:
7 min read
Count pairs with Even Product from two given arrays Given two arrays, arr[] and brr[] of size N and M respectively, the task is to find the count of pairs (arr[i], brr[j]) such that the product of elements of the pairs is an even number. Examples: Input: arr[] = { 1, 2, 3 }, brr[] = { 1, 2 } Output: 4 Explanation: Pairs with even product are: { (arr[
10 min read