Count array elements whose all distinct digits appear in K
Last Updated :
09 Jan, 2023
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
// C++ program for the above approach
#include <bits/stdc++.h>
using namespace std;
// Function to check a digit occurs
// in the digit of K or not
static bool isValidDigit(int digit, int K)
{
// Iterate over all possible
// digits of K
while (K != 0)
{
// If current digit
// equal to digit
if (K % 10 == digit)
{
return true;
}
// Update K
K = K / 10;
}
return false;
}
// Function to find the count of array
// elements whose distinct digits are
// a subset of digits of K
int noOfValidNumbers(int K, int arr[], int n)
{
// Stores count of array elements
// whose distinct digits are subset
// of digits of K
int count = 0;
// Traverse the array, []arr
for(int i = 0; i < n; i++)
{
// Stores the current element
int no = arr[i];
// Check if all the digits arr[i]
// is a subset of the digits of
// K or not
bool flag = true;
// Iterate over all possible
// digits of arr[i]
while (no != 0)
{
// Stores current digit
int digit = no % 10;
// If current digit does not
// appear in K
if (!isValidDigit(digit, K))
{
// Update flag
flag = false;
break;
}
// Update no
no = no / 10;
}
// If all the digits arr[i]
// appear in K
if (flag == true)
{
// Update count
count++;
}
}
// Finally print count
return count;
}
// Driver Code
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;
}
// This code is contributed by susmitakundugoaldanga
Java
// Java program for the above approach
import java.io.*;
class GFG {
// Function to check a digit occurs
// in the digit of K or not
static boolean isValidDigit(int digit, int K)
{
// Iterate over all possible
// digits of K
while (K != 0) {
// If current digit
// equal to digit
if (K % 10 == digit) {
return true;
}
// Update K
K = K / 10;
}
return false;
}
// Function to find the count of array elements
// whose distinct digits are a subset of digits of K
static int noOfValidNumbers(int K, int arr[])
{
// Stores count of array elements
// whose distinct digits are subset
// of digits of K
int count = 0;
// Traverse the array, arr[]
for (int i = 0; i < arr.length; i++) {
// Stores the current element
int no = arr[i];
// Check if all the digits arr[i]
// is a subset of the digits of
// K or not
boolean flag = true;
// Iterate over all possible
// digits of arr[i]
while (no != 0) {
// Stores current digit
int digit = no % 10;
// If current digit does not
// appear in K
if (!isValidDigit(digit, K)) {
// Update flag
flag = false;
break;
}
// Update no
no = no / 10;
}
// If all the digits arr[i] appear in K
if (flag == true) {
// Update count
count++;
}
}
// Finally print count
return count;
}
// Driver Code
public static void main(String[] args)
{
int K = 12;
int arr[] = { 1, 12, 1222, 13, 2 };
System.out.println(noOfValidNumbers(K, arr));
}
}
Python3
# Python3 program for the above approach
# Function to check a digit occurs
# in the digit of K or not
def isValidDigit(digit, K):
# Iterate over all possible
# digits of K
while (K != 0):
# If current digit
# equal to digit
if (K % 10 == digit):
return True
# Update K
K = K // 10
return False
# Function to find the count of array
# elements whose distinct digits are
# a subset of digits of K
def noOfValidNumbers(K, arr, n):
# Stores count of array elements
# whose distinct digits are subset
# of digits of K
count = 0
# Traverse the array, []arr
for i in range(n):
# Stores the current element
no = arr[i]
# Check if all the digits arr[i]
# is a subset of the digits of
# K or not
flag = True
# Iterate over all possible
# digits of arr[i]
while (no != 0):
# Stores current digit
digit = no % 10
# If current digit does not
# appear in K
if (not isValidDigit(digit, K)):
# Update flag
flag = False
break
# Update no
no = no // 10
# If all the digits arr[i]
# appear in K
if (flag == True):
# Update count
count += 1
# Finally print count
return count
# Driver Code
if __name__ == "__main__":
K = 12
arr = [1, 12, 1222, 13, 2]
n = len(arr)
print(noOfValidNumbers(K, arr, n))
# This code is contributed by chitranayal.
C#
// C# program for the above approach
using System;
class GFG{
// Function to check a digit occurs
// in the digit of K or not
static bool isValidDigit(int digit, int K)
{
// Iterate over all possible
// digits of K
while (K != 0)
{
// If current digit
// equal to digit
if (K % 10 == digit)
{
return true;
}
// Update K
K = K / 10;
}
return false;
}
// Function to find the count of array elements
// whose distinct digits are a subset of digits of K
static int noOfValidNumbers(int K, int []arr)
{
// Stores count of array elements
// whose distinct digits are subset
// of digits of K
int count = 0;
// Traverse the array, []arr
for(int i = 0; i < arr.Length; i++)
{
// Stores the current element
int no = arr[i];
// Check if all the digits arr[i]
// is a subset of the digits of
// K or not
bool flag = true;
// Iterate over all possible
// digits of arr[i]
while (no != 0)
{
// Stores current digit
int digit = no % 10;
// If current digit does not
// appear in K
if (!isValidDigit(digit, K))
{
// Update flag
flag = false;
break;
}
// Update no
no = no / 10;
}
// If all the digits arr[i] appear in K
if (flag == true)
{
// Update count
count++;
}
}
// Finally print count
return count;
}
// Driver Code
public static void Main(String[] args)
{
int K = 12;
int []arr = { 1, 12, 1222, 13, 2 };
Console.WriteLine(noOfValidNumbers(K, arr));
}
}
// This code is contributed by shikhasingrajput
JavaScript
<script>
// Javascript program for the above approach
// Function to check a digit occurs
// in the digit of K or not
function isValidDigit(digit, K)
{
// Iterate over all possible
// digits of K
while (K != 0)
{
// If current digit
// equal to digit
if (K % 10 == digit)
{
return true;
}
// Update K
K = parseInt(K / 10);
}
return false;
}
// Function to find the count of array
// elements whose distinct digits are
// a subset of digits of K
function noOfValidNumbers(K, arr, n)
{
// Stores count of array elements
// whose distinct digits are subset
// of digits of K
let count = 0;
// Traverse the array, []arr
for(let i = 0; i < n; i++)
{
// Stores the current element
let no = arr[i];
// Check if all the digits arr[i]
// is a subset of the digits of
// K or not
let flag = true;
// Iterate over all possible
// digits of arr[i]
while (no != 0)
{
// Stores current digit
let digit = no % 10;
// If current digit does not
// appear in K
if (!isValidDigit(digit, K))
{
// Update flag
flag = false;
break;
}
// Update no
no = parseInt(no / 10);
}
// If all the digits arr[i]
// appear in K
if (flag == true)
{
// Update count
count++;
}
}
// Finally print count
return count;
}
// Driver Code
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++
// C++ program to implement
// the above approach
#include <bits/stdc++.h>
using namespace std;
// Function to the count of array elements whose
// distinct digits are a subset of the digits of K
static int noOfValidKbers(int K, vector<int> arr)
{
// Stores distinct digits of K
map<int,int> set;
// Iterate over all the digits of K
while (K != 0)
{
// Insert current digit into set
set[K % 10] = 1;
// Update K
K = K / 10;
}
// Stores the count of array elements
// whose distinct digits are a subset
// of the digits of K
int count = 0;
// Traverse the array, arr[]
for (int i = 0; i < arr.size(); i++)
{
// Stores current element
int no = arr[i];
// Check if all the digits of arr[i]
// are present in K or not
bool flag = true;
// Iterate over all the digits of arr[i]
while (no != 0)
{
// Stores current digit
int digit = no % 10;
// If digit not present in the set
if (set.find(digit)==set.end())
{
// Update flag
flag = false;
break;
}
// Update no
no = no / 10;
}
// If all the digits of
// arr[i] present in set
if (flag == true)
{
// Update count
count++;
}
}
return count;
}
// Driver Code
int main()
{
int K = 12;
vector<int> arr = { 1, 12, 1222, 13, 2 };
cout<<(noOfValidKbers(K, arr));
}
// This code is contributed by mohit kumar 29
Java
// Java program to implement
// the above approach
import java.io.*;
import java.util.*;
class GFG {
// Function to the count of array elements whose
// distinct digits are a subset of the digits of K
static int noOfValidKbers(int K, int arr[])
{
// Stores distinct digits of K
HashSet<Integer> set = new HashSet<>();
// Iterate over all the digits of K
while (K != 0) {
// Insert current digit into set
set.add(K % 10);
// Update K
K = K / 10;
}
// Stores the count of array elements
// whose distinct digits are a subset
// of the digits of K
int count = 0;
// Traverse the array, arr[]
for (int i = 0; i < arr.length; i++) {
// Stores current element
int no = arr[i];
// Check if all the digits of arr[i]
// are present in K or not
boolean flag = true;
// Iterate over all the digits of arr[i]
while (no != 0) {
// Stores current digit
int digit = no % 10;
// If digit not present in the set
if (!set.contains(digit)) {
// Update flag
flag = false;
break;
}
// Update no
no = no / 10;
}
// If all the digits of
// arr[i] present in set
if (flag == true) {
// Update count
count++;
}
}
return count;
}
// Driver Code
public static void main(String[] args)
{
int K = 12;
int arr[] = { 1, 12, 1222, 13, 2 };
System.out.println(noOfValidKbers(K, arr));
}
}
Python3
# Python 3 program to implement
# the above approach
# Function to the count of array elements whose
# distinct digits are a subst of the digits of K
def noOfValidKbers(K, arr):
# Stores distinct digits of K
st = {}
# Iterate over all the digits of K
while(K != 0):
# Insert current digit into st
if(K % 10 in st):
st[K % 10] = 1
else:
st[K % 10] = st.get(K % 10, 0) + 1
# Update K
K = K // 10
# Stores the count of array elements
# whose distinct digits are a subst
# of the digits of K
count = 0
# Traverse the array, arr[]
for i in range(len(arr)):
# Stores current element
no = arr[i]
# Check if all the digits of arr[i]
# are present in K or not
flag = True
# Iterate over all the digits of arr[i]
while(no != 0):
# Stores current digit
digit = no % 10
# If digit not present in the st
if (digit not in st):
# Update flag
flag = False
break
# Update no
no = no//10
# If all the digits of
# arr[i] present in st
if (flag == True):
# Update count
count += 1
return count
# Driver Code
if __name__ == '__main__':
K = 12
arr = [1, 12, 1222, 13, 2]
print(noOfValidKbers(K, arr))
# This code is contributed by SURENDRA_GANGWAR.
C#
// C# program to implement
// the above approach
using System;
using System.Collections.Generic;
class GFG{
// Function to the count of array elements whose
// distinct digits are a subset of the digits of K
static int noOfValidKbers(int K, int []arr)
{
// Stores distinct digits of K
HashSet<int> set = new HashSet<int>();
// Iterate over all the digits of K
while (K != 0)
{
// Insert current digit into set
set.Add(K % 10);
// Update K
K = K / 10;
}
// Stores the count of array elements
// whose distinct digits are a subset
// of the digits of K
int count = 0;
// Traverse the array, []arr
for(int i = 0; i < arr.Length; i++)
{
// Stores current element
int no = arr[i];
// Check if all the digits of arr[i]
// are present in K or not
bool flag = true;
// Iterate over all the digits of arr[i]
while (no != 0)
{
// Stores current digit
int digit = no % 10;
// If digit not present in the set
if (!set.Contains(digit))
{
// Update flag
flag = false;
break;
}
// Update no
no = no / 10;
}
// If all the digits of
// arr[i] present in set
if (flag == true)
{
// Update count
count++;
}
}
return count;
}
// Driver Code
public static void Main(String[] args)
{
int K = 12;
int []arr = { 1, 12, 1222, 13, 2 };
Console.WriteLine(noOfValidKbers(K, arr));
}
}
// This code is contributed by 29AjayKumar
JavaScript
<script>
// Javascript program to implement
// the above approach
// Function to the count of array elements whose
// distinct digits are a subset of the digits of K
function noOfValidKbers(K , arr) {
// Stores distinct digits of K
var set = new Set();
// Iterate over all the digits of K
while (K != 0) {
// Insert current digit into set
set.add(K % 10);
// Update K
K = parseInt(K / 10);
}
// Stores the count of array elements
// whose distinct digits are a subset
// of the digits of K
var count = 0;
// Traverse the array, arr
for (i = 0; i < arr.length; i++) {
// Stores current element
var no = arr[i];
// Check if all the digits of arr[i]
// are present in K or not
var flag = true;
// Iterate over all the digits of arr[i]
while (no != 0) {
// Stores current digit
var digit = no % 10;
// If digit not present in the set
if (!set.has(digit)) {
// Update flag
flag = false;
break;
}
// Update no
no = parseInt(no / 10);
}
// If all the digits of
// arr[i] present in set
if (flag == true) {
// Update count
count++;
}
}
return count;
}
// Driver Code
var K = 12;
var arr = [ 1, 12, 1222, 13, 2 ];
document.write(noOfValidKbers(K, arr));
// This code contributed by umadevi9616
</script>
Time Complexity: O(N * log10(Max)), where Max is the largest array element
Auxiliary Space: O(1)
Similar Reads
Count distinct elements in an array in Python
Given an unsorted array, count all distinct elements in it. Examples: Input : arr[] = {10, 20, 20, 10, 30, 10} Output : 3 Input : arr[] = {10, 20, 20, 10, 20} Output : 2 We have existing solution for this article. We can solve this problem in Python3 using Counter method. Approach#1: Using Set() Thi
2 min read
Construct an Array having K Subarrays with all distinct elements
Given integers N and K, the task is to construct an array arr[] of size N using numbers in the range [1, N] such that it has K sub-arrays whose all the elements are distinct. Note: If there are multiple possible answers return any of them. Examples: Input: N = 5, K = 8Output: {1, 2, 3, 3, 3}Explanat
7 min read
Split an array containing N elements into K sets of distinct elements
Given two integers N and K and an array arr[] consisting of duplicate elements, the task is to split N elements into K sets of distinct elements.Examples: Input: N = 5, K = 2, arr[] = {3, 2, 1, 2, 3} Output: ( 3 2 1 ) ( 2 3 )Input: N = 5, K = 2, arr[] = {2, 1, 1, 2, 1} Output: -1 Explanation: It is
7 min read
Count array elements having sum of digits equal to K
Given an array arr[] of size N, the task is to count the number of array elements whose sum of digits is equal to K. Examples: Input: arr[] = {23, 54, 87, 29, 92, 62}, K = 11Output: 2Explanation: 29 = 2 + 9 = 1192 = 9 + 2 = 11 Input: arr[]= {11, 04, 57, 99, 98, 32}, K = 18Output: 1 Approach: Follow
6 min read
Count the number of elements in an array which are divisible by k
Given an array of integers. The task is to calculate the count of a number of elements which are divisible by a given number k. Examples: Input: arr[] = { 2, 6, 7, 12, 14, 18 }, k = 3 Output: 3 Numbers which are divisible by k are { 6, 12, 18 } Input: arr[] = { 2, 6, 7, 12, 14, 18 }, k = 2 Output: 5
6 min read
Absolute distinct count in a sorted array
Given a sorted array of integers, return the number of distinct absolute values among the elements of the array. The input can contain duplicates values. Examples: Input: [-3, -2, 0, 3, 4, 5] Output: 5 There are 5 distinct absolute values among the elements of this array, i.e. 0, 2, 3, 4 and 5) Inpu
12 min read
Count of integers in an Array whose length is a multiple of K
Given an array arr of N elements and an integer K, the task is to count all the elements whose length is a multiple of K.Examples: Input: arr[]={1, 12, 3444, 544, 9}, K = 2 Output: 2 Explanation: There are 2 numbers whose digit count is multiple of 2 {12, 3444}. Input: arr[]={12, 345, 2, 68, 7896},
5 min read
Count of N-digit numbers with all distinct digits
Given an integer N, the task is to find the count of N-digit numbers with all distinct digits.Examples: Input: N = 1 Output: 9 1, 2, 3, 4, 5, 6, 7, 8 and 9 are the 1-digit numbers with all distinct digits.Input: N = 3 Output: 648 Naive Approach: If N > 10 i.e. there will be atleast one digit whic
7 min read
Counting Distinct elements after Range of Operations
Given an integer K and array A[] of size N, count the number of distinct elements after performing an operation on every array index. For each i (0 ⤠i ⤠N - 1), we can perform one of the following operations exactly once: Add K to A[i]Subtract K from A[i]Leave A[i] as it is.Examples: Input: N = 6,
7 min read
Count frequency of digit K in given Array
Given an array arr[] of integers of size N and a single digit integer K. The task is to find the total count of occurrences of the digit K in the array Examples: Input: arr[] = {15, 66, 26, 91}, K = 6Output: 3Explanation: Occurrences of 6 in each array elements are: 0, 2, 1, 0 respectively.Therefore
5 min read