Count ways to obtain triplets with positive product consisting of at most one negative element
Last Updated :
27 Apr, 2021
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++
#include <bits/stdc++.h>
using namespace std;
long long int possibleTriplets( int arr[], int N)
{
int freq = 0;
for ( int i = 0; i < N; i++) {
if (arr[i] > 0) {
freq++;
}
}
return (freq * 1LL * (freq - 1)
* (freq - 2))
/ 6;
}
int main()
{
int arr[] = { 2, 5, -9, -3, 6 };
int N = sizeof (arr) / sizeof (arr[0]);
cout << possibleTriplets(arr, N);
return 0;
}
|
Java
import java.util.*;
class GFG
{
static int possibleTriplets( int arr[], int N)
{
int freq = 0 ;
for ( int i = 0 ; i < N; i++)
{
if (arr[i] > 0 )
{
freq++;
}
}
return ( int ) ((freq * 1L * (freq - 1 )
* (freq - 2 ))
/ 6 );
}
public static void main(String[] args)
{
int arr[] = { 2 , 5 , - 9 , - 3 , 6 };
int N = arr.length;
System.out.print(possibleTriplets(arr, N));
}
}
|
Python3
def possibleTriplets(arr, N):
freq = 0
for i in range (N):
if (arr[i] > 0 ):
freq + = 1
return (freq * (freq - 1 ) * (freq - 2 )) / / 6
if __name__ = = '__main__' :
arr = [ 2 , 5 , - 9 , - 3 , 6 ]
N = len (arr)
print (possibleTriplets(arr, N))
|
C#
using System;
public class GFG
{
static int possibleTriplets( int []arr, int N)
{
int freq = 0;
for ( int i = 0; i < N; i++)
{
if (arr[i] > 0)
{
freq++;
}
}
return ( int ) ((freq * 1L * (freq - 1)
* (freq - 2)) / 6);
}
public static void Main(String[] args)
{
int []arr = { 2, 5, -9, -3, 6 };
int N = arr.Length;
Console.Write(possibleTriplets(arr, N));
}
}
|
Javascript
<script>
function possibleTriplets(arr, N)
{
var freq = 0;
for ( var i = 0; i < N; i++) {
if (arr[i] > 0) {
freq++;
}
}
return (freq * 1 * (freq - 1)
* (freq - 2))
/ 6;
}
var arr = [ 2, 5, -9, -3, 6 ];
var N = arr.length;
document.write( possibleTriplets(arr, N));
</script>
|
Time Complexity : O(N)
Auxiliary Space : O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...