Find largest number smaller than N with same set of digits
Given a number N in the form of string. The task is to find the greatest number that has same set of digits as N and is smaller than N. If it is not possible to find any such numebr, then print “not possible”.
Examples:
Input: N = "218765" Output: 218756 Input: N = "1234" Output: Not Possible Input: N = "262345" Output: 256432
Following are few observations about the greatest number smaller than N:
- If all digits sorted in ascending order, then output is always “Not Possible”. For example, 1234.
- If all digits are sorted in descending order, then we need to swap last two digits. For example, 4321.
- For other cases, we need to process the number from rightmost side (why? because we need to find the greatest of all smaller numbers).
Algorithm:
- Traverse the given number from rightmost digit, keep traversing till you find a digit which is greater than the previously traversed digit. For example, if the input number is “262345”, we stop at 6 because 6 is greater than previous digit 5. If we do not find such a digit, then output is “Not Possible”.
- Now search the right side of above found digit ‘d’ for the greatest digit smaller than ‘d’. For “262345?, the right side of 6 contains “2345”. The greatest digit smaller than 6 is 5.
- Swap the above found two digits, we get “252346” in above example.
- Now sort all digits from position next to ‘d’ to the end of number in descending order. The number that we get after sorting is the output. For above example, we sort digits in bold 252346. We get 256432 which is the previous smaller number for input 262345.</li
Below is the implementation of the above approach:
C++
// C++ implementation of the above approach #include <bits/stdc++.h> using namespace std; // Function to find previous number void findPrevious(string number, int n) { int i, j; // I) Start from the right most digit // and find the first digit that is // smaller than the digit next to it. for (i = n - 1; i > 0; i--) if (number[i] < number[i - 1]) break ; // If no such digit is found // then all digits are in ascending order // means there cannot be a smallest number // with same set of digits if (i == 0) { cout << "Previous number is not possible" ; return ; } // II) Find the greatest digit on // right side of (i-1)'th digit that is // smaller than number[i-1] int x = number[i - 1], greatest = i; for (j = i; j < n; j++) if (number[j] < x && number[j] > number[greatest]) greatest = j; // III) Swap the above found smallest digit with number[i-1] swap(number[greatest], number[i - 1]); // IV) Sort the digits after (i-1) in descending order sort(number.begin() + i, number.begin() + n, greater< char >()); cout << "Greatest smaller number with same set of digits is " << number; return ; } // Driver code int main() { string digits = "262345" ; int n = digits.length(); findPrevious(digits, n); return 0; } |
chevron_right
filter_none
Java
// Java implementation of the above approach import java.util.*; class GFG { // Function to find previous number static void findPrevious( char [] number, int n) { int i, j; // I) Start from the right most digit // and find the first digit that is // smaller than the digit next to it. for (i = n - 1 ; i > 0 ; i--) { if (number[i] < number[i - 1 ]) { break ; } } // If no such digit is found // then all digits are in ascending order // means there cannot be a smallest number // with same set of digits if (i == 0 ) { System.out.print( "Previous number is not possible" ); return ; } // II) Find the greatest digit on // right side of (i-1)'th digit that is // smaller than number[i-1] int x = number[i - 1 ], greatest = i; for (j = i; j < n; j++) { if (number[j] < x && number[j] > number[greatest]) { greatest = j; } } // III) Swap the above found smallest digit with number[i-1] swap(number, greatest, i - 1 ); // IV) Sort the digits after (i-1) in descending order Arrays.sort(number, i, n); reverse(number, i, n - 1 ); System.out.print( "Greatest smaller number with" + "same set of digits is " + String.valueOf(number)); return ; } static String swap( char [] ch, int i, int j) { char temp = ch[i]; ch[i] = ch[j]; ch[j] = temp; return String.valueOf(ch); } static void reverse( char str[], int start, int end) { // Temporary variable to store character char temp; while (start <= end) { // Swapping the first and last character temp = str[start]; str[start] = str[end]; str[end] = temp; start++; end--; } } // Driver code public static void main(String[] args) { String digits = "262345" ; int n = digits.length(); findPrevious(digits.toCharArray(), n); } } // This code has been contributed by 29AjayKumar |
chevron_right
filter_none
Python3
# Python3 implementation of the above approach # Function to find previous number def findPrevious(number, n): # This is necessary as strings # do not support item assignment number = list (number) i, j = - 1 , - 1 # I) Start from the right most digit # and find the first digit that is # smaller than the digit next to it. for i in range (n - 1 , 0 , - 1 ): if number[i] < number[i - 1 ]: break # If no such digit is found # then all digits are in ascending order # means there cannot be a smallest number # with same set of digits if i = = 0 : print ( "Previous number is not possible" ) return x, greatest = number[i - 1 ], i # II) Find the greatest digit on # right side of(i-1)'th digit that is # smaller than number[i-1] for j in range (i, n): if (number[j] < x and number[j] > number[greatest]): greatest = j # III) Swap the above found smallest digit # with number[i-1] (number[greatest], number[i - 1 ]) = (number[i - 1 ], number[greatest]) l = number[i:] del number[i:] # IV) Sort the digits after(i-1) # in descending order l.sort(reverse = True ) number + = l # Again join the list to make it string number = '' . join(number) print ( "Greatest smaller number with" , "same set of digits is" , number) return # Driver Code if __name__ = = "__main__" : digits = "262345" n = len (digits) findPrevious(digits, n) # This code is contributed by sanjeev2552 |
chevron_right
filter_none
C#
// c# implementation of the approach using System; using System.Collections.Generic; class GFG { // Function to find previous number static void findPrevious( char [] number, int n) { int i, j; // I) Start from the right most digit // and find the first digit that is // smaller than the digit next to it. for (i = n - 1; i > 0; i--) { if (number[i] < number[i - 1]) { break ; } } // If no such digit is found // then all digits are in ascending order // means there cannot be a smallest number // with same set of digits if (i == 0) { Console.Write( "Previous number is not possible" ); return ; } // II) Find the greatest digit on // right side of (i-1)'th digit that is // smaller than number[i-1] int x = number[i - 1], greatest = i; for (j = i; j < n; j++) { if (number[j] < x && number[j] > number[greatest]) { greatest = j; } } // III) Swap the above found // smallest digit with number[i-1] swap(number, greatest, i - 1); // IV) Sort the digits after (i-1) in descending order Array.Sort(number, i, n-i); reverse(number, i, n - 1); Console.Write( "Greatest smaller number with" + "same set of digits is " + String.Join( "" ,number)); return ; } static String swap( char [] ch, int i, int j) { char temp = ch[i]; ch[i] = ch[j]; ch[j] = temp; return String.Join( "" ,ch); } static void reverse( char []str, int start, int end) { // Temporary variable to store character char temp; while (start <= end) { // Swapping the first and last character temp = str[start]; str[start] = str[end]; str[end] = temp; start++; end--; } } // Driver code public static void Main(String[] args) { String digits = "262345" ; int n = digits.Length; findPrevious(digits.ToCharArray(), n); } } /* This code contributed by PrinciRaj1992 */ |
chevron_right
filter_none
Output:
Greatest smaller number with same set of digits is 256432
Recommended Posts:
- Largest number smaller than or equal to n and digits in non-decreasing order
- Find the Largest number with given number of digits and sum of digits
- Find the largest number that can be formed by changing at most K digits
- Find the Largest Cube formed by Deleting minimum Digits from a number
- Largest number smaller than or equal to N divisible by K
- Largest smaller number possible using only one swap operation
- Largest number with the given set of N digits that is divisible by 2, 3 and 5
- Largest number with prime digits
- Largest number not greater than N all the digits of which are odd
- Find the largest multiple of 3 from array of digits | Set 2 (In O(n) time and O(1) space)
- Largest number not greater than N which can become prime after rearranging its digits
- Find the closest and smaller tidy number
- Find the average of k digits from the beginning and l digits from the end of the given number
- Find the number of positive integers less than or equal to N that have an odd number of digits
- Find the largest good number in the divisors of given number N
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.