Largest even number possible by using one swap operation in given number Last Updated : 15 Sep, 2023 Comments Improve Suggest changes Like Article Like Report Try it on GfG Practice 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: 1235785Output: 1535782Explanation: Swap 2 and 5. Input: 536425Output: 536524Explanation: Swap 4 and 5 to make the largest even number. Recommended PracticeOdd to EvenTry It!Find the first even number less than or equal to the odd number at the 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. Implementation: C++ // C++ program for above implementation #include <bits/stdc++.h> using namespace std; // Make the largest even number string makeEven(string& str) { int n = str.length(); int even = INT_MAX, index; // Start traversing the string for (int i = 0; i < n - 1; i++) { // Find the even number if ((str[i] - '0') % 2 == 0) { even = (str[i] - '0'); index = i; } // Check if current even is equal to // or less than the odd number if (even <= (str[n - 1] - '0')) break; } // Return original string if there is no // even value if (even == INT_MAX) return str; // Swap even and odd value swap(str[index], str[n - 1]); return str; } // Driver code int main() { string str = "1356425"; cout << makeEven(str); return 0; } Java // Java program for above implementation class GFG { // Make the largest even number static char[] makeEven(String string) { char[] str = string.toCharArray(); int n = str.length; int even = Integer.MAX_VALUE, index = 0; // Start traversing the String for (int i = 0; i < n - 1; i++) { // Find the even number if ((str[i] - '0') % 2 == 0) { even = (str[i] - '0'); index = i; } // Check if current even is equal to // or less than the odd number if (even <= (str[n - 1] - '0')) { break; } } // Return original String if there is no // even value if (even == Integer.MAX_VALUE) { return str; } // Swap even and odd value swap(str, index, n - 1); return str; } static void swap(char[] str, int index1, int index2) { char temp = str[index1]; str[index1] = str[index2]; str[index2] = temp; } // Driver code public static void main(String[] args) { String str = "1356425"; System.out.print(makeEven(str)); } } /*This code is contributed by PrinciRaj1992*/ Python3 # Python3 code for the above implementation import sys # Make the largest even number def makeEven(arr, n): # index to first even no,if any first_e_i = -1 # index to last even no, if any last_e_i = -1 # index to last no last_n_i = n - 1 # Start traversing the String for i in range(n): # if it finds any first even no less # than last digit then break the loop if (int(arr[i]) % 2 == 0 and int(arr[i]) < int(arr[last_n_i])): first_e_i = i break # it finds last even no if int(arr[i]) % 2 == 0: last_e_i = i if first_e_i != -1: # swap even and odd value (arr[first_e_i], arr[last_n_i]) = (arr[last_n_i], arr[first_e_i]) return arr if first_e_i == -1 and last_e_i != -1: # swap even and odd value (arr[last_e_i], arr[last_n_i]) = (arr[last_n_i], arr[last_e_i]) return arr # Return original String if there is # no even number return arr # Driver Code if __name__ == '__main__': string = "1356425" result = "".join(makeEven(list(string), len(list(string)))) print(result) # This code is contributed # by Vikash Kumar 37 C# // C# implementation of above approach using System; public class GFG { // Make the largest even number static char[] makeEven(String str1) { char[] str = str1.ToCharArray(); int n = str.Length; int even = int.MaxValue, index = 0; // Start traversing the String for (int i = 0; i < n - 1; i++) { // Find the even number if ((str[i] - '0') % 2 == 0) { even = (str[i] - '0'); index = i; } // Check if current even is equal to // or less than the odd number if (even <= (str[n - 1] - '0')) { break; } } // Return original String if there is no // even value if (even == int.MaxValue) { return str; } // Swap even and odd value swap(str, index, n - 1); return str; } static void swap(char[] str, int index1, int index2) { char temp = str[index1]; str[index1] = str[index2]; str[index2] = temp; } // Driver code public static void Main() { String str = "1356425"; Console.WriteLine(makeEven(str)); } } /*This code is contributed by PrinciRaj1992*/ PHP <?php // PHP program for above implementation // Make the largest even number function makeEven($str) { $n = strlen($str); $even = PHP_INT_MAX; $index; // Start traversing the string for ($i = 0; $i < $n - 1; $i++) { // Find the even number if (($str[$i] - '0') % 2 == 0) { $even = ($str[$i] - '0'); $index = $i; } // Check if current even is // equal to or less than // the odd number if ($even <= ($str[$n - 1] - '0')) break; } // Return original string if // there is no even value if ($even == PHP_INT_MAX) return $str; //swap the number list($str[$index], $str[$n - 1]) = array($str[$n - 1], $str[$index]); return $str; } // Driver code $str = "1356425"; echo makeEven($str); // This code is contributed by ajit ?> JavaScript <script> // Javascript program for above implementation // Make the largest even number function makeEven(string) { let str = string.split(''); let n = str.length; let even = Number.MAX_VALUE, index = 0; // Start traversing the String for (let i = 0; i < n - 1; i++) { // Find the even number if ((str[i].charCodeAt() - '0'.charCodeAt()) % 2 == 0) { even = (str[i] - '0'); index = i; } // Check if current even is equal to // or less than the odd number if (even <= (str[n-1].charCodeAt() - '0'.charCodeAt())) { break; } } // Return original String if there is no // even value if (even == Number.MAX_VALUE) { return str; } // Swap even and odd value swap(str,index, n - 1); return str.join(""); } function swap(str, index1, index2) { let temp = str[index1]; str[index1] = str[index2]; str[index2] =temp; } let str = "1356425"; document.write(makeEven(str)); </script> Output1356524 Time Complexity: O(N)Auxiliary Space: O(1) Comment More infoAdvertise with us Next Article Largest even number possible by using one swap operation in given number S Sahil Chhabra Improve Article Tags : Strings DSA Morgan Stanley large-numbers Practice Tags : Morgan StanleyStrings Similar Reads Largest smaller number possible using only one swap operation Given a non-negative number N in the form of string. The task is to apply at most one swap operation on the number N so that the resultant is smaller than N and is the largest such number. Examples: Input :str = "12435" Output : 12345 Although the number 12354 will be the largest smaller number from 9 min read Form the largest number using at most one swap operation Given a non-negative number num. The problem is to apply at most one swap operation on the number num so that the resultant is the largest possible number. The number could be very large so a string type can be used to store the number. Examples: Input : n = 8725634 Output : 8765234 Swapped the digi 9 min read Next higher number using atmost one swap operation Given a non-negative number num. The problem is to find the smallest number greater than num by performing atmost on swap operation between any two digits in num. If no larger number can be formed then print "Not Possible".The number could be very large and may not even fit into long long int. Examp 11 min read Largest number by rearranging digits of a given positive or negative number Given an integer N(positive or negative), the task is to find the maximum number that can be formed using all of the digits of this number. Examples: Input: -38290367Output: -20336789Explanation: As there is need to use all the digits, 0 cannot be the first digit because it becomes redundant at firs 7 min read Form the smallest number using at most one swap operation Given a non-negative number num. The problem is to apply at most one swap operation on the number num so that the resultant is the smallest possible number. The number could be very large so a string type can be used to store the number. The input does not contain leading 0's and the output should a 12 min read Like