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.
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.
- Minimum operations required to modify the array such that parity of adjacent elements is different
- Count of all subsequences having adjacent elements with different parity
- Minimum value exceeding X whose count of divisors has different parity with count of divisors of X
- Smallest array that can be obtained by replacing adjacent pairs with their products
- Minimum digits to be removed to make either all digits or alternating digits same
- Program to count digits in an integer (4 Different Methods)
- Smallest number possible by swapping adjacent even odd pairs
- Shuffle the position of each Array element by swapping adjacent elements
- Count numbers which can be represented as sum of same parity primes
- 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
- Count different numbers that can be generated such that there digits sum is equal to 'n'
- Check if array can be sorted by swapping pairs with GCD of set bits count equal to that of the smallest array element
- Check if array can be sorted by swapping pairs having GCD equal to the smallest element in the array
- Minimum decrements to make integer A divisible by integer B
- Numbers of Length N having digits A and B and whose sum of digits contain only digits A and B
- Maximum array sum that can be obtained after exactly k changes
- Total distinct pairs from two arrays such that second number can be obtained by inverting bits of first
- Count of distinct sums that can be obtained by adding prime numbers from given arrays
- Check if N can be obtained from 1 by repetitively multiplying by 10 or 20
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.