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.
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.
- Largest smaller number possible using only one swap operation
- Next higher number using atmost one swap operation
- Form the smallest number using at most one swap operation
- Largest number with one swap allowed
- Largest even number that can be formed by any number of swaps
- Number of ways to swap two bit of s1 so that bitwise OR of s1 and s2 changes
- Generate number with given operation and check if it is palindrome
- Transform One String to Another using Minimum Number of Given Operation
- Lexicographically largest string possible in one swap
- Largest subarray with equal number of 0s and 1s
- Largest palindromic number by permuting digits
- Form the largest palindromic number using atmost two swaps
- Minimum number of distinct powers of 2 required to express a given binary number
- Find smallest possible Number from a given large Number with same count of digits
- Given a number as a string, find the number of contiguous subsequences which recursively add up to 9 | Set 2
- Number of ways to split a binary number such that every part is divisible by 2
- Given a number as a string, find the number of contiguous subsequences which recursively add up to 9
- Check if a large number is divisible by a number which is a power of 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