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++
// C++ code for the above approach
#include <bits/stdc++.h>
using namespace std;
void solve(vector<vector<int> >& details, int N)
{
// Vector to store the pairs with
// common values
vector<int> adj_a[N + 1], adj_b[N + 1];
for (int i = 0; i < N; i++) {
// Storing pairs with same first values
adj_a[details[i][0]].push_back(details[i][1]);
// Storing pairs with same second values
adj_b[details[i][1]].push_back(details[i][0]);
}
// Total ways to choose a triplet from
// n pairs is nc3
int ans = (N * (N - 1) * (N - 2)) / 6;
for (int i = 1; i < N + 1; i++) {
// Size to keep record of the elements
// with same first values
int size = adj_a[i].size();
for (auto& u : adj_a[i]) {
// Count of pairs that forms
// invalid triplets
int same_cnt = adj_b[u].size() - 1;
// Subtracting the invalid values
ans -= (size - 1) * same_cnt;
}
}
// Printing the answer
cout << ans << endl;
}
// Driver's code
int main()
{
// Given input
int n = 5;
vector<vector<int> > details{
{ 1, 4 }, { 2, 4 }, { 3, 3 }, { 4, 2 }, { 1, 2 }
};
// Function Call
solve(details, n);
return 0;
}
Java
// Java code for the above approach
import java.io.*;
import java.util.*;
class GFG {
@SuppressWarnings("unchecked")
public static void solve(int details[][], int N)
{
// ArrayList to store the pairs with
// common values
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>();
}
// ArrayList<ArrayList<Integer>> adj_a=new
// ArrayList<ArrayList<Integer>>(N+1);
// ArrayList<ArrayList<Integer>> adj_b=new
// ArrayList<ArrayList<Integer>>(N+1);
for (int i = 0; i < N; i++) {
// Storing pairs with same first values
adj_a[details[i][0]].add(details[i][1]);
// Storing pairs with same second values
adj_b[details[i][1]].add(details[i][0]);
}
// Total ways to choose a triplet from
// n pairs is nc3
int ans = (N * (N - 1) * (N - 2)) / 6;
for (int i = 1; i < N + 1; i++) {
// Size to keep record of the elements
// with same first values
int size = adj_a[i].size();
for (Integer u : adj_a[i]) {
// Count of pairs that forms
// invalid triplets
int same_cnt = adj_b[u].size() - 1;
// Subtracting the invalid values
ans -= (size - 1) * same_cnt;
}
}
// Printing the answer
System.out.println(ans);
}
// Driver Code
public static void main(String[] args)
{
// Given input
int n = 5;
int details[][] = {
{ 1, 4 }, { 2, 4 }, { 3, 3 }, { 4, 2 }, { 1, 2 }
};
// Function Call
solve(details, n);
}
}
// This code is contributed by Rohit Pradhan
Python3
# Python code for the above approach
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):
# Storing pairs with same first values
adj_a[details[i][0]].append(details[i][1])
# Storing pairs with same second values
adj_b[details[i][1]].append(details[i][0])
# Total ways to choose a triplet from n pairs is nC3
ans = (N * (N-1)*(N-2))/6
for i in range(1, N+1):
# Size to keep record of the elements with same first values
size = len(adj_a[i])
for u in adj_a[i]:
# Count of pairs that forms invalid triplets
same_cnt = len(adj_b[u]) - 1
# Subtracting the invalid values
ans -= (size-1) * same_cnt
# Printing the answer
print(int(ans))
n = 5
details = [[1, 4],
[2, 4],
[3, 3],
[4, 2],
[1, 2]]
# Function call
solve(details, n)
# This code is contributed by lokeshmvs21.
C#
using System;
using System.Collections.Generic;
public class GFG {
public static void solve(int[, ] details, int N)
{
// ArrayList to store the pairs with
// common values
/*List<int>[] adj_a = new List[N + 1];
List<int>[] adj_b = new List[N + 1];*/
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++) {
// Storing pairs with same first values
adj_a[details[i, 0]].Add(details[i, 1]);
// Storing pairs with same second values
adj_b[details[i, 1]].Add(details[i, 0]);
}
// Total ways to choose a triplet from
// n pairs is nc3
int ans = (N * (N - 1) * (N - 2)) / 6;
for (int i = 1; i < N + 1; i++) {
// Size to keep record of the elements
// with same first values
int size = adj_a[i].Count;
for (int u = 0; u < adj_a[i].Count; u++) {
// Count of pairs that forms
// invalid triplets
int same_cnt = adj_b[adj_a[i][u]].Count - 1;
// Subtracting the invalid values
ans -= (size - 1) * same_cnt;
}
}
// Printing the answer
Console.WriteLine(ans);
}
static public void Main()
{
// Given input
int n = 5;
int[, ] details = {
{ 1, 4 }, { 2, 4 }, { 3, 3 }, { 4, 2 }, { 1, 2 }
};
// Function Call
solve(details, n);
}
}
// This code is contributed by akashish__
JavaScript
// JavaScript code for the above approach
function solve(details, N)
{
// Vector to store the pairs with
// common values
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++) {
// Storing pairs with same first values
adj_a[details[i][0]].push(details[i][1]);
// Storing pairs with same second values
adj_b[details[i][1]].push(details[i][0]);
}
// Total ways to choose a triplet from
// n pairs is nc3
let ans = (N * (N - 1) * (N - 2)) / 6;
for (let i = 1; i < N + 1; i++) {
// Size to keep record of the elements
// with same first values
let size = adj_a[i].length;
for (let u of adj_a[i]) {
// Count of pairs that forms
// invalid triplets
let same_cnt = adj_b[u].length - 1;
// Subtracting the invalid values
ans -= (size - 1) * same_cnt;
}
}
// Printing the answer
console.log(ans);
}
// Driver's code
// Given input
let n = 5;
let details = [
[1, 4],
[2, 4],
[3, 3],
[4, 2],
[1, 2],
];
// Function Call
solve(details, n);
// This code is contributed by Ishankhandelwals.
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
Similar Reads
Inclusion Exclusion principle for Competitive Programming What is the Inclusion-Exclusion Principle?The inclusion-exclusion principle is a combinatoric way of computing the size of multiple intersecting sets or the probability of complex overlapping events. Generalised Inclusion-Exclusion over Set:For 2 Intersecting Set A and B: A\bigcup B= A + B - A\bigca
5 min read
Prefix Sum of Matrix (Or 2D Array) Given a matrix (or 2D array) a[][] of integers, find the prefix sum matrix for it. Let prefix sum matrix be psa[][]. The value of psa[i][j] contains the sum of all values which are above it or on the left of it. Recommended PracticePrefix Sum of Matrix (Or 2D Array)Try It! Prerequisite: Prefix Sum -
12 min read
Count ways to create string of size N with given restrictions Given a number N, the task is to count the number of ways to create a string of size N (only with capital alphabets) such that no vowel is between two consonants and the string does not start with the letter 'A' and does not end with the letter 'Z'. (Print the answer modulo 109 + 7). Examples: Input
15 min read
Count ways of selecting X red balls and Y blue balls Given integers A, B, C, and D, There are two boxes First Box has A red balls and B blue balls and the second box has C red balls and D blue balls, the task is to count ways to select 3 red balls and 3 blue balls so that there are 3 balls drawn from the first box and 3 balls drawn from the second box
15+ min read
Count ways choosing K balls from any given A boxes Given integers A and K, there are A boxes the first box contains K balls, the Second box contains K + 1 balls, the Third Box contains K + 3 balls so on till the A'th box contains K + A balls, the task for this problem is to count the number of ways of picking K balls from any of the boxes. (Print an
9 min read
Count number of ways in which following people can be arranged Given integers X and Y representing X girls and Y boys, the task is to count the number of ways arranging X girls and Y boys such that girls always stay together and two boys from Y refuse to stay consecutive. Print the answer modulo 109 + 7. Examples: Input: X = 2, Y = 2Output: 4Explanation: Let's
6 min read
Count ways to choose Triplets of Pairs such that either first or second values are distinct 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 dis
7 min read