Count Weird Triplets
Last Updated :
26 Feb, 2024
Given an array A[] of integers. Your task is to output the count of triplets such that in each triplet, there can be at most 2 same elements.
Examples:
Input: A[] = [5, 5, 2, 5]
Output: 3
Explanation: The triplets satisfying given condition are:
- 1st Triplet: (A0, A1, A2) = (5, 5, 2). As there are at most two 5 are present, therefore it’s a valid triplet.
- 2nd Triplet: (A0, A2, A3) = (5, 2, 5). As there are at most two 5 are present, therefore it’s a valid triplet.
- 3rd Triplet: (A1, A2, A3) = (5, 2, 5). As there are at most two 5 are present, therefore it’s a valid triplet.
Since there are a total of 3 triplets that is (5, 5, 2), (5, 2, 5), and (5, 2, 5), we will return 3.
Input: A[] = {2, 4, 5}
Output: 1
Explanation: There is only one triplet satisfying given condition. Which is (2, 4, 5). Since, there can be at most 2 same element, but this triplet contains all different elements. Therefore, it’s a valid triplet.
Input: A[] = {4, 4, 4, 4}
Output: 0
Explanation: All possible combinations of triplets contain more than 2 same elements. Therefore, there are no valid triplets.
Approach: To solve the problem follow the below idea.
Check for all the possible combination of triplets and check manually that, if there are at most two same element the count that triplet else don’t consider it. At the end return the count of valid triplets.
Steps were taken to implement the approach:
- Create a variable let say Count to store the count of valid triplets.
- Run 3 nested loops, create all the possible combination of triplets and check below condition:
- If (A[i] != A[j] Or A[j] != A[k])
- Return Count.
Below is the implement of the above approach:
C++
#include <iostream>
using namespace std;
class Geek {
public :
static int checkTriplets( int A[], int n)
{
int count = 0;
for ( int i = 0; i < n - 2; i++) {
for ( int j = i + 1; j < n - 1; j++) {
for ( int k = j + 1; k < n; k++) {
if (A[i] != A[j] || A[j] != A[k]) {
count++;
}
}
}
}
return count;
}
};
int main()
{
int A[] = { 5, 5, 2, 5 };
int n = sizeof (A) / sizeof (A[0]);
Geek gfg;
cout << gfg.checkTriplets(A, n);
return 0;
}
|
Java
class GFG {
static int checkTriplets( int [] A)
{
int n = A.length;
int count = 0 ;
for ( int i = 0 ; i < n - 2 ; i++) {
for ( int j = i + 1 ; j < n - 1 ; j++) {
for ( int k = j + 1 ; k < n; k++) {
if (A[i] != A[j] || A[j] != A[k]) {
count++;
}
}
}
}
return count;
}
public static void main(String[] args)
{
int A[] = { 5 , 5 , 2 , 5 };
System.out.print(checkTriplets(A));
}
}
|
Python3
class GFG:
@staticmethod
def check_triplets(A):
n = len (A)
count = 0
for i in range (n - 2 ):
for j in range (i + 1 , n - 1 ):
for k in range (j + 1 , n):
if A[i] ! = A[j] or A[j] ! = A[k]:
count + = 1
return count
def main():
A = [ 5 , 5 , 2 , 5 ]
print (GFG.check_triplets(A))
GFG.main()
|
C#
using System;
public class GFG
{
public static int CheckTriplets( int [] A)
{
int n = A.Length;
int count = 0;
for ( int i = 0; i < n - 2; i++)
{
for ( int j = i + 1; j < n - 1; j++)
{
for ( int k = j + 1; k < n; k++)
{
if (A[i] != A[j] || A[j] != A[k])
{
count++;
}
}
}
}
return count;
}
public static void Main( string [] args)
{
int [] A = { 5, 5, 2, 5 };
Console.WriteLine(CheckTriplets(A));
}
}
|
Javascript
class Geek {
static checkTriplets(A, n) {
let count = 0;
for (let i = 0; i < n - 2; i++) {
for (let j = i + 1; j < n - 1; j++) {
for (let k = j + 1; k < n; k++) {
if (A[i] !== A[j] || A[j] !== A[k]) {
count++;
}
}
}
}
return count;
}
}
const A = [5, 5, 2, 5];
const n = A.length;
console.log(Geek.checkTriplets(A, n));
|
Time Complexity: O(N3)
Auxiliary space: O(1)
Optimal Approach: We can solve this problem optimally using below idea:
The problem can also be solved using the Combinatorics. Calculate and store frequency of each element into frequency array let say Freq[]. Then count of valid triplets = (Total possible triplets – invalid triplets).
- Where, invalid triplets are the triplets with all 3 same elements.
Steps were taken to solve the problem:
- Create a frequency array let say F[].
- Run a loop to iterate A[] and initialize frequency in F[] of each element.
- Create a variable let say Total_triplets and initialize it with (N*(N-1)*(N-2))/6
- Run a loop to iterate over F[] and follow below mentioned steps under the scope of loop:
- If (frequency of current element == 2)
- Invalid triplets (with respect to current element) = (Fi*(Fi – 1)*(Fi – 2))/6
- Total_triplets -= Invalid triplets
- return Invalid triplets.
Below is the implement of the above approach:
C++
#include <iostream>
using namespace std;
int countTriplets( int A[], int N) {
int F[1001] = {0};
for ( int i = 0; i < N; ++i) {
F[A[i]]++;
}
int totalTriplets = N * (N - 1) * (N - 2) / 6;
for ( int freq : F) {
if (freq < 2) {
continue ;
}
int allSame = freq * (freq - 1) * (freq - 2) / 6;
totalTriplets = totalTriplets - allSame;
}
return totalTriplets;
}
int main() {
int A[] = { 5, 5, 2, 5 };
int N = sizeof (A) / sizeof (A[0]);
cout << countTriplets(A, N);
return 0;
}
|
Java
class GFG {
static int CountTriplets( int [] A)
{
int N = A.length;
int [] F = new int [ 1001 ];
for ( int element : A) {
F[element]++;
}
int total_triplets = N * (N - 1 ) * (N - 2 ) / 6 ;
for ( int freq : F) {
if (freq < 2 ) {
continue ;
}
int allSame
= freq * (freq - 1 ) * (freq - 2 ) / 6 ;
total_triplets = total_triplets - (allSame);
}
return total_triplets;
}
public static void main(String[] args)
{
int A[] = { 5 , 5 , 2 , 5 };
System.out.print(CountTriplets(A));
}
}
|
Python3
def count_triplets(arr):
F = [ 0 ] * 1001
for element in arr:
F[element] + = 1
total_triplets = len (arr) * ( len (arr) - 1 ) * ( len (arr) - 2 ) / / 6
for freq in F:
if freq < 2 :
continue
all_same = freq * (freq - 1 ) * (freq - 2 ) / / 6
total_triplets - = all_same
return total_triplets
if __name__ = = "__main__" :
A = [ 5 , 5 , 2 , 5 ]
print (count_triplets(A))
|
C#
using System;
class Program
{
static int CountTriplets( int [] A, int N)
{
int [] F = new int [1001];
foreach ( int num in A)
{
F[num]++;
}
int totalTriplets = N * (N - 1) * (N - 2) / 6;
foreach ( int freq in F)
{
if (freq < 2)
{
continue ;
}
int allSame = freq * (freq - 1) * (freq - 2) / 6;
totalTriplets = totalTriplets - allSame;
}
return totalTriplets;
}
static void Main( string [] args)
{
int [] A = { 5, 5, 2, 5 };
int N = A.Length;
Console.WriteLine(CountTriplets(A, N));
}
}
|
Javascript
function countTriplets(A) {
let F = new Array(1001).fill(0);
for (let i = 0; i < A.length; ++i) {
F[A[i]]++;
}
let totalTriplets = (A.length * (A.length - 1) * (A.length - 2)) / 6;
for (let freq of F) {
if (freq < 2) {
continue ;
}
let allSame = (freq * (freq - 1) * (freq - 2)) / 6;
totalTriplets -= allSame;
}
return totalTriplets;
}
function main() {
let A = [5, 5, 2, 5];
console.log(countTriplets(A));
}
main();
|
Time Complexity: O(N)
Auxiliary Space: O(1), As frequency array Freq[] of size 1000 is used.
Share your thoughts in the comments
Please Login to comment...