给定范围内的整数计数,仅包含给定的一组数字
给定两个整数L和R以及一个包含单个数字整数的数组arr[] ,任务是从给定的数字数组中找到由数字组成的范围 [L, R) 中的所有整数。
例子:
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.
方法:该问题的解决方案基于贪心方法,使用以下思想:
Traverse each integer in range [L, R) and check if it consists of only given set of digits.
请按照以下步骤解决问题:
- 迭代范围[L, R) 。
- 在集合的帮助下检查数字是否是arr[]中给出的数字的组合。
- 如果它是给定数字的子集,则将count增加 1,否则不增加。
下面是上述方法的实现:
C++
// C++ code to implement the approach
#include
using namespace std;
// Function Check number is
// subset of prime digit of not
bool has_val(int x, set& 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 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 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 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 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 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
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
输出
20
时间复杂度: O((RL)*logN)
辅助空间: O(1)