Count array elements whose all distinct digits appear in K
Given an array arr[] consisting of N positive integers and a positive integer K, the task is to find the count of array elements whose distinct digits are a subset of the digits of K.
Examples:
Input: arr[] = { 1, 12, 1222, 13, 2 }, K = 12
Output: 4
Explanation:
Distinct Digits of K are { 1, 2 }
Distinct Digits of arr[0] are { 1 }, which is the subset of the digits of K.
Distinct Digits of arr[1] are { 1, 2 }, which is the subset of the digits of K.
Distinct Digits of arr[2] are { 1, 2 }, which is the subset of the digits of K.
Distinct Digits of arr[3] are { 1, 3 }, which is not the subset of the digits of K.
Distinct Digits of arr[4] are { 2 }, which is the subset of the digits of K.
Therefore, the required output is 4.
Input: arr = {1, 2, 3, 4, 1234}, K = 1234
Output: 5
Naive Approach: The simplest approach to solve the problem is to traverse the array arr[] and for each array element, check if all its distinct digits appear in K or not. If found to be true, then increment the count. Finally, print the count obtained.
Below is the implementation of the above approach:
C++14
#include <bits/stdc++.h>
using namespace std;
static bool isValidDigit( int digit, int K)
{
while (K != 0)
{
if (K % 10 == digit)
{
return true ;
}
K = K / 10;
}
return false ;
}
int noOfValidNumbers( int K, int arr[], int n)
{
int count = 0;
for ( int i = 0; i < n; i++)
{
int no = arr[i];
bool flag = true ;
while (no != 0)
{
int digit = no % 10;
if (!isValidDigit(digit, K))
{
flag = false ;
break ;
}
no = no / 10;
}
if (flag == true )
{
count++;
}
}
return count;
}
int main()
{
int K = 12;
int arr[] = { 1, 12, 1222, 13, 2 };
int n = sizeof (arr) / sizeof (arr[0]);
cout << noOfValidNumbers(K, arr, n);
return 0;
}
|
Java
import java.io.*;
class GFG {
static boolean isValidDigit( int digit, int K)
{
while (K != 0 ) {
if (K % 10 == digit) {
return true ;
}
K = K / 10 ;
}
return false ;
}
static int noOfValidNumbers( int K, int arr[])
{
int count = 0 ;
for ( int i = 0 ; i < arr.length; i++) {
int no = arr[i];
boolean flag = true ;
while (no != 0 ) {
int digit = no % 10 ;
if (!isValidDigit(digit, K)) {
flag = false ;
break ;
}
no = no / 10 ;
}
if (flag == true ) {
count++;
}
}
return count;
}
public static void main(String[] args)
{
int K = 12 ;
int arr[] = { 1 , 12 , 1222 , 13 , 2 };
System.out.println(noOfValidNumbers(K, arr));
}
}
|
Python3
def isValidDigit(digit, K):
while (K ! = 0 ):
if (K % 10 = = digit):
return True
K = K / / 10
return False
def noOfValidNumbers(K, arr, n):
count = 0
for i in range (n):
no = arr[i]
flag = True
while (no ! = 0 ):
digit = no % 10
if ( not isValidDigit(digit, K)):
flag = False
break
no = no / / 10
if (flag = = True ):
count + = 1
return count
if __name__ = = "__main__" :
K = 12
arr = [ 1 , 12 , 1222 , 13 , 2 ]
n = len (arr)
print (noOfValidNumbers(K, arr, n))
|
C#
using System;
class GFG{
static bool isValidDigit( int digit, int K)
{
while (K != 0)
{
if (K % 10 == digit)
{
return true ;
}
K = K / 10;
}
return false ;
}
static int noOfValidNumbers( int K, int []arr)
{
int count = 0;
for ( int i = 0; i < arr.Length; i++)
{
int no = arr[i];
bool flag = true ;
while (no != 0)
{
int digit = no % 10;
if (!isValidDigit(digit, K))
{
flag = false ;
break ;
}
no = no / 10;
}
if (flag == true )
{
count++;
}
}
return count;
}
public static void Main(String[] args)
{
int K = 12;
int []arr = { 1, 12, 1222, 13, 2 };
Console.WriteLine(noOfValidNumbers(K, arr));
}
}
|
Javascript
<script>
function isValidDigit(digit, K)
{
while (K != 0)
{
if (K % 10 == digit)
{
return true ;
}
K = parseInt(K / 10);
}
return false ;
}
function noOfValidNumbers(K, arr, n)
{
let count = 0;
for (let i = 0; i < n; i++)
{
let no = arr[i];
let flag = true ;
while (no != 0)
{
let digit = no % 10;
if (!isValidDigit(digit, K))
{
flag = false ;
break ;
}
no = parseInt(no / 10);
}
if (flag == true )
{
count++;
}
}
return count;
}
let K = 12;
let arr = [ 1, 12, 1222, 13, 2 ];
let n = arr.length;
document.write(noOfValidNumbers(K, arr, n));
</script>
|
Time Complexity: O(N * log10(K) * log10(Max)), Max is the largest array element
Auxiliary Space: O(1)
Efficient Approach: The above approach can be optimized using a HashSet. Follow the steps below to solve the problem:
- Iterate over the digits of K and insert all the digits into a HashSet say set
- Traverse the array arr[] and for every array element, iterate over all the digits of the current element and check if all the digits are present in the set or not. If found to be true, then increment the count.
- Finally, print the count obtained.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
static int noOfValidKbers( int K, vector< int > arr)
{
map< int , int > set;
while (K != 0)
{
set[K % 10] = 1;
K = K / 10;
}
int count = 0;
for ( int i = 0; i < arr.size(); i++)
{
int no = arr[i];
bool flag = true ;
while (no != 0)
{
int digit = no % 10;
if (set.find(digit)==set.end())
{
flag = false ;
break ;
}
no = no / 10;
}
if (flag == true )
{
count++;
}
}
return count;
}
int main()
{
int K = 12;
vector< int > arr = { 1, 12, 1222, 13, 2 };
cout<<(noOfValidKbers(K, arr));
}
|
Java
import java.io.*;
import java.util.*;
class GFG {
static int noOfValidKbers( int K, int arr[])
{
HashSet<Integer> set = new HashSet<>();
while (K != 0 ) {
set.add(K % 10 );
K = K / 10 ;
}
int count = 0 ;
for ( int i = 0 ; i < arr.length; i++) {
int no = arr[i];
boolean flag = true ;
while (no != 0 ) {
int digit = no % 10 ;
if (!set.contains(digit)) {
flag = false ;
break ;
}
no = no / 10 ;
}
if (flag == true ) {
count++;
}
}
return count;
}
public static void main(String[] args)
{
int K = 12 ;
int arr[] = { 1 , 12 , 1222 , 13 , 2 };
System.out.println(noOfValidKbers(K, arr));
}
}
|
Python3
def noOfValidKbers(K, arr):
st = {}
while (K ! = 0 ):
if (K % 10 in st):
st[K % 10 ] = 1
else :
st[K % 10 ] = st.get(K % 10 , 0 ) + 1
K = K / / 10
count = 0
for i in range ( len (arr)):
no = arr[i]
flag = True
while (no ! = 0 ):
digit = no % 10
if (digit not in st):
flag = False
break
no = no / / 10
if (flag = = True ):
count + = 1
return count
if __name__ = = '__main__' :
K = 12
arr = [ 1 , 12 , 1222 , 13 , 2 ]
print (noOfValidKbers(K, arr))
|
C#
using System;
using System.Collections.Generic;
class GFG{
static int noOfValidKbers( int K, int []arr)
{
HashSet< int > set = new HashSet< int >();
while (K != 0)
{
set .Add(K % 10);
K = K / 10;
}
int count = 0;
for ( int i = 0; i < arr.Length; i++)
{
int no = arr[i];
bool flag = true ;
while (no != 0)
{
int digit = no % 10;
if (! set .Contains(digit))
{
flag = false ;
break ;
}
no = no / 10;
}
if (flag == true )
{
count++;
}
}
return count;
}
public static void Main(String[] args)
{
int K = 12;
int []arr = { 1, 12, 1222, 13, 2 };
Console.WriteLine(noOfValidKbers(K, arr));
}
}
|
Javascript
<script>
function noOfValidKbers(K , arr) {
var set = new Set();
while (K != 0) {
set.add(K % 10);
K = parseInt(K / 10);
}
var count = 0;
for (i = 0; i < arr.length; i++) {
var no = arr[i];
var flag = true ;
while (no != 0) {
var digit = no % 10;
if (!set.has(digit)) {
flag = false ;
break ;
}
no = parseInt(no / 10);
}
if (flag == true ) {
count++;
}
}
return count;
}
var K = 12;
var arr = [ 1, 12, 1222, 13, 2 ];
document.write(noOfValidKbers(K, arr));
</script>
|
Time Complexity: O(N * log10(Max)), where Max is the largest array element
Auxiliary Space: O(1)
Last Updated :
09 Jan, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...