Open In App

Total Negative and Positive Product Array Pairs

Last Updated : 29 Jan, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

Given an array arr[] of size n, the task is to calculate two following values:

  1. Number of pairs [l,r] such that product of all the element from indices l to r is negative.
  2. 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;
 
// Function to solve the problem
pair<long long, long long> solve(vector<long long>& A)
{
    // Initialize variables to count pairs with negative and
    // positive products
    long long neg = 0, pos = 0;
 
    // Variables for dynamic programming
    long long dpneg = 0, dppos = 0, now = 1;
 
    // Iterate through the array
    for (auto& a : A) {
        // Update 'now' by multiplying it with the sign of
        // 'a'
        now *= (a < 0 ? -1 : 1);
 
        // Depending on the sign of 'now'
        if (now < 0) {
            // Increment 'dpneg'
            dpneg++;
 
            // Update 'neg' by adding 'dppos + 1'
            neg += dppos + 1;
 
            // Update 'pos' by adding 'dpneg - 1'
            pos += dpneg - 1;
        }
        else {
            // Increment 'dppos'
            dppos++;
 
            // Update 'neg' by adding 'dpneg'
            neg += dpneg;
 
            // Update 'pos' by adding 'dppos'
            pos += dppos;
        }
    }
 
    // Return the counts of pairs with negative and positive
    // products
    return { neg, pos };
}
 
// Main function
int main()
{
    // Provided static input
    long long n = 5;
    vector<long long> A = { 5, -3, 3, -1, 1 };
 
    // Call the solve function to get the result
    auto res = solve(A);
 
    // Output the result
    cout << res.first << ' ' << res.second << endl;
 
    return 0;
}


Java




import java.util.*;
 
public class Main {
    // Function to solve the problem
    static class Pair {
        long first, second;
 
        public Pair(long first, long second)
        {
            this.first = first;
            this.second = second;
        }
    }
 
    static Pair solve(ArrayList<Long> A)
    {
        // Initialize variables to count pairs with negative
        // and positive products
        long neg = 0, pos = 0;
 
        // Variables for dynamic programming
        long dpneg = 0, dppos = 0, now = 1;
 
        // Iterate through the array
        for (long a : A) {
            // Update 'now' by multiplying it with the sign
            // of 'a'
            now *= (a < 0) ? -1 : 1;
 
            // Depending on the sign of 'now'
            if (now < 0) {
                // Increment 'dpneg'
                dpneg++;
 
                // Update 'neg' by adding 'dppos + 1'
                neg += dppos + 1;
 
                // Update 'pos' by adding 'dpneg - 1'
                pos += dpneg - 1;
            }
            else {
                // Increment 'dppos'
                dppos++;
 
                // Update 'neg' by adding 'dpneg'
                neg += dpneg;
 
                // Update 'pos' by adding 'dppos'
                pos += dppos;
            }
        }
 
        // Return the counts of pairs with negative and
        // positive products
        return new Pair(neg, pos);
    }
 
    // Main function
    public static void main(String[] args)
    {
        // Provided static input
        long n = 5;
        ArrayList<Long> A = new ArrayList<>(
            Arrays.asList(5L, -3L, 3L, -1L, 1L));
 
        // Call the solve function to get the result
        Pair res = solve(A);
 
        // Output the result
        System.out.println(res.first + " " + res.second);
    }
}


Python3




def solve(A):
    # Initialize variables to count pairs
    # with negative and positive products
    neg = 0
    pos = 0
 
    # Variables for dynamic programming
    dpneg = 0
    dppos = 0
    now = 1
 
    # Iterate through the array
    for a in A:
        # Update 'now' by multiplying
        # it with the sign of 'a'
        now *= (-1 if a < 0 else 1)
 
        # Depending on the sign of 'now'
        if now < 0:
            # Increment 'dpneg'
            dpneg += 1
 
            # Update 'neg' by adding 'dppos + 1'
            neg += dppos + 1
 
            # Update 'pos' by adding 'dpneg - 1'
            pos += dpneg - 1
        else:
            # Increment 'dppos'
            dppos += 1
 
            # Update 'neg' by adding 'dpneg'
            neg += dpneg
 
            # Update 'pos' by adding 'dppos'
            pos += dppos
 
    # Return the counts of pairs with
    # negative and positive products
    return [neg, pos]
 
# Provided static input
n = 5
A = [5, -3, 3, -1, 1]
 
# Call the solve function
# to get the result
res = solve(A)
 
# Output the result
print(res[0], res[1])


C#




using System;
using System.Collections.Generic;
 
public class Program
{
    // Function to solve the problem
    public static Tuple<long, long> Solve(List<long> A)
    {
        // Initialize variables to count pairs with negative and
        // positive products
        long neg = 0, pos = 0;
 
        // Variables for dynamic programming
        long dpneg = 0, dppos = 0, now = 1;
 
        // Iterate through the array
        foreach (var a in A)
        {
            // Update 'now' by multiplying it with the sign of 'a'
            now *= (a < 0 ? -1 : 1);
 
            // Depending on the sign of 'now'
            if (now < 0)
            {
                // Increment 'dpneg'
                dpneg++;
 
                // Update 'neg' by adding 'dppos + 1'
                neg += dppos + 1;
 
                // Update 'pos' by adding 'dpneg - 1'
                pos += dpneg - 1;
            }
            else
            {
                // Increment 'dppos'
                dppos++;
 
                // Update 'neg' by adding 'dpneg'
                neg += dpneg;
 
                // Update 'pos' by adding 'dppos'
                pos += dppos;
            }
        }
 
        // Return the counts of pairs with negative and positive
        // products
        return new Tuple<long, long>(neg, pos);
    }
 
    // Main function
    public static void Main()
    {
        // Provided static input
        List<long> A = new List<long> { 5, -3, 3, -1, 1 };
 
        // Call the Solve function to get the result
        var res = Solve(A);
 
        // Output the result
        Console.WriteLine($"{res.Item1} {res.Item2}");
    }
}
 
 // This code is contributed by shivamgupta0987654321


Javascript




// Function to solve the problem
function solve(A) {
    // Initialize variables to count pairs
    // with negative and positive products
    let neg = 0, pos = 0;
 
    // Variables for dynamic programming
    let dpneg = 0, dppos = 0, now = 1;
 
    // Iterate through the array
    for (let a of A) {
        // Update 'now' by multiplying
        // it with the sign of 'a'
        now *= (a < 0 ? -1 : 1);
 
        // Depending on the sign of 'now'
        if (now < 0) {
            // Increment 'dpneg'
            dpneg++;
 
            // Update 'neg' by adding 'dppos + 1'
            neg += dppos + 1;
 
            // Update 'pos' by adding 'dpneg - 1'
            pos += dpneg - 1;
        }
        else {
            // Increment 'dppos'
            dppos++;
 
            // Update 'neg' by adding 'dpneg'
            neg += dpneg;
 
            // Update 'pos' by adding 'dppos'
            pos += dppos;
        }
    }
 
    // Return the counts of pairs with
    // negative and positive products
    return [neg, pos];
}
 
// Provided static input
let n = 5;
let A = [5, -3, 3, -1, 1];
 
// Call the solve function
// to get the result
let res = solve(A);
 
// Output the result
console.log(res[0] + ' ' + res[1]);


Output

8 7

Time complexity: O(N), where N is the size of the input array.
Auxiliary Space: O(1)



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads