Hello Programmers In this post, you will know how to solve the Balsa For The Three Codechef Solution. The Problem Code: BFTT
One more thing to add, don’t directly look for the solutions, first try to solve the problems of Codechef by yourself. If you find any difficulty after trying several times, then you can look for solutions.
Problem
It’s well known that our friend Balsa likes the digit 3 very much. He’s so obsessed with this digit that he spends time thinking about it even during holidays.
Today, Balsa invented an interesting problem: For a positive integer N, find the smallest integer greater than N such that its decimal representation contains the digit 3 at least three times.
You are given the integer N. Please solve this problem for Balsa.
Input
- The first line of the input contains a single integer T denoting the number of test cases. The description of T test cases follows.
- The first and only line of each test case contains a single integer N.
Output
For each test case, print a single line containing one integer — the smallest number greater than N containing the digit 3 at least three times.
Constraints
- 1≤T≤401≤T≤40
- 1≤N≤2⋅10^9
Example
Sample Input 1
3
221
333
3002
Sample Output 1
333 1333 3033
Balsa For The Three CodeChef Solution in JAVA
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int T = sc.nextInt(); for (int tc = 0; tc < T; tc++) { int N = sc.nextInt(); System.out.println(solve(N)); } sc.close(); } static int solve(int N) { for (int i = N + 1;; i++) { if (String.valueOf(i).chars().filter(ch -> ch == '3').count() >= 3) { return i; } } } }
Balsa For The Three CodeChef Solutions in CPP
#include <bits/stdc++.h> #include <algorithm> #include <vector> #include <string> using namespace std; int check(int num){ int temp,c; c = 0; temp = num; while(temp>0){ if(temp%10==3){ c++; } temp = temp/10; } if(c>=3){ return 1; }else{ return 0; } } int main() { int t; cin>>t; while(t--) { int n; cin>>n; n+=1; while(1){ if(check(n)){ break; } n++; } cout<<n<<endl; } return 0; }
Balsa For The Three CodeChef Solutions in Python
for u in range(int(input())): n=int(input()) n+=1 q=str(n).count('3') while(q<3): n+=1 q=str(n).count('3') print(n)
Disclaimer: The above Problem (Balsa For The Three) is generated by CodeChef but the solution is provided by BrokenProgrammers. This tutorial is only for Educational and Learning purposes.
Note:- I compile all programs, if there is any case program is not working and showing an error please let me know in the comment section. If you are using adblocker, please disable adblocker because some functions of the site may not work correctly.