Pairs whose concatenation contain all digits
Given an array of n numbers. The task is to find the number of pairs that can be taken from the given which on concatenation will contain all the digits from 0 to 9.
Examples:
Input : num[][] = { “129300455”, “5559948277”, “012334556”, “56789”, “123456879” }
Output : 5
{“129300455”, “56789”}, { “129300455”, “123456879”}, {“5559948277”, “012334556”},
{“012334556”, “56789”}, {“012334556”, “123456879”} are the pair which contain all the digits from 0 to 9 on concatenation.
Note: The number of the digit in each of the numbers can be 10^6.
The idea is to represent each number as the mask of 10 bits such that if it contains digit i at least once then ith bit will be set in the mask.
For example,
let n = 4556120 then 0th, 1st, 2nd, 4th, 5th, 6th bits will be set in the mask.
Thus, mask = (0001110111)2 = (119)10
Now, for every mask m from 0 to 2^10 – 1, we will store the count of the number of numbers having the mask of their number equals to m.
So, we will make an array, say cnt[], where cnt[i] stores the count of the number of numbers whose mask is equal to i. Pseudocode for this:
for (i = 0; i < (1 << 10); i++)
cnt[i] = 0;
for (i = 1; i <= n; i++)
{
string x = p[i];
int mask = 0;
for (j = 0; j < x.size(); j++)
mask |= (1 << (x[j] - '0';);
cnt[mask]++;
}
A pair of numbers will have all the digit from 0 to 9 if every bit from 0 to 9 is set in the bitwise OR of maskof both the number, i.e if it’s equal to (1111111111)2</sub) = (1023)10
Now, we will iterate over all pairs of masks whose bitwise OR is equal to 1023 and add a number of ways.
Below is the implementation of this approach:
C++
#include <bits/stdc++.h>
using namespace std;
#define N 20
int countPair( char str[N][N], int n)
{
int cnt[1 << 10] = { 0 };
for ( int i = 0; i < n; i++) {
int mask = 0;
for ( int j = 0; str[i][j] != '\0' ; ++j)
mask |= (1 << (str[i][j] - '0' ));
cnt[mask]++;
}
int ans = 0;
for ( int m1 = 0; m1 <= 1023; m1++)
for ( int m2 = 0; m2 <= 1023; m2++)
if ((m1 | m2) == 1023) {
ans += ((m1 == m2) ?
(cnt[m1] * (cnt[m1] - 1)) :
(cnt[m1] * cnt[m2]));
}
return ans / 2;
}
int main()
{
int n = 5;
char str[][N] = { "129300455" , "5559948277" ,
"012334556" , "56789" , "123456879" };
cout << countPair(str, n) << endl;
return 0;
}
|
Java
import java.io.*;
public class GFG
{
static final int N = 20 ;
static int countPair( char str[][], int n)
{
int [] cnt = new int [ 1 << 10 ];
for ( int i = 0 ; i < n; i++)
{
int mask = 0 ;
for ( int j = 0 ; j < str[i].length; ++j)
mask |= ( 1 << (str[i][j] - '0' ));
cnt[mask]++;
}
int ans = 0 ;
for ( int m1 = 0 ; m1 <= 1023 ; m1++)
for ( int m2 = 0 ; m2 <= 1023 ; m2++)
if ((m1 | m2) == 1023 )
{
ans += ((m1 == m2) ? (cnt[m1] * (cnt[m1] - 1 )) :
(cnt[m1] * cnt[m2]));
}
return ans / 2 ;
}
public static void main(String[] args)
{
int n = 5 ;
char str[][] = { "129300455" .toCharArray(),
"5559948277" .toCharArray(),
"012334556" .toCharArray(),
"56789" .toCharArray(),
"123456879" .toCharArray() };
System.out.print(countPair(str, n) + "\n" );
}
}
|
Python3
N = 20
def countPair(st, n):
cnt = [ 0 ] * ( 1 << 10 )
for i in range (n):
mask = 0
for j in range ( len (st[i])):
mask | = ( 1 << ( ord (st[i][j]) - ord ( '0' )))
cnt[mask] + = 1
ans = 0
for m1 in range ( 1024 ):
for m2 in range ( 1024 ):
if ((m1 | m2) = = 1023 ):
if (m1 = = m2):
ans + = (cnt[m1] * (cnt[m1] - 1 ))
else :
ans + = (cnt[m1] * cnt[m2])
return ans / / 2
if __name__ = = "__main__" :
n = 5
st = [ "129300455" , "5559948277" ,
"012334556" , "56789" , "123456879" ]
print (countPair(st, n))
|
C#
using System;
class GFG
{
static readonly int N = 20;
static int countPair(String []str, int n)
{
int [] cnt = new int [1 << 10];
for ( int i = 0; i < n; i++)
{
int mask = 0;
for ( int j = 0; j < str[i].Length; ++j)
mask |= (1 << (str[i][j] - '0' ));
cnt[mask]++;
}
int ans = 0;
for ( int m1 = 0; m1 <= 1023; m1++)
for ( int m2 = 0; m2 <= 1023; m2++)
if ((m1 | m2) == 1023)
{
ans += ((m1 == m2) ? (cnt[m1] * (cnt[m1] - 1)) :
(cnt[m1] * cnt[m2]));
}
return ans / 2;
}
public static void Main(String[] args)
{
int n = 5;
String []str = { "129300455" ,
"5559948277" ,
"012334556" ,
"56789" ,
"123456879" };
Console.Write(countPair(str, n) + "\n" );
}
}
|
Javascript
<script>
let N = 20;
function countPair(str,n)
{
let cnt = new Array(1 << 10);
for (let i=0;i<cnt.length;i++)
{
cnt[i]=0;
}
for (let i = 0; i < n; i++)
{
let mask = 0;
for (let j = 0; j < str[i].length; ++j)
mask |= (1 << (str[i][j] - '0' ));
cnt[mask]++;
}
let ans = 0;
for (let m1 = 0; m1 <= 1023; m1++)
for (let m2 = 0; m2 <= 1023; m2++)
if ((m1 | m2) == 1023)
{
ans += ((m1 == m2) ? (cnt[m1] * (cnt[m1] - 1)) :
(cnt[m1] * cnt[m2]));
}
return Math.floor(ans / 2);
}
let n = 5;
let st = [ "129300455" , "5559948277" ,
"012334556" , "56789" , "123456879" ];
document.write(countPair(st, n))
</script>
|
Complexity : O(n + 2^10 * 2^10)
Last Updated :
16 Dec, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...