VOOZH about

URL: https://www.geeksforgeeks.org/dsa/largest-even-number-possible-using-one-swap-operation-given-number/

⇱ Largest even number possible by using one swap operation in given number - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Largest even number possible by using one swap operation in given number

Last Updated : 15 Sep, 2023

Given an odd number in the form of a string, the task is to make the largest even number from the given number, and you are allowed to do only one swap operation.

Examples : 

Input: 1235785
Output: 1535782
Explanation: Swap 2 and 5.

Input: 536425
Output:  536524
Explanation: Swap 4 and 5 to make the largest even number.

  1. Find the first even number less than or equal to the odd number at the last index.
  2. If found, swap both values. Else swap with the last even value in the string.
  3. If not possible to make even, print the given string.

Implementation:


Output
1356524

Time Complexity: O(N)
Auxiliary Space: O(1)

Comment
Article Tags: