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++
// C++ Program to find the
// count of index pairs
// in the array positive
// range product
#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++) {
// Condition if number of
// negative elements is even
// then increase even_count
if (total_count % 2 == 0)
even_count++;
// Otherwise increase odd_count
else
odd_count++;
// Condition if current element
// is negative
if (arr[i] < 0)
total_count++;
// Condition if number of
// negative elements is even
// then add even_count
// in answer
if (total_count % 2 == 0)
ans += even_count;
// Otherwise add odd_count
// in answer
else
ans += odd_count;
}
cout << ans << "\n";
}
// Driver Code
int main()
{
int A[] = { 5, -3, 3, -1, 1 };
int size = sizeof(A) / sizeof(A[0]);
positiveProduct(A, size);
return 0;
}
Java
// Java program to find the count of
// index pairs in the array positive
// range product
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++)
{
// Condition if number of
// negative elements is even
// then increase even_count
if (total_count % 2 == 0)
{
even_count++;
}
// Otherwise increase odd_count
else
{
odd_count++;
}
// Condition if current element
// is negative
if (arr[i] < 0)
{
total_count++;
}
// Condition if number of
// negative elements is even
// then add even_count
// in answer
if (total_count % 2 == 0)
ans += even_count;
// Otherwise add odd_count
// in answer
else
ans += odd_count;
}
System.out.println(ans);
}
// Driver Code
public static void main(String[] args)
{
int A[] = { 5, -3, 3, -1, 1 };
int size = A.length;
positiveProduct(A, size);
}
}
// This code is contributed by divyeshrabadiya07
Python3
# Python3 program to find the count
# of index pairs in the array
# positive range product
def positiveProduct(arr, n):
even_count = 0
odd_count = 0
total_count = 0
ans = 0
for i in range(n):
# Condition if number of
# negative elements is even
# then increase even_count
if(total_count % 2 == 0):
even_count += 1
# Otherwise increase odd_count
else:
odd_count += 1
# Condition if current element
# is negative
if(arr[i] < 0):
total_count += 1
# Condition if number of
# negative elements is even
# then add even_count
# in answer
if(total_count % 2 == 0):
ans += even_count
# Otherwise add odd_count
# in answer
else:
ans += odd_count
print(ans)
# Driver Code
if __name__ == '__main__':
A = [ 5, -3, 3, -1, 1 ]
size = len(A)
positiveProduct(A, size)
# This code is contributed by Shivam Singh
C#
// C# program to find the count of
// index pairs in the array positive
// range product
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++)
{
// Condition if number of
// negative elements is even
// then increase even_count
if (total_count % 2 == 0)
{
even_count++;
}
// Otherwise increase odd_count
else
{
odd_count++;
}
// Condition if current element
// is negative
if (arr[i] < 0)
{
total_count++;
}
// Condition if number of
// negative elements is even
// then add even_count
// in answer
if (total_count % 2 == 0)
ans += even_count;
// Otherwise add odd_count
// in answer
else
ans += odd_count;
}
Console.WriteLine(ans);
}
// Driver Code
public static void Main(String[] args)
{
int []A = { 5, -3, 3, -1, 1 };
int size = A.Length;
positiveProduct(A, size);
}
}
// This code is contributed by 29AjayKumar
JavaScript
<script>
// Javascript program to find the count of
// index pairs in the array positive
// range product
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++)
{
// Condition if number of
// negative elements is even
// then increase even_count
if (total_count % 2 == 0)
{
even_count++;
}
// Otherwise increase odd_count
else
{
odd_count++;
}
// Condition if current element
// is negative
if (arr[i] < 0)
{
total_count++;
}
// Condition if number of
// negative elements is even
// then add even_count
// in answer
if (total_count % 2 == 0)
ans += even_count;
// Otherwise add odd_count
// in answer
else
ans += odd_count;
}
document.write(ans);
}
// Driver Code
let A = [5, -3, 3, -1, 1 ];
let size = A.length;
positiveProduct(A, size);
// This code is contributed by sravan kumar Gottumukkala
</script>
Time Complexity: O(N)
Space Complexity: O(1)
Similar Reads
Count pairs in Array whose product is a Kth power of any positive integer Given an array arr[] of length N and an integer K, the task is to count pairs in the array whose product is Kth power of a positive integer, i.e. A[i] * A[j] = ZK for any positive integer Z. Examples: Input: arr[] = {1, 3, 9, 8, 24, 1}, K = 3 Output: 5 Explanation: There are 5 such pairs, those can
14 min read
Count pairs from a given array whose product lies in a given range Given an array arr[] of size N, and integers L and R, the task is to count the number of pairs [arri , arrj] such that i < j and the product of arr[i] * arr[j] lies in the given range [L, R], i.e. L ? arr[i] * arr[j] ? R. Examples: Input: arr[ ] = {4, 1, 2, 5}, L = 4, R = 9Output: 3Explanation: V
13 min read
Count pairs in a sorted array whose product is less than k Given a sorted integer array and number k, the task is to count pairs in an array whose product is less than x. Examples: Input: A = {2, 3, 5, 6}, k = 16 Output: 4 Pairs having product less than 16: (2, 3), (2, 5), (2, 6), (3, 5) Input: A = {2, 3, 4, 6, 9}, k = 20 Output: 6 Pairs having product less
12 min read
Count of pairs in Array whose product is divisible by K Given an array A[] and positive integer K, the task is to count the total number of pairs in the array whose product is divisible by K. Examples : Input: A[] = [1, 2, 3, 4, 5], K = 2Output: 7Explanation: The 7 pairs of indices whose corresponding products are divisible by 2 are(0, 1), (0, 3), (1, 2)
9 min read
Count all possible pairs in given Array with product K Given an integer array arr[] of size N and a positive integer K, the task is to count all the pairs in the array with a product equal to K. Examples: Input: arr[] = {1, 2, 16, 4, 4, 4, 8 }, K=16Output: 5Explanation: Possible pairs are (1, 16), (2, 8), (4, 4), (4, 4), (4, 4) Input: arr[] = {1, 10, 20
11 min read