Open In App
Related Articles

Count ways to obtain triplets with positive product consisting of at most one negative element

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Share
Report issue
Report

Given an array arr[] of size N (1 ? N ? 105), the task is to find the number of ways to select triplet i, j, and k such that i < j < k and the product arr[i] * arr[j] * arr[k] is positive. 
Note: Each triplet can consist of at most one negative element.

Examples:  

Input: arr[] = {2, 5, -9, -3, 6} 
Output: 1 
Explanation: The total number ways to obtain a triplet i, j and k to satisfy given conditions is 1 {0, 1, 4}.

Input : arr[] = {2, 5, 6, -2, 5} 
Output : 4 
Explanation: The total number ways to obtain a triplet i, j and k to satisfy given conditions are 4 {0, 1, 2}, {0, 1, 4}, {1, 2, 4} and {0, 2, 4}.

Approach: All possible combinations of a triplet are as follows:
 

  • # negative elements or 2 negative elements and 1 positive element. Both these combinations cannot be considered as the maximum allowed negative elements in a triplet is 1.
  • 2 negative (-ve) elements and 1 positive (+ve) element. Since the product of the triplet will be negative, the triplet cannot be considered.
  • 3 positive elements.

Follow the steps below to solve the problem: 

  • Traverse the array and count frequency of positive array elements, say freq
  • Count of ways to select a valid triplet from freq number of array elements using the formula PnC = NC3 = (N * (N – 1) * (N – 2)) / 6. Add the count obtained to the answer. 
  • Print the count obtained.

Below is the implementation of the above approach:
 

C++

// C++ Program to implement
// the above approach
 
#include <bits/stdc++.h>
using namespace std;
 
// Function to calculate
// possible number of triplets
long long int possibleTriplets(int arr[], int N)
{
    int freq = 0;
    // counting frequency of positive numbers
    // in array
 
    for (int i = 0; i < N; i++) {
 
        // If current array
        // element is positive
        if (arr[i] > 0) {
 
            // Increment frequency
            freq++;
        }
    }
 
    // Select a triplet from freq
    // elements such that i < j < k.
    return (freq * 1LL * (freq - 1)
            * (freq - 2))
           / 6;
}
 
// Driver Code
int main()
{
    int arr[] = { 2, 5, -9, -3, 6 };
    int N = sizeof(arr) / sizeof(arr[0]);
 
    cout << possibleTriplets(arr, N);
 
    return 0;
}

                    

Java

// Java Program to implement
// the above approach
import java.util.*;
class GFG
{
 
// Function to calculate
// possible number of triplets
static int possibleTriplets(int arr[], int N)
{
    int freq = 0;
   
    // counting frequency of positive numbers
    // in array
    for (int i = 0; i < N; i++)
    {
 
        // If current array
        // element is positive
        if (arr[i] > 0)
        {
 
            // Increment frequency
            freq++;
        }
    }
 
    // Select a triplet from freq
    // elements such that i < j < k.
    return (int) ((freq * 1L * (freq - 1)
            * (freq - 2))
           / 6);
}
 
// Driver Code
public static void main(String[] args)
{
    int arr[] = { 2, 5, -9, -3, 6 };
    int N = arr.length;
    System.out.print(possibleTriplets(arr, N));
}
}
 
// This code is contributed by 29AjayKumar

                    

Python3

# Python3 Program to implement
# the above approach
 
# Function to calculate
# possible number of triplets
def possibleTriplets(arr, N):
    freq = 0
     
    # counting frequency of positive numbers
    # in array
    for i in range(N):
 
        # If current array
        # element is positive
        if (arr[i] > 0):
 
            # Increment frequency
            freq += 1
 
    # Select a triplet from freq
    # elements such that i < j < k.
    return (freq * (freq - 1) * (freq - 2)) // 6
 
# Driver Code
if __name__ == '__main__':
    arr = [2, 5, -9, -3, 6]
    N = len(arr)
 
    print(possibleTriplets(arr, N))
 
    # This code is contributed by mohit kumar 29

                    

C#

// C# Program to implement
// the above approach
using System;
 
public class GFG
{
 
// Function to calculate
// possible number of triplets
static int possibleTriplets(int []arr, int N)
{
    int freq = 0;
   
    // counting frequency of positive numbers
    // in array
    for (int i = 0; i < N; i++)
    {
 
        // If current array
        // element is positive
        if (arr[i] > 0)
        {
 
            // Increment frequency
            freq++;
        }
    }
 
    // Select a triplet from freq
    // elements such that i < j < k.
    return (int) ((freq * 1L * (freq - 1)
            * (freq - 2)) / 6);
}
 
// Driver Code
public static void Main(String[] args)
{
    int []arr = { 2, 5, -9, -3, 6 };
    int N = arr.Length;
    Console.Write(possibleTriplets(arr, N));
}
}
 
// This code is contributed by 29AjayKumar

                    

Javascript

<script>
 
// Javascript Program to implement
// the above approach
 
// Function to calculate
// possible number of triplets
function possibleTriplets(arr, N)
{
    var freq = 0;
    // counting frequency of positive numbers
    // in array
 
    for (var i = 0; i < N; i++) {
 
        // If current array
        // element is positive
        if (arr[i] > 0) {
 
            // Increment frequency
            freq++;
        }
    }
 
    // Select a triplet from freq
    // elements such that i < j < k.
    return (freq * 1 * (freq - 1)
            * (freq - 2))
        / 6;
}
 
// Driver Code
var arr = [ 2, 5, -9, -3, 6 ];
var N = arr.length;
document.write( possibleTriplets(arr, N));
 
</script>

                    

Output: 
1

 

Time Complexity : O(N) 
Auxiliary Space : O(1)



Last Updated : 27 Apr, 2021
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads
Complete Tutorials