Total Negative and Positive Product Array Pairs
Last Updated :
29 Jan, 2024
Given an array arr[] of size n, the task is to calculate two following values:
- Number of pairs [l,r] such that product of all the element from indices l to r is negative.
- Number of pairs [l,r] such that product of all the element from indices l to r is positive.
Example:
Input: n = 5, A = { 5, -3, 3, -1, 1 }
Output: 8 7
Input: n = 10, A = {4, 2, -4, 3, 1, 2, -4, 3, 2, 3}
Output: 28 27
Total Negative and Positive Product Array Pairs using Dynamic Programming:
In this problem, dynamic programming is used to efficiently calculate the number of subsequences with negative and positive products. The dynamic programming approach involves maintaining counts of subsequences ending at the current position, and these counts are updated based on the sign of the current element. Let’s take a look into the dynamic programming aspect of the solution:
Dynamic Programming Variables:
- dpneg: Count of subsequences ending at the current position with a negative product.
- dppos: Count of subsequences ending at the current position with a positive product.
- now: A variable to keep track of the sign of the current product.
Iteration and State Update:
- The solution iterates through the given sequence.
- For each element ai, it updates the now variable by multiplying it with the sign of ai.
- Depending on the sign of now, the dpneg and dppos counts are updated accordingly.
Count Accumulation:
- The counts of negative and positive subsequences are accumulated as the sequence is traversed.
- At each position, the counts are updated based on the sign of the current element.
Pair Counting:
- For each transition point where the sign of the product changes, the counts of pairs are updated.
- If the product becomes negative, it contributes to the count of negative pairs.
- If the product becomes positive, it contributes to the count of positive pairs.
Steps:
- Initialize variables neg and pos to keep track of the count of pairs with negative and positive products, respectively.
- Initialize variables dpneg, dppos, and now.
- Loop through each element in the array.
- For each element a:
- Update now by multiplying it with the sign of a.
- Depending on the sign of now:
- If now is negative:
- Increment dpneg.
- Update neg by adding dppos + 1.
- Update pos by adding dpneg – 1.
- If now is positive:
- Increment dppos.
- Update neg by adding dpneg.
- Update pos by adding dppos.
- Output the count of pairs with negative and positive products (neg and pos).
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
pair< long long , long long > solve(vector< long long >& A)
{
long long neg = 0, pos = 0;
long long dpneg = 0, dppos = 0, now = 1;
for ( auto & a : A) {
now *= (a < 0 ? -1 : 1);
if (now < 0) {
dpneg++;
neg += dppos + 1;
pos += dpneg - 1;
}
else {
dppos++;
neg += dpneg;
pos += dppos;
}
}
return { neg, pos };
}
int main()
{
long long n = 5;
vector< long long > A = { 5, -3, 3, -1, 1 };
auto res = solve(A);
cout << res.first << ' ' << res.second << endl;
return 0;
}
|
Java
import java.util.*;
public class Main {
static class Pair {
long first, second;
public Pair( long first, long second)
{
this .first = first;
this .second = second;
}
}
static Pair solve(ArrayList<Long> A)
{
long neg = 0 , pos = 0 ;
long dpneg = 0 , dppos = 0 , now = 1 ;
for ( long a : A) {
now *= (a < 0 ) ? - 1 : 1 ;
if (now < 0 ) {
dpneg++;
neg += dppos + 1 ;
pos += dpneg - 1 ;
}
else {
dppos++;
neg += dpneg;
pos += dppos;
}
}
return new Pair(neg, pos);
}
public static void main(String[] args)
{
long n = 5 ;
ArrayList<Long> A = new ArrayList<>(
Arrays.asList(5L, -3L, 3L, -1L, 1L));
Pair res = solve(A);
System.out.println(res.first + " " + res.second);
}
}
|
Python3
def solve(A):
neg = 0
pos = 0
dpneg = 0
dppos = 0
now = 1
for a in A:
now * = ( - 1 if a < 0 else 1 )
if now < 0 :
dpneg + = 1
neg + = dppos + 1
pos + = dpneg - 1
else :
dppos + = 1
neg + = dpneg
pos + = dppos
return [neg, pos]
n = 5
A = [ 5 , - 3 , 3 , - 1 , 1 ]
res = solve(A)
print (res[ 0 ], res[ 1 ])
|
C#
using System;
using System.Collections.Generic;
public class Program
{
public static Tuple< long , long > Solve(List< long > A)
{
long neg = 0, pos = 0;
long dpneg = 0, dppos = 0, now = 1;
foreach ( var a in A)
{
now *= (a < 0 ? -1 : 1);
if (now < 0)
{
dpneg++;
neg += dppos + 1;
pos += dpneg - 1;
}
else
{
dppos++;
neg += dpneg;
pos += dppos;
}
}
return new Tuple< long , long >(neg, pos);
}
public static void Main()
{
List< long > A = new List< long > { 5, -3, 3, -1, 1 };
var res = Solve(A);
Console.WriteLine($ "{res.Item1} {res.Item2}" );
}
}
|
Javascript
function solve(A) {
let neg = 0, pos = 0;
let dpneg = 0, dppos = 0, now = 1;
for (let a of A) {
now *= (a < 0 ? -1 : 1);
if (now < 0) {
dpneg++;
neg += dppos + 1;
pos += dpneg - 1;
}
else {
dppos++;
neg += dpneg;
pos += dppos;
}
}
return [neg, pos];
}
let n = 5;
let A = [5, -3, 3, -1, 1];
let res = solve(A);
console.log(res[0] + ' ' + res[1]);
|
Time complexity: O(N), where N is the size of the input array.
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...