VOOZH about

URL: https://www.geeksforgeeks.org/dsa/find-the-maximum-number-formed-by-swapping-digits-of-same-parity/

⇱ Find the maximum number formed by swapping digits of same parity - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Find the maximum number formed by swapping digits of same parity

Last Updated : 1 Jul, 2022

Given a number N, the task is to maximize this number by following the given conditions:

  • The odd digit of the number can only be swapped by any odd digit present in the given number.
  • The even digit of the number can only be swapped by any even digit present in the given number.

Examples:

Input: N = 234
Output: 432
Explanation: The 0th index is swapped with 2nd index.

Input: N = 6587
Output: 8765
Explanation: The 0th index is swapped with 2nd index.
The 1st index is swapped with 3rd index.

Naive approach: If a digit in the given number N is even then find the greatest element to its right which is also even and finally swap both. similarly do the same, if the digit is odd.

Follow the steps mentioned below to implement the idea:

  • Convert the given number N into string s (This will make traversing on each digit of the number easy)
  • Iterate the string from 0 to s.length()-1:
    • Use variables to store the maximum value to the right of current index (say maxi) and its position (say idx).
    • Iterate the string from j = i+1 to s.length()-1
      • If both ith digit and jth digit is of the same parity and jth digit is greater than ith, then update the maxi and idx.
      • Otherwise, continue the iteration.
    • Finally swap s[i] with s[idx]
  • Return the integer value of the string s.

Below is the implementation of the above approach.


Output
8765

Time Complexity: O(N2), Where N is the length of the given string.
Auxiliary Space: O(N)

Efficient approach: This problem can be solved efficiently based on the following idea:

Store all even digits in non-increasing order and do the same for odd digits. 
Now replace all stored even digits of given number in non-increasing order with even digits in it and do the same for odd digits.

Follow the steps mentioned below to implement the idea.

  • Convert given number N into string s.
  • Iterate over s and do the following:
    • Store all even digits in a string (say evenDigit) and all odd digits in another string (say oddDigit).
    • Sort both the strings in non-increasing order.
  • Iterate over s and do the following:
    • Use two iterators (say itr1 and itr2) to point to the next even or odd digit to be picked.
    • If the digit in s is even, then replace it with the digit in evenDigit[itr1] and increment itr1. 
    • If the digit in s is odd, then replace it with the digit in oddDigit[itr2] and increment itr2.
  • Finally, convert the string s into an integer and return it.

Below is the implementation of the above approach.


Output
8765

Time Complexity: O(M * log M), Where M is the number of digits present in N.
Auxiliary Space: O(M)

Comment
Article Tags: