Count ways to obtain triplets with positive product consisting of at most one negative element | C ++ Program to implement the above approach ; Function to calculate possible number of triplets ; counting frequency of positive numbers in array ; If current array element is positive ; Increment frequency ; Select a triplet from freq elements such that i < j < k . ; Driver Code
#include <bits/stdc++.h> NEW_LINE 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 ; }