Count of Integers in given range consisting only given set of Digits
Given two integers L and R, and an array arr[] containing single digit integers, the task is to find all the integers in the range [L, R) consisting of digits from given array of digits.
Examples:
Input: L = 1, R = 100, arr[] = {2, 3, 5, 7}
Output: 20
Explanation: The number between 1 and 100 total integers which are made up with 2, 3, 5, 7 are:
2, 3, 5, 7, 22, 23, 25, 27, 32, 33, 35, 37, 52, 53, 55, 57, 72, 73, 75, and 77. Total 20.
Input: L = 50, R = 60, arr[] = 5
Output: 1
Explanation: The only number in range 50 and 60 55.
Approach: The solution to the problem is based on greedy approach by using the below idea:
Traverse each integer in range [L, R) and check if it consists of only given set of digits.
Follow the steps below to solve the problem:
- Iterate over the range [L, R).
- Check whether the number is a combination of numbers given in arr[] or not with the help of a set.
- If it is a subset of given digits then increase count by 1, otherwise not.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
bool has_val( int x, set< int >& st)
{
while (x != 0) {
if (st.find(x % 10) == st.end())
return false ;
x /= 10;
}
return true ;
}
int total_Num( int A, int B, int arr[], int N)
{
int ans = 0;
set< int > st;
for ( int i = 0; i < N; i++)
st.insert(arr[i]);
for ( int k = A; k < B; k++) {
if (has_val(k, st))
ans += 1;
}
return ans;
}
int main()
{
int L = 1, R = 100;
int arr[] = { 2, 3, 5, 7 };
int N = sizeof (arr) / sizeof (arr[0]);
int ans = total_Num(L, R, arr, N);
cout << ans;
return 0;
}
|
Java
import java.util.*;
public class GFG {
static boolean has_val( int x, HashSet<Integer> st)
{
while (x != 0 ) {
if (st.contains(x % 10 ) == false )
return false ;
x /= 10 ;
}
return true ;
}
static int total_Num( int A, int B, int arr[], int N)
{
int ans = 0 ;
HashSet<Integer> st = new HashSet<>();
for ( int i = 0 ; i < N; i++)
st.add(arr[i]);
for ( int k = A; k < B; k++) {
if (has_val(k, st))
ans += 1 ;
}
return ans;
}
public static void main(String args[])
{
int L = 1 , R = 100 ;
int [] arr = { 2 , 3 , 5 , 7 };
int N = arr.length;
int ans = total_Num(L, R, arr, N);
System.out.print(ans);
}
}
|
Python3
def has_val(x, st):
while (x ! = 0 ):
if ( not x % 10 in st):
return False
x / / = 10
return True
def total_Num(A, B, arr, N):
ans = 0
st = set ()
for i in range ( 0 , N):
st.add(arr[i])
for k in range (A, B):
if (has_val(k, st)):
ans + = 1
return ans
if __name__ = = "__main__" :
L, R = 1 , 100
arr = [ 2 , 3 , 5 , 7 ]
N = len (arr)
ans = total_Num(L, R, arr, N)
print (ans)
|
C#
using System;
using System.Collections.Generic;
public class GFG{
static bool has_val( int x, HashSet< int > st)
{
while (x != 0) {
if (st.Contains(x % 10) == false )
return false ;
x /= 10;
}
return true ;
}
static int total_Num( int A, int B, int [] arr, int N)
{
int ans = 0;
HashSet< int > st = new HashSet< int >();
for ( int i = 0; i < N; i++)
st.Add(arr[i]);
for ( int k = A; k < B; k++) {
if (has_val(k, st))
ans += 1;
}
return ans;
}
static public void Main (){
int L = 1, R = 100;
int [] arr = { 2, 3, 5, 7 };
int N = arr.Length;
int ans = total_Num(L, R, arr, N);
Console.Write(ans);
}
}
|
Javascript
<script>
function has_val(x, st) {
while (x != 0) {
if (!st.has(x % 10))
return false ;
x = Math.floor(x / 10);
}
return true ;
}
function total_Num(A, B, arr, N) {
let ans = 0;
let st = new Set();
for (let i = 0; i < N; i++)
st.add(arr[i]);
for (let k = A; k < B; k++) {
if (has_val(k, st))
ans += 1;
}
return ans;
}
let L = 1, R = 100;
let arr = [2, 3, 5, 7];
let N = arr.length;
let ans = total_Num(L, R, arr, N);
document.write(ans);
</script>
|
Time Complexity: O((R-L)*logN)
Auxiliary Space: O(1)
Last Updated :
21 Mar, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...