Count ways to choose Triplets of Pairs such that either first or second values are distinct
Last Updated :
23 Jan, 2023
Given an array of pairs arr[] of size N (N ? 3) where each element of pair is at most N and each pair is unique, the task is to determine the number of ways to select triplets from the given N pairs that satisfy at least one of the following conditions:
- The first value (a) of each pair should be distinct.
- The second value (b) of each pair should be distinct.
Examples:
Input: N = 4, arr[] = {{2, 4}, {3, 4}, {2, 1}, {1, 4}}
Output: 2
Explanation: The valid triplets are {{2, 4}. {3, 4}, {1, 4}}
and {{3, 4}, {2, 1}, {1, 4}}
Input: N = 5, arr[] = {{1, 4}, {2, 4}, {3, 3}, {4, 2}, {1, 2}}
Output: 8
Approach: Use the below idea to solve the problem
The total number of triplets possible is n*(n – 1)*(n – 2)/6. So we can count the number of invalid pairs for the triplets and subtract that count from the total triplets possible
Follow the below steps to solve the problem:
- Create two 2D vectors to keep a record of the pairs in the given array.
- Store pairs with the same first value in the first 2D vector and pairs with the same second value in the second 2D vector.
- Initialize ans = N*(N – 1)*(N – 2)/6, the count of total pairs possible.
- Traverse over first 2D vector and calculate the number of triplets that are not possible with each value in the given array.
- Subtract the count of invalid pairs from the ans.
- Return ans as the final answer.
Below is the implementation of the ab
C++
#include <bits/stdc++.h>
using namespace std;
void solve(vector<vector< int > >& details, int N)
{
vector< int > adj_a[N + 1], adj_b[N + 1];
for ( int i = 0; i < N; i++) {
adj_a[details[i][0]].push_back(details[i][1]);
adj_b[details[i][1]].push_back(details[i][0]);
}
int ans = (N * (N - 1) * (N - 2)) / 6;
for ( int i = 1; i < N + 1; i++) {
int size = adj_a[i].size();
for ( auto & u : adj_a[i]) {
int same_cnt = adj_b[u].size() - 1;
ans -= (size - 1) * same_cnt;
}
}
cout << ans << endl;
}
int main()
{
int n = 5;
vector<vector< int > > details{
{ 1, 4 }, { 2, 4 }, { 3, 3 }, { 4, 2 }, { 1, 2 }
};
solve(details, n);
return 0;
}
|
Java
import java.io.*;
import java.util.*;
class GFG {
@SuppressWarnings ( "unchecked" )
public static void solve( int details[][], int N)
{
ArrayList<Integer>[] adj_a = new ArrayList[N + 1 ];
ArrayList<Integer>[] adj_b = new ArrayList[N + 1 ];
for ( int i = 0 ; i < N + 1 ; i++) {
adj_a[i] = new ArrayList<Integer>();
adj_b[i] = new ArrayList<Integer>();
}
for ( int i = 0 ; i < N; i++) {
adj_a[details[i][ 0 ]].add(details[i][ 1 ]);
adj_b[details[i][ 1 ]].add(details[i][ 0 ]);
}
int ans = (N * (N - 1 ) * (N - 2 )) / 6 ;
for ( int i = 1 ; i < N + 1 ; i++) {
int size = adj_a[i].size();
for (Integer u : adj_a[i]) {
int same_cnt = adj_b[u].size() - 1 ;
ans -= (size - 1 ) * same_cnt;
}
}
System.out.println(ans);
}
public static void main(String[] args)
{
int n = 5 ;
int details[][] = {
{ 1 , 4 }, { 2 , 4 }, { 3 , 3 }, { 4 , 2 }, { 1 , 2 }
};
solve(details, n);
}
}
|
Python3
def solve(details, N):
adj_a = [ 0 ] * (N + 1 )
adj_b = [ 0 ] * (N + 1 )
for i in range (N + 1 ):
adj_a[i] = []
adj_b[i] = []
for i in range (N):
adj_a[details[i][ 0 ]].append(details[i][ 1 ])
adj_b[details[i][ 1 ]].append(details[i][ 0 ])
ans = (N * (N - 1 ) * (N - 2 )) / 6
for i in range ( 1 , N + 1 ):
size = len (adj_a[i])
for u in adj_a[i]:
same_cnt = len (adj_b[u]) - 1
ans - = (size - 1 ) * same_cnt
print ( int (ans))
n = 5
details = [[ 1 , 4 ],
[ 2 , 4 ],
[ 3 , 3 ],
[ 4 , 2 ],
[ 1 , 2 ]]
solve(details, n)
|
C#
using System;
using System.Collections.Generic;
public class GFG {
public static void solve( int [, ] details, int N)
{
List< int >[] adj_a = new List< int >[ N + 1 ];
List< int >[] adj_b = new List< int >[ N + 1 ];
for ( int i = 0; i < N + 1; i++) {
adj_a[i] = new List< int >();
adj_b[i] = new List< int >();
}
for ( int i = 0; i < N; i++) {
adj_a[details[i, 0]].Add(details[i, 1]);
adj_b[details[i, 1]].Add(details[i, 0]);
}
int ans = (N * (N - 1) * (N - 2)) / 6;
for ( int i = 1; i < N + 1; i++) {
int size = adj_a[i].Count;
for ( int u = 0; u < adj_a[i].Count; u++) {
int same_cnt = adj_b[adj_a[i][u]].Count - 1;
ans -= (size - 1) * same_cnt;
}
}
Console.WriteLine(ans);
}
static public void Main()
{
int n = 5;
int [, ] details = {
{ 1, 4 }, { 2, 4 }, { 3, 3 }, { 4, 2 }, { 1, 2 }
};
solve(details, n);
}
}
|
Javascript
function solve(details, N)
{
let adj_a = new Array(N + 1);
let adj_b = new Array(N + 1);
for (let i = 0; i <= N; i++) {
adj_a[i] = [];
adj_b[i] = [];
}
for (let i = 0; i < N; i++) {
adj_a[details[i][0]].push(details[i][1]);
adj_b[details[i][1]].push(details[i][0]);
}
let ans = (N * (N - 1) * (N - 2)) / 6;
for (let i = 1; i < N + 1; i++) {
let size = adj_a[i].length;
for (let u of adj_a[i]) {
let same_cnt = adj_b[u].length - 1;
ans -= (size - 1) * same_cnt;
}
}
console.log(ans);
}
let n = 5;
let details = [
[1, 4],
[2, 4],
[3, 3],
[4, 2],
[1, 2],
];
solve(details, n);
|
above approach:
Time Complexity: O(N), the loop run once for every first value.
Auxiliary Space: O(N), where N is the size of the adjacency list
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...