Smallest number greater than K by removing digits from N
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++
#include <bits/stdc++.h>
using namespace std;
int ans = 0;
void find( int i, vector< int >& digits, int mask, int k)
{
if (i == digits.size())
return ;
int temp = 0;
int pow_of_10 = 1;
for ( int j = digits.size() - 1; j >= 0; j--) {
int k = pow (2, j);
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);
}
int GreaterthanK( int N, int K)
{
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;
}
int main()
{
int N = 121230;
int K = 756;
cout << (GreaterthanK(N, K)) << endl;
return 0;
}
|
Java
import java.util.*;
class GFG{
static int ans = 0 ;
static void find( int i, ArrayList<Integer> digits , int mask, int k)
{
if (i == digits.size())
return ;
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 ((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);
}
static int GreaterthanK( int N, int K)
{
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;
}
public static void main(String[] args)
{
int N = 121230 ;
int K = 756 ;
System.out.println(GreaterthanK(N, K));
}
}
|
Python3
ans = 0 ;
def find(i, digits, mask, k) :
global ans
if (i = = len (digits)) :
return ans;
temp = 0 ;
pow_of_10 = 1 ;
for j in range ( len (digits) - 1 , - 1 , - 1 ) :
kk = 2 * * j;
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);
def GreaterthanK(N, K) :
global ans
digits = [];
M = N;
while M > 0 :
digits.append(M % 10 );
M / / = 10 ;
digits.reverse()
ans = N;
find( 0 , digits, 0 , K);
return ans;
if __name__ = = "__main__" :
N = 121230 ;
K = 756 ;
print (GreaterthanK(N, K));
|
C#
using System;
using System.Collections.Generic;
public class GFG {
public static int ans = 0;
public static void find( int i, List< int > digits,
int mask, int k)
{
if (i == digits.Count)
return ;
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 ((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);
}
public static int GreaterthanK( int N, int K)
{
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;
Console.WriteLine(GreaterthanK(N, K));
}
}
|
Javascript
let ans = 0;
function find(i, digits, mask, k) {
if (i == digits.length)
return ;
let temp = 0;
let pow_of_10 = 1;
for (let j = digits.length - 1; j >= 0; j--) {
let k = Math.pow(2, j);
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 GreaterthanK(N, K) {
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;
}
let N = 121230;
let K = 756;
console.log(GreaterthanK(N, K) + "<br>" )
|
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
Share your thoughts in the comments
Please Login to comment...