Given a long integer, return the smallest(magnitude) integer permutation of that number.
Input : 5468001 Output : 1004568 Input : 5341 Output : 1345
Question Source : GE digital Interview Experience | Set 6
We have already discussed a solution in below post.
In this post, a different approach is discussed.
Approach : As number is long, store the number as string, sort the string, if there is no leading zero, return this string, if there is any leading zero, swap first element of string with first non-zero element of string, and return the string.
Below is the implementation of above approach :
Since character set is limited (‘0’ to ‘9’), we can write our own sort method that works in linear time (by counting frequencies of all characters)
This article is contributed by Mandeep Singh. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Lexicographically smallest permutation with no digits at Original Index
- Smallest non-zero substring which has any permutation divisible by 2^K
- Lexicographically smallest permutation of size A having B integers exceeding all preceeding integers
- Print the last character of lexicographically smallest non-palindromic permutation of a string
- Find smallest number with given number of digits and sum of digits under given constraints
- Find a permutation of 2N numbers such that the result of given expression is exactly 2K
- Find permutation of first N natural numbers that satisfies the given condition
- Find the smallest number whose digits multiply to a given number n
- Find smallest number with given number of digits and sum of digits
- Find smallest possible Number from a given large Number with same count of digits
- Find smallest number formed by inverting digits of given number N
- Find a permutation such that number of indices for which gcd(p[i], i) > 1 is exactly K
- Find the number of sub arrays in the permutation of first N natural numbers such that their median is M
- Given a number, find the next smallest palindrome
- Find smallest number n such that n XOR n+1 equals to given k.
- Find the smallest positive number which can not be represented by given digits
- Find permutation of n which is divisible by 3 but not divisible by 6
- Find the good permutation of first N natural numbers
- Find permutation with maximum remainder Sum
- Find the permutation of first N natural numbers such that sum of i % Pi is maximum possible