Count distinct Triplets with negative product
Last Updated :
13 Mar, 2023
Given an array arr[] of N integers, the task is to find count of triplets having a product less than 0.
Examples:
Input: arr[] = {-1, -2, 3, 4, 5}
Output: 6
Explanation: Triplets are {-1, 3, 4}, {-1, 4, 5},
{-1, 3, 5}, {-2, 3, 4}, {-2, 4, 5}, {-2, 3, 5}
Input: arr[] = {8, 7, 0}
Output: 0
Input: arr[] = {-1, 2, 2, 2}
Output: 3
Explanation: Three triplets can be formed {-1, 2, 2}, {-1, 2, 2} and {-1, 2, 2}
by choosing 2s from the following pair of indices {1, 2}, {2, 3}, {1, 3}.
Approach: The problem can be solved based on the following observation.
The observation is we can form a triplets having negative product, if all three elements are negative or only one element is negative.
Follow the steps to solve this problem:
- If N<3, return 0, as triplets can’t be formed.
- Else, check the positive and negative count in arr[].
- Let’s say the negative count is NegCount and the positive count is PosCount.
- If NegCount = 0, no such triplet is possible.
- Else, initialize x = 0 and y = 0.
- If PosCount > 1, we can choose any combination of two positive elements.
- If NegCount > 2, we can choose any combination of three negative elements.
- For the above calculation, use the formula of combinatorics nCr = n! / r! *(n – r)!.
- Return the summation of counts of the above two possible ways as the required answer.
Below is the implementation of the above approach.
C++
#include <bits/stdc++.h>
using namespace std;
int fact( int n)
{
if (n == 0)
return 1;
return n * fact(n - 1);
}
int nCr( int n, int r)
{
return fact(n) / (fact(r) * fact(n - r));
}
int findTriplets( int * arr, int N)
{
if (N <= 2)
return 0;
int PosCount = 0;
int NegCount = 0;
for ( int i = 0; i < N; i++) {
if (arr[i] > 0)
PosCount++;
else if (arr[i] < 0)
NegCount++;
}
if (NegCount == 0)
return 0;
int x = 0, y = 0;
if (PosCount >= 2)
x = NegCount * nCr(PosCount, 2);
if (NegCount >= 3)
y = nCr(NegCount, 3);
return x + y;
}
int main()
{
int arr[] = { -1, -2, 3, 4, 5 };
int N = sizeof (arr) / sizeof (arr[0]);
cout << findTriplets(arr, N);
return 0;
}
|
Java
import java.io.*;
class GFG {
public static int fact( int n)
{
if (n == 0 )
return 1 ;
return n * fact(n - 1 );
}
public static int nCr( int n, int r)
{
return fact(n) / (fact(r) * fact(n - r));
}
public static int findTriplets( int arr[], int N)
{
if (N <= 2 )
return 0 ;
int PosCount = 0 ;
int NegCount = 0 ;
for ( int i = 0 ; i < N; i++) {
if (arr[i] > 0 )
PosCount++;
else if (arr[i] < 0 )
NegCount++;
}
if (NegCount == 0 )
return 0 ;
int x = 0 , y = 0 ;
if (PosCount >= 2 )
x = NegCount * nCr(PosCount, 2 );
if (NegCount >= 3 )
y = nCr(NegCount, 3 );
return x + y;
}
public static void main(String[] args)
{
int arr[] = { - 1 , - 2 , 3 , 4 , 5 };
int N = arr.length;
System.out.print(findTriplets(arr, N));
}
}
|
Python3
def fact(n) :
if (n = = 0 ):
return 1
return n * fact(n - 1 )
def nCr(n, r) :
return fact(n) / / (fact(r) * fact(n - r))
def findTriplets(arr, N) :
if (N < = 2 ) :
return 0
PosCount = 0
NegCount = 0
for i in range (N):
if (arr[i] > 0 ):
PosCount + = 1
elif (arr[i] < 0 ):
NegCount + = 1
if (NegCount = = 0 ):
return 0
x = 0
y = 0
if (PosCount > = 2 ):
x = NegCount * nCr(PosCount, 2 )
if (NegCount > = 3 ):
y = nCr(NegCount, 3 )
return x + y
if __name__ = = "__main__" :
arr = [ - 1 , - 2 , 3 , 4 , 5 ]
N = len (arr)
print (findTriplets(arr, N))
|
C#
using System;
class GFG
{
public static int fact( int n)
{
if (n == 0)
return 1;
return n * fact(n - 1);
}
public static int nCr( int n, int r)
{
return fact(n) / (fact(r) * fact(n - r));
}
public static int findTriplets( int [] arr, int N)
{
if (N <= 2)
return 0;
int PosCount = 0;
int NegCount = 0;
for ( int i = 0; i < N; i++) {
if (arr[i] > 0)
PosCount++;
else if (arr[i] < 0)
NegCount++;
}
if (NegCount == 0)
return 0;
int x = 0, y = 0;
if (PosCount >= 2)
x = NegCount * nCr(PosCount, 2);
if (NegCount >= 3)
y = nCr(NegCount, 3);
return x + y;
}
public static void Main()
{
int [] arr = { -1, -2, 3, 4, 5 };
int N = arr.Length;
Console.Write(findTriplets(arr, N));
}
}
|
Javascript
function fact(no){
if (no == 0)
return 1;
return no * fact(no - 1);
}
function nCr(n, r){
return fact(n) / (fact(r) * fact(n - r));
}
function findTriplets(arr, N){
if (N <= 2)
return 0
let PosCount = 0;
let NegCount = 0;
for (i = 0; i < N; i++){
if (arr[i] > 0)
PosCount++;
else
NegCount++;
}
if (NegCount == 0)
return 0;
let x = 0;
let y = 0;
if (PosCount >= 2){
x = NegCount * nCr(PosCount, 2);
}
if (NegCount >= 3){
y = nCr(NegCount, 3);
}
return x + y;
}
let arr = [-1, -2, 3, 4, 5 ];
let N = arr.length;
console.log(findTriplets(arr, N));
|
Time Complexity: O(N^2) because of calculating nCr using factorial function.
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...