Given an integer N, the task is to find the minimum integer that can be obtained from the given integer such that the adjacent digits of different parity can be swapped any no of times.
Two digits of different parity means that they will have different remainders when divided by two.
Input: N = 64432
Swap the 4th and 3rd digit; N = 64342
Swap the 3rd and 2nd digit; N = 63442
Swap the 2nd and 1st digit; N = 36442
Approach: The idea of the approach is to use two stacks to keep the digits of the number.
- In one stack, numbers which are divisible by two can be stored.
- In another stack, numbers which are not divisible by two can be stored.
- The elements are then pushed into both the stacks from reverse order of the number.
- Now, the element from the stack whose top contains the smaller element is popped and concatenated with the answer till one of the stacks is empty.
- The remaining elements of the stack are then concatenated which is not empty with the answer and finally the output is returned.
Below is the implementation of the above approach.
GeeksforGeeks has prepared a complete interview preparation course with premium videos, theory, practice problems, TA support and many more features. Please refer Placement 100 for details
- Minimum operations required to modify the array such that parity of adjacent elements is different
- Find the minimum positive integer such that it is divisible by A and sum of its digits is equal to B
- Smallest number possible by swapping adjacent even odd pairs
- Construct a frequency array of digits of the values obtained from x^1, x^2, ........, x^n
- Minimum sum obtained from groups of four elements from the given array
- Minimum decrements to make integer A divisible by integer B
- Nth positive number whose absolute difference of adjacent digits is at most 1
- Print first k digits of 1/n where n is a positive integer
- Program to count digits in an integer (4 Different Methods)
- Reverse digits of an integer with overflow handled
- Partition the digits of an integer such that it satisfies a given condition
- Minimum inversions required so that no two adjacent elements are same
- Minimum positive integer value possible of X for given A and B in X = P*A + Q*B
- Minimum numbers needed to express every integer below N as a sum
- Count the minimum steps to reach 0 from the given integer 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 email@example.com. 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.