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.
Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.
- 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
- Count of numbers upto N digits formed using digits 0 to K-1 without any adjacent 0s
- Construct a frequency array of digits of the values obtained from x^1, x^2, ........, x^n
- Minimum decrements to make integer A divisible by integer B
- Minimum sum obtained from groups of four elements from the given array
- Minimum sum obtained by choosing N number from given N pairs
- 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
- Reverse digits of an integer with overflow handled
- Program to count digits in an integer (4 Different Methods)
- 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 positive integer divisible by C and is not in range [A, 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
- Minimum positive integer to divide a number such that the result is an odd
- Minimum integer such that it leaves a remainder 1 on dividing with any element from the range [2, 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.