Count of index pairs in array whose range product is a positive integer
Last Updated :
10 May, 2021
Given an array A of non-zero integers, the task is to find the number of pairs (l, r) where (l <= r) such that A[l]*A[l+1]*A[l+2]….A[r] is positive.
Examples:
Input: A = {5, -3, 3, -1, 1}
Output: 7
Explanation:
First pair, (1, 1) = 5 is positive
Second pair, (3, 3) = 3 is positive
Third pair, (1, 4) = 5 * -3 * 3 * -1 = 45 is positive
Forth pair, (2, 4) = -3 * 3 * -1 = 9 is positive
Fifth pair, (1, 5) = 5 * -3 * 3 * -1 * 1 = 45 is positive
Sixth pair, (2, 5) = -3 * 3 * -1 * 1 = 9 is positive
Seventh pair, (5, 5) = 1 is positive
So, there are seven pairs with positive product.
Input: A = {4, 2, -4, 3, 1, 2, -4, 3, 2, 3}
Output: 27
Approach:
The idea is to check possible number pairs for every array element.
- Iterate through an array, follow the below steps for every element in array.
- Keep a track of the number of elements having an even number of negative elements before them (as even_count) and number of elements having odd number of negative elements before them (as odd_count).
- Store the total number of negative elements till now (as total_count).
- If total_count is even then add even_count to the answer. Otherwise add odd_count.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void positiveProduct( int arr[], int n)
{
int even_count = 0;
int odd_count = 0;
int total_count = 0;
int ans = 0;
for ( int i = 0; i < n; i++) {
if (total_count % 2 == 0)
even_count++;
else
odd_count++;
if (arr[i] < 0)
total_count++;
if (total_count % 2 == 0)
ans += even_count;
else
ans += odd_count;
}
cout << ans << "\n" ;
}
int main()
{
int A[] = { 5, -3, 3, -1, 1 };
int size = sizeof (A) / sizeof (A[0]);
positiveProduct(A, size);
return 0;
}
|
Java
class GFG{
public static void positiveProduct( int arr[],
int n)
{
int even_count = 0 ;
int odd_count = 0 ;
int total_count = 0 ;
int ans = 0 ;
for ( int i = 0 ; i < n; i++)
{
if (total_count % 2 == 0 )
{
even_count++;
}
else
{
odd_count++;
}
if (arr[i] < 0 )
{
total_count++;
}
if (total_count % 2 == 0 )
ans += even_count;
else
ans += odd_count;
}
System.out.println(ans);
}
public static void main(String[] args)
{
int A[] = { 5 , - 3 , 3 , - 1 , 1 };
int size = A.length;
positiveProduct(A, size);
}
}
|
Python3
def positiveProduct(arr, n):
even_count = 0
odd_count = 0
total_count = 0
ans = 0
for i in range (n):
if (total_count % 2 = = 0 ):
even_count + = 1
else :
odd_count + = 1
if (arr[i] < 0 ):
total_count + = 1
if (total_count % 2 = = 0 ):
ans + = even_count
else :
ans + = odd_count
print (ans)
if __name__ = = '__main__' :
A = [ 5 , - 3 , 3 , - 1 , 1 ]
size = len (A)
positiveProduct(A, size)
|
C#
using System;
class GFG{
public static void positiveProduct( int []arr,
int n)
{
int even_count = 0;
int odd_count = 0;
int total_count = 0;
int ans = 0;
for ( int i = 0; i < n; i++)
{
if (total_count % 2 == 0)
{
even_count++;
}
else
{
odd_count++;
}
if (arr[i] < 0)
{
total_count++;
}
if (total_count % 2 == 0)
ans += even_count;
else
ans += odd_count;
}
Console.WriteLine(ans);
}
public static void Main(String[] args)
{
int []A = { 5, -3, 3, -1, 1 };
int size = A.Length;
positiveProduct(A, size);
}
}
|
Javascript
<script>
function positiveProduct(arr,n)
{
let even_count = 0;
let odd_count = 0;
let total_count = 0;
let ans = 0;
for (let i = 0; i < n; i++)
{
if (total_count % 2 == 0)
{
even_count++;
}
else
{
odd_count++;
}
if (arr[i] < 0)
{
total_count++;
}
if (total_count % 2 == 0)
ans += even_count;
else
ans += odd_count;
}
document.write(ans);
}
let A = [5, -3, 3, -1, 1 ];
let size = A.length;
positiveProduct(A, size);
</script>
|
Time Complexity: O(N)
Space Complexity: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...