Count distinct Triplets with negative product
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: 0Input: 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++
// C++ code to implement the above approach #include <bits/stdc++.h> using namespace std; // Function to find factorial of given number int fact(int n) { if (n == 0) return 1; return n * fact(n - 1); } // Function to calculate nCr int nCr(int n, int r) { return fact(n) / (fact(r) * fact(n - r)); } // Function to return count of triplets // having negative product 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; } // Driver Code int main() { int arr[] = { -1, -2, 3, 4, 5 }; int N = sizeof(arr) / sizeof(arr[0]); // Function call cout << findTriplets(arr, N); return 0; }
Java
// Java code to implement the above approach import java.io.*; class GFG { // Function to find factorial of given number public static int fact(int n) { if (n == 0) return 1; return n * fact(n - 1); } // Function to calculate nCr public static int nCr(int n, int r) { return fact(n) / (fact(r) * fact(n - r)); } // Function to return count of triplets // having negative product 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; } // Driver Code public static void main(String[] args) { int arr[] = { -1, -2, 3, 4, 5 }; int N = arr.length; // Function call System.out.print(findTriplets(arr, N)); } } // This code is contributed by Rohit Pradhan
Python3
# Python3 code to implement the approach # Function to find factorial of given number def fact(n) : if (n == 0): return 1 return n * fact(n - 1) # Function to calculate nCr def nCr(n, r) : return fact(n) // (fact(r) * fact(n - r)) # Function to return count of triplets # having negative product 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 # Driver Code if __name__ == "__main__": arr = [ -1, -2, 3, 4, 5 ] N = len(arr) # Function call print(findTriplets(arr, N)) # This code is contributed by sanjoy_62.
C#
// C# code to implement the approach using System; class GFG { // Function to find factorial of given number public static int fact(int n) { if (n == 0) return 1; return n * fact(n - 1); } // Function to calculate nCr public static int nCr(int n, int r) { return fact(n) / (fact(r) * fact(n - r)); } // Function to return count of triplets // having negative product 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; } // Driver Code public static void Main() { int[] arr = { -1, -2, 3, 4, 5 }; int N = arr.Length; // Function call Console.Write(findTriplets(arr, N)); } } // This code is contributed by code_hunt.
Javascript
// javascript code to implement the approach // Function to find factorial of given number function fact(no){ if (no == 0) return 1; return no * fact(no - 1); } // Function to calculate nCr function nCr(n, r){ return fact(n) / (fact(r) * fact(n - r)); } // Function to return count of triplets // having negative product 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; } // Driver Code let arr = [-1, -2, 3, 4, 5 ]; let N = arr.length; // Function call console.log(findTriplets(arr, N)); // this code is contributed by ksam24000
6
Time Complexity: O(N)
Auxiliary Space: O(1)
Please Login to comment...