Given an odd number in the form of a string, the task is to make largest even number from the given number and you are allowed to do only one swap operation.
Input : 1235785 Output :1535782 Swap 2 and 5. Input : 536425 Output : 536524 Swap 4 and 5 to make the largest even number.
- Find the first even number less than or equal to the odd number at last index.
- If found, swap both values. Else swap with the last even value in the string.
- If not possible to make even, print the given string.
Time Complexity : O(N)
This article is contributed by Sahil Chhabra. 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 write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
- Largest smaller number possible using only one swap operation
- Form the smallest number using at most one swap operation
- Next higher number using atmost one swap operation
- Largest number with one swap allowed
- Largest even number that can be formed by any number of swaps
- Generate number with given operation and check if it is palindrome
- Transform One String to Another using Minimum Number of Given Operation
- Number of ways to swap two bit of s1 so that bitwise OR of s1 and s2 changes
- Largest subarray with equal number of 0s and 1s
- Largest palindromic number by permuting digits
- Form the largest palindromic number using atmost two swaps
- Given a number as a string, find the number of contiguous subsequences which recursively add up to 9
- Given a number as a string, find the number of contiguous subsequences which recursively add up to 9 | Set 2
- Number of digits to be removed to make a number divisible by 3
- Build Lowest Number by Removing n digits from a given number