Subarray Count

3

4 votes
Basic Programming, Hashmap, C++
Problem

Given an Array of N elements.

Find the count of sub-arrays, such that the product of elements in it is ODD. 

Example

If A = [9, 6, 15, 7, 1] . There are 7 subarrays with product of elements as Odd.

  • Subarray [15, 7, 1] has product of elements as 15*7*1 = 105 which is Odd.
  • Subarray [15,7] has product of elements as 105 which is Odd.
  • Subarray [7,1] has product of elements as 7 which is Odd.
  • Similarly, Subarray [9], [15], [7], [1] has product of elements as Odd. 

Function Description

Complete the countSubarray function provided in the editor. This function takes the following 2 parameters and returns the count of sub-arrays with ODD product of elements.

  • N : Represents the number of elements
  • A : Represents the elements in the array

Input :
1. First line contain an integer N, denoting number of elements.
2. Next line contain N space separated integers.

Constraints :
1 <= N <= 105
1 <= |A[i]| <= 106

Output :
Print number of sub-arrays with ODD product.

Time Limit: 1
Memory Limit: 256
Source Limit:
Explanation

All subarrays except which include 2 are valid answers.

Editor Image

?