Smallest integer greater than n such that it consists of digit m exactly k times - GeeksforGeeks

24 Nov, 2021
Given three integer n, m and k, the task is to find the smallest integer > n such that digit m appears exactly k times in it.Examples:  
Input: n = 111, m = 2, k = 2 Output: 122Input: n = 111, m = 2, k = 3 Output: 222  
 
Approach: Start iterating from n + 1 and for each integer i check whether it consists of digit m exactly k times. This way smallest integer > n with digit m occurring exactly k times can be found.Below is the implementation of the above approach:  
C++
Java
Python3
C#
PHP
Javascript
// C++ implementation of the approach#include <bits/stdc++.h>using namespace std; // Function that returns true if n contains// digit m exactly k timesbool digitWell(int n, int m, int k){    int cnt = 0;    while (n > 0) {        if (n % 10 == m)            ++cnt;        n /= 10;    }    return cnt == k;} // Function to return the smallest integer > n// with digit m occurring exactly k timesint findInt(int n, int m, int k){     int i = n + 1;     while (true) {        if (digitWell(i, m, k))            return i;        i++;    }} // Driver codeint main(){    int n = 111, m = 2, k = 2;    cout << findInt(n, m, k);    return 0;}
// Java implementation of the approachclass GFG{     // Function that returns true if n contains// digit m exactly k timesstatic boolean digitWell(int n, int m, int k){    int cnt = 0;    while (n > 0)    {        if (n % 10 == m)            ++cnt;        n /= 10;    }    return cnt == k;} // Function to return the smallest integer > n// with digit m occurring exactly k timesstatic int findInt(int n, int m, int k){     int i = n + 1;     while (true)    {        if (digitWell(i, m, k))            return i;        i++;    }} // Driver codepublic static void main(String[] args){    int n = 111, m = 2, k = 2;    System.out.println(findInt(n, m, k));}} // This code is contributed by Code_Mech
# Python3 implementation of the approach # Function that returns true if n# contains digit m exactly k timesdef digitWell(n, m, k):     cnt = 0    while (n > 0):         if (n % 10 == m):            cnt = cnt + 1;        n = (int)(n / 10);     return cnt == k; # Function to return the smallest integer > n# with digit m occurring exactly k timesdef findInt(n, m, k):     i = n + 1;     while (True):        if (digitWell(i, m, k)):            return i;        i = i + 1; # Driver coden = 111; m = 2; k = 2;print(findInt(n, m, k)); # This code is contributed# by Akanksha Rai
// C# implementation of the approachusing System;class GFG{     // Function that returns true if n contains// digit m exactly k timesstatic bool digitWell(int n, int m, int k){    int cnt = 0;    while (n > 0)    {        if (n % 10 == m)            ++cnt;        n /= 10;    }    return cnt == k;} // Function to return the smallest integer > n// with digit m occurring exactly k timesstatic int findInt(int n, int m, int k){     int i = n + 1;     while (true)    {        if (digitWell(i, m, k))            return i;        i++;    }} // Driver codepublic static void Main(){    int n = 111, m = 2, k = 2;    Console.WriteLine(findInt(n, m, k));}} // This code is contributed// by Akanksha Rai
<?php// PHP implementation of the approach // Function that returns true if n// contains digit m exactly k timesfunction digitWell($n, $m, $k){    $cnt = 0;    while ($n > 0)    {        if ($n % 10 == $m)            ++$cnt;        $n = floor($n / 10);    }    return $cnt == $k;} // Function to return the smallest integer > n// with digit m occurring exactly k timesfunction findInt($n, $m, $k){    $i = $n + 1;     while (true)    {        if (digitWell($i, $m, $k))            return $i;        $i++;    }} // Driver code$n = 111;$m = 2;$k = 2; echo findInt($n, $m, $k); // This code is contributed by Ryuga?>
<script> // Javascript implementation of the approach // Function that returns true if n contains// digit m exactly k timesfunction digitWell(n, m, k){    var cnt = 0;    while (n > 0) {        if (n % 10 == m)            ++cnt;        n = Math.floor(n/10);    }    if(cnt == k)      return true;    else        return false;} // Function to return the smallest integer > n// with digit m occurring exactly k timesfunction findInt(n, m, k){     var i = n + 1;     while (true) {        if (digitWell(i, m, k))            return i;        i++;    }} // Driver code    var n = 111, m = 2, k = 2;    document.write(findInt(n, m, k)); </script>
122
 
Time Complexity: O(n * log10n)
Auxiliary Space: O(1)
ankthon
Code_Mech
Akanksha_Rai
bgangwar59
samim2000
number-digits
Mathematical
School Programming
Mathematical
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Program to print prime numbers from 1 to N.
Segment Tree | Set 1 (Sum of given range)
Modular multiplicative inverse
Count all possible paths from top left to bottom right of a mXn matrix
Fizz Buzz Implementation
Python Dictionary
Arrays in C/C++
Inheritance in C++
Reverse a string in Java
C++ Classes and Objects