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.
- 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
- 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
- Print first k digits of 1/n where n is a positive integer
- Reverse digits of an integer with overflow handled
- Partition the digits of an integer such that it satisfies a given condition
- Program to count digits in an integer (4 Different Methods)
- 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
- Minimum positive integer divisible by C and is not in range [A, B]
- Minimum positive integer to divide a number such that the result is an odd
- Find the minimum sum of distance to A and B from any integer point in a ring of size N
- 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 firstname.lastname@example.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.