Open In App

Smallest number greater than K by removing digits from N

Improve
Improve
Like Article
Like
Save
Share
Report

Given two integers N and K (K<N), the task is to find the smallest number greater than K by removing digits from N.

Note: You cannot rearrange the digits of N.

Examples:

Input: N = 7182, K = 11
Output: 12
Explanation: Among all the possible combination, 12 is minimum number greater than 11. 

Input: N = 121320, K = 756
Output: 1120
Explanation: Among all the possible combination, 1120 is minimum number greater than 756.

Approach: The basic idea is to 

Find all the subsequences and from the possible numbers get the minimum number that is greater than K.

Follow the steps mentioned below to implement the idea:

  • Initialize the ‘ans’ with 0.
  • Take the current number and store all its digits in ‘digits’ array.
  • Generate all possible sequences for a number.
  • Maintain an integer ‘mask’ variable whose binary representation represents elements to be taken or elements to be removed.
    • If the ith bit is 1, don’t take the current element means we are removing the current element.
    • If the ith bit is 0, don’t take the current element.
  • Now compute N from the current mask. Let’s call it ‘temp’. If temp > K, then ans = min(ans, temp).
  • Now there are two cases for the current element for its contribution in further cases:
    • Leave the current element,  then mask become = mask| pow(2, i) where i represents the position of the current element.
    • Include the current element, then the mask remains the same. That means the ith bit is kept 0.
  • Return the ans.

Below is the implementation of the above approach.

C++




// C++ code to implement the approach
 
#include <bits/stdc++.h>
using namespace std;
int ans = 0;
 
// Function to generate all possible combination
void find(int i, vector<int>& digits, int mask, int k)
{
    // Base case
    if (i == digits.size())
        return;
 
    // Store the ans for current state of mask
    int temp = 0;
    int pow_of_10 = 1;
    for (int j = digits.size() - 1; j >= 0; j--) {
        int k = pow(2, j);
 
        // If the bit is 1 means current element
        // is removed in that state
        if (!(mask & k)) {
            temp = temp + pow_of_10 * digits[j];
            pow_of_10 *= 10;
        }
    }
    if (temp > k) {
        ans = min(ans, temp);
    }
    int next = pow(2, i);
    find(i + 1, digits, mask, k);
    find(i + 1, digits, mask | next, k);
}
 
// Function to find number less than N greater than K
int GreaterthanK(int N, int K)
{
    // Array to store digits of N
    vector<int> digits;
    int M = N;
    while (M) {
        digits.push_back(M % 10);
        M /= 10;
    }
    reverse(digits.begin(), digits.end());
    ans = N;
    find(0, digits, 0, K);
    return ans;
}
 
// Driver code
int main()
{
    int N = 121230;
    int K = 756;
 
    // Function call
    cout << (GreaterthanK(N, K)) << endl;
    return 0;
}


Java




// Java code to implement the approach
import java.util.*;
class GFG{
 
static int ans = 0;
 
// Function to generate all possible combination
static void find(int i, ArrayList<Integer> digits , int mask, int k)
{
    // Base case
    if (i == digits.size())
        return;
 
    // Store the ans for current state of mask
    int temp = 0;
    int pow_of_10 = 1;
    for (int j = digits.size() - 1; j >= 0; j--) {
         int kk = (int)Math.pow(2, j);
 
        // If the bit is 1 means current element
        // is removed in that state
        if ((mask & kk)==0) {
            temp = temp + pow_of_10 * digits.get(j);
            pow_of_10 *= 10;
        }
    }
    if (temp > k) {
        ans = Math.min(ans, temp);
    }
    int next = (int)Math.pow(2, i);
    find(i + 1, digits, mask, k);
    find(i + 1, digits, mask | next, k);
}
 
// Function to find number less than N greater than K
static int GreaterthanK(int N, int K)
{
    // Array to store digits of N
    ArrayList<Integer> digits = new ArrayList<Integer>();
    int M = N;
    while (M>0) {
        digits.add(M % 10);
        M /= 10;
    }
    Collections.reverse(digits);
    ans = N;
    find(0, digits, 0, K);
    return ans;
}
 
// Driver code
public static void main(String[] args)
{
    int N = 121230;
    int K = 756;
 
    // Function call
    System.out.println(GreaterthanK(N, K));
}
 
}
// This code is contributed by Pushpesh Raj.


Python3




# Python3 code to implement the above approach
ans = 0;
 
# Function to generate all possible combination
def find(i, digits, mask, k) :
    global ans
     
    # Base case
    if (i == len(digits)) :
        return ans;
 
    # Store the ans for current state of mask
    temp = 0;
    pow_of_10 = 1;
     
    for j in range(len(digits) - 1, -1, -1) :
        kk = 2 ** j;
 
        # If the bit is 1 means current element
        # is removed in that state
        if ((mask & kk) == 0) :
            temp = temp + pow_of_10 * digits[j];
            pow_of_10 *= 10;
       
    if (temp > k) :
        ans = min(ans, temp);
 
    next = 2 ** i;
    find(i + 1, digits, mask, k);
     
    tmp = mask | next;
    find(i + 1, digits, tmp, k);
 
# Function to find number less than N greater than K
def GreaterthanK(N, K) :
    global ans
     
    # Array to store digits of N
    digits = [];
    M = N;
    while M > 0:
        digits.append(M % 10);
        M //= 10;
 
    digits.reverse()
    ans = N;
    find(0, digits, 0, K);
    return ans;
 
# Driver code
if __name__ == "__main__" :
 
    N = 121230;
    K = 756;
 
    # Function call
    print(GreaterthanK(N, K));
   
    # This code is contributed by AnkThon


C#




// C# implementation
using System;
using System.Collections.Generic;
public class GFG {
  public static int ans = 0;
 
  // Function to generate all possible combination
  public static void find(int i, List<int> digits,
                          int mask, int k)
  {
    // Base case
    if (i == digits.Count)
      return;
 
    // Store the ans for current state of mask
    int temp = 0;
    int pow_of_10 = 1;
    for (int j = digits.Count - 1; j >= 0; j--) {
      int kk = (int)Math.Pow(2, j);
 
      // If the bit is 1 means current element
      // is removed in that state
      if ((mask & kk) == 0) {
        temp = temp + pow_of_10 * digits[j];
        pow_of_10 *= 10;
      }
    }
    if (temp > k) {
      ans = Math.Min(ans, temp);
    }
    int next = (int)Math.Pow(2, i);
    find(i + 1, digits, mask, k);
    find(i + 1, digits, mask | next, k);
  }
 
  // Function to find number less than N greater than K
  public static int GreaterthanK(int N, int K)
  {
    // Array to store digits of N
    List<int> dig = new List<int>();
    int M = N;
    while (M > 0) {
      int rem = M % 10;
      dig.Add(rem);
      M = (int)(M / 10);
    }
    dig.Reverse();
    ans = N;
    find(0, dig, 0, K);
    return ans;
  }
 
  static public void Main()
  {
    int N = 121230;
    int K = 756;
 
    // Function call
    Console.WriteLine(GreaterthanK(N, K));
  }
}
// this code is contributed by ksam24000


Javascript




       // JavaScript code for the above approach
 
       let ans = 0;
 
       // Function to generate all possible combination
       function find(i, digits, mask, k) {
           // Base case
           if (i == digits.length)
               return;
 
           // Store the ans for current state of mask
           let temp = 0;
           let pow_of_10 = 1;
           for (let j = digits.length - 1; j >= 0; j--) {
               let k = Math.pow(2, j);
 
               // If the bit is 1 means current element
               // is removed in that state
               if (!(mask & k)) {
                   temp = temp + pow_of_10 * digits[j];
                   pow_of_10 *= 10;
               }
           }
           if (temp > k) {
               ans = Math.min(ans, temp);
           }
           let next = Math.pow(2, i);
           find(i + 1, digits, mask, k);
           find(i + 1, digits, mask | next, k);
       }
 
       // Function to find number less than N greater than K
       function GreaterthanK(N, K) {
           // Array to store digits of N
           let digits = [];
           let M = N;
           while (M) {
               digits.push(M % 10);
               M = Math.floor(M / 10);
           }
           digits.reverse();
           ans = N;
           find(0, digits, 0, K);
           return ans;
       }
 
       // Driver code
       let N = 121230;
       let K = 756;
 
       // Function call
       console.log(GreaterthanK(N, K) + "<br>")
 
// This code is contributed by Potta Lokesh


Output

1120

Time Complexity: O(M * 2M), where M represents the length of N.
Auxiliary Space: O(M)



Last Updated : 14 Nov, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads