✅Count Numbers (Contest)
Count Numbers easy Time Limit: 2 sec Memory Limit: 128000 kB
Problem Statement :
Given an integer N, find all the numbers less than N which contains only digits 3, 4, and 5. Each digit should be present at least one time. Input The input contains a single integer N.
Constraints:- 1 <= N <= 1000000000 Output Print the count of such numbers which contains only digits 3, 4, and 5. Example Sample Input:- 500
Sample Output:- 4
Explanation:- 345. 435. 453, 354
Sample Input:- 1000
Sample Output:- 6
link:https://my.newtonschool.co/playground/code/l5y3r1zvnj1l/
import java.io.*; // for handling input/output
import java.util.*; // contains Collections framework
// don't change the name of this class
// you can add inner classes if needed
class Main {
private static int recursive(long n, long num, boolean three, boolean four, boolean five){
if(num > n){
return 0;
}
int count = 0;
if(three && four && five){
count++;
}
count += recursive(n, num*10 + 3, true, four, five);
count += recursive(n, num*10 + 4, three, true, five);
count += recursive(n, num*10 + 5, three, four, true);
return count;
}
public static void main (String[] args) {
// Your code here
Scanner sc = new Scanner(System.in);
long n = sc.nextLong();
int count = recursive(n, 0, false, false, false);
System.out.println(count);
}
}
Last updated