Given an array arr[] of size N (1 ≤ N ≤ 105), the task is to find the number of ways to select a 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 atmost 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 elements. 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 tripletslong 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 Codeint 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 approachimport java.util.*;class GFG{// Function to calculate// possible number of tripletsstatic 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 Codepublic 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 tripletsdef 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 Codeif __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 approachusing System;public class GFG{// Function to calculate// possible number of tripletsstatic 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 Codepublic 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 |
1
Time Complexity : O(N)
Auxiliary Space : O(1)
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.


