Count of Integers in given range consisting only given set of Digits
Last Updated :
21 Mar, 2022
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++
// C++ code to implement the approach
#include <bits/stdc++.h>
using namespace std;
// Function Check number is
// subset of prime digit of not
bool has_val(int x, set<int>& st)
{
while (x != 0) {
if (st.find(x % 10) == st.end())
return false;
x /= 10;
}
return true;
}
// Function to find
// non-prime between range
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]);
// Loop to check if number contains
// only the digits in given set
for (int k = A; k < B; k++) {
if (has_val(k, st))
ans += 1;
}
return ans;
}
// Driver Code
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
// Java code to implement the approach
import java.util.*;
public class GFG {
// Function Check number is
// subset of prime digit of not
static boolean has_val(int x, HashSet<Integer> st)
{
while (x != 0) {
if (st.contains(x % 10) == false)
return false;
x /= 10;
}
return true;
}
// Function to find
// non-prime between range
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]);
// Loop to check if number contains
// only the digits in given set
for (int k = A; k < B; k++) {
if (has_val(k, st))
ans += 1;
}
return ans;
}
// Driver Code
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);
}
}
// This code is contributed by Samim Hossain Mondal.
Python3
# python3 code to implement the approach
# Function Check number is
# subset of prime digit of not
def has_val(x, st):
while (x != 0):
if (not x % 10 in st):
return False
x //= 10
return True
# Function to find
# non-prime between range
def total_Num(A, B, arr, N):
ans = 0
st = set()
for i in range(0, N):
st.add(arr[i])
# Loop to check if number contains
# only the digits in given set
for k in range(A, B):
if (has_val(k, st)):
ans += 1
return ans
# Driver Code
if __name__ == "__main__":
L, R = 1, 100
arr = [2, 3, 5, 7]
N = len(arr)
ans = total_Num(L, R, arr, N)
print(ans)
# This code is contributed by rakeshsahni
C#
// C# code to implement the approach
using System;
using System.Collections.Generic;
public class GFG{
// Function Check number is
// subset of prime digit of not
static bool has_val(int x, HashSet<int> st)
{
while (x != 0) {
if (st.Contains(x % 10) == false)
return false;
x /= 10;
}
return true;
}
// Function to find
// non-prime between range
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]);
// Loop to check if number contains
// only the digits in given set
for (int k = A; k < B; k++) {
if (has_val(k, st))
ans += 1;
}
return ans;
}
// Driver Code
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);
}
}
// This code is contributed by hrithikgarg03188.
JavaScript
<script>
// JavaScript code for the above approach
// Function Check number is
// subset of prime digit of not
function has_val(x, st) {
while (x != 0) {
if (!st.has(x % 10))
return false;
x = Math.floor(x / 10);
}
return true;
}
// Function to find
// non-prime between range
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]);
// Loop to check if number contains
// only the digits in given set
for (let k = A; k < B; k++) {
if (has_val(k, st))
ans += 1;
}
return ans;
}
// Driver Code
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);
// This code is contributed by Potta Lokesh
</script>
Time Complexity: O((R-L)*logN)
Auxiliary Space: O(1)
Similar Reads
Count numbers that does not contain digit N in given range Given integers, N, L, and R, the task is to find the number of integers in the range L to R that does not contain the digit N. print the answer modulo 109 + 7. ( L ? R ? 101000000) Examples: Input: N = 5, L = 1, R = 10Output: 9Explanation: excluding all 5 others from 1 to 10 will be included in the
14 min read
Count of integers in given range having their last K digits are equal Given a range from L to R and an integer K, the task is to count the number of integers in the given range such that their last K digits are equal. Example: Input: L = 49, R = 101, K=2Output: 6Explanation: There are 6 possible integers t.e., 55, 66, 77, 88, 99 and 100 such that their last K(i.e., 2)
6 min read
Count of numbers with all digits same in a given range Given two integers L and R denoting the starting and end values of a range, the task is to count all numbers in that range whose all digit are same, like 1, 22, 444, 3333, etc.Example: Input: L = 12, R = 68 Output: 5 Explanation: { 22, 33, 44, 55, 66} are the numbers with same digits in the given ra
9 min read
Count number of integers in given range with adjacent digits different and sum of digits equal to M Given integers T, A, and B, the task for this problem is to find numbers in the range [A, B] such that the adjacent digits of the number are different and the sum of digits is equal to T. ( A ? B ? 1018) Examples: Input: T = 5, A = 1, B = 100Output: 6Explanation: 5, 14, 23, 32, 41, and 50 are valid
15+ min read
Count numbers from a given range whose product of digits is K Given three positive integers L, R and K, the task is to count the numbers in the range [L, R] whose product of digits is equal to K Examples: Input: L = 1, R = 130, K = 14Output: 3Explanation: Numbers in the range [1, 100] whose sum of digits is K(= 14) are: 27 => 2 * 7 = 14 72 => 7 * 2 = 14
15+ min read
Count numbers from a given range whose product of digits is K Given three positive integers L, R and K, the task is to count the numbers in the range [L, R] whose product of digits is equal to K Examples: Input: L = 1, R = 130, K = 14Output: 3Explanation: Numbers in the range [1, 100] whose sum of digits is K(= 14) are: 27 => 2 * 7 = 14 72 => 7 * 2 = 14
15+ min read