Largest even number that can be formed by any number of swaps
Given an integer N in the form of string, the task is to find the largest even number from the given number when you are allowed to do any number of swaps (swapping the digits of the number). If no even number can be formed then print -1.
Examples:
Input: N = 1324
Output: 4312
Input: N = 135
Output: -1
No even number can be formed using odd digits.
Approach: Sort the string in descending order then we will get the largest number possible with the given digit but it may or may not be an even number. In order to make it even (if it not already), an even digit from the number must be swapped with the last digit and in order to maximize the even number the even digit which is to be swapped must the smallest even digit from the number.
Note that the sorting can be done in linear time using frequency array for the digits of the number as the number of distinct elements that are need to be sorted can be at most 10 in the worst case.
Below is the implementation of the above approach:
C++
// C++ implementation of the approach #include <bits/stdc++.h> using namespace std; const int MAX = 10; // Function to return the maximum // even number that can be formed // with any number of digit swaps string getMaxEven(string str, int len) { // To store the frequencies of // all the digits int freq[MAX] = { 0 }; // To store the minimum even digit // and the minimum overall digit int i, minEvenDigit = MAX, minDigit = MAX; for (i = 0; i < len; i++) { int digit = str[i] - '0'; freq[digit]++; // If digit is even then update // the minimum even digit if (digit % 2 == 0) minEvenDigit = min(digit, minEvenDigit); // Update the overall minimum digit minDigit = min(digit, minDigit); } // If there is no even digit then // it is not possible to generate // an even number with swaps if (minEvenDigit == MAX) return "-1"; // Decrease the frequency of the // digits that need to be swapped freq[minEvenDigit]--; freq[minDigit]--; i = 0; // Take every digit starting from the maximum // in order to maximize the number for (int j = MAX - 1; j >= 0; j--) { // Take current digit number of times // it appeared in the original number for (int k = 0; k < freq[j]; k++) str[i++] = (char)(j + '0'); // If current digit equals to the // minimum even digit then one instance of it // needs to be swapped with the minimum overall digit // i.e. append the minimum digit here if (j == minEvenDigit) str[i++] = (char)(minDigit + '0'); } // Append once instance of the minimum // even digit in the end to make the number even str[i] = (char)(minEvenDigit + '0'); return str; } // Driver code int main() { string str = "1023422"; int len = str.length(); cout << getMaxEven(str, len); return 0; } |
Java
// Java implementation of the approach class GFG { static int MAX = 10; // Function to return the maximum // even number that can be formed // with any number of digit swaps static String getMaxEven(char[] str, int len) { // To store the frequencies of // all the digits int []freq = new int[MAX]; // To store the minimum even digit // and the minimum overall digit int i, minEvenDigit = MAX, minDigit = MAX; for (i = 0; i < len; i++) { int digit = str[i] - '0'; freq[digit]++; // If digit is even then update // the minimum even digit if (digit % 2 == 0) minEvenDigit = Math.min(digit, minEvenDigit); // Update the overall minimum digit minDigit = Math.min(digit, minDigit); } // If there is no even digit then // it is not possible to generate // an even number with swaps if (minEvenDigit == MAX) return "-1"; // Decrease the frequency of the // digits that need to be swapped freq[minEvenDigit]--; freq[minDigit]--; i = 0; // Take every digit starting from the maximum // in order to maximize the number for (int j = MAX - 1; j >= 0; j--) { // Take current digit number of times // it appeared in the original number for (int k = 0; k < freq[j]; k++) str[i++] = (char)(j + '0'); // If current digit equals to the // minimum even digit then one instance of it // needs to be swapped with the minimum overall digit // i.e. append the minimum digit here if (j == minEvenDigit) str[i++] = (char)(minDigit + '0'); } // Append once instance of the minimum // even digit in the end to make the number even str[i-1] = (char)(minEvenDigit + '0'); return String.valueOf(str); } // Driver code public static void main(String[] args) { char[] str = "1023422".toCharArray(); int len = str.length; System.out.println(getMaxEven(str, len)); } } /* This code contributed by PrinciRaj1992 */ |
Python3
# Python3 implementation of the approach MAX = 10 # Function to return the maximum # even number that can be formed # with any number of digit swaps def getMaxEven(string, length) : string = list(string) # To store the frequencies of # all the digits freq = [0]*MAX # To store the minimum even digit # and the minimum overall digit minEvenDigit = MAX; minDigit = MAX; for i in range(length) : digit = ord(string[i]) - ord('0'); freq[digit] += 1; # If digit is even then update # the minimum even digit if (digit % 2 == 0) : minEvenDigit = min(digit, minEvenDigit); # Update the overall minimum digit minDigit = min(digit, minDigit); # If there is no even digit then # it is not possible to generate # an even number with swaps if (minEvenDigit == MAX) : return "-1"; # Decrease the frequency of the # digits that need to be swapped freq[minEvenDigit] -= 1; freq[minDigit] -= 1; i = 0; # Take every digit starting from the maximum # in order to maximize the number for j in range(MAX - 1, -1, -1) : # Take current digit number of times # it appeared in the original number for k in range(freq[j]) : string[i] = chr(j + ord('0')); i += 1 # If current digit equals to the # minimum even digit then one instance of it # needs to be swapped with the minimum overall digit # i.e. append the minimum digit here if (j == minEvenDigit) : string[i] = chr(minDigit + ord('0')); i += 1 # Append once instance of the minimum # even digit in the end to make the number even #string.append(chr(minEvenDigit + ord('0'))); return "".join(string); # Driver code if __name__ == "__main__" : string = "1023422"; length = len(string); print(getMaxEven(string, length)); # This code is contributed by AnkitRai01 |
C#
// C# implementation of the approach using System; class GFG { static int MAX = 10; // Function to return the maximum // even number that can be formed // with any number of digit swaps static String getMaxEven(char[] str, int len) { // To store the frequencies of // all the digits int []freq = new int[MAX]; // To store the minimum even digit // and the minimum overall digit int i, minEvenDigit = MAX, minDigit = MAX; for (i = 0; i < len; i++) { int digit = str[i] - '0'; freq[digit]++; // If digit is even then update // the minimum even digit if (digit % 2 == 0) minEvenDigit = Math.Min(digit, minEvenDigit); // Update the overall minimum digit minDigit = Math.Min(digit, minDigit); } // If there is no even digit then // it is not possible to generate // an even number with swaps if (minEvenDigit == MAX) return "-1"; // Decrease the frequency of the // digits that need to be swapped freq[minEvenDigit]--; freq[minDigit]--; i = 0; // Take every digit starting from the maximum // in order to maximize the number for (int j = MAX - 1; j >= 0; j--) { // Take current digit number of times // it appeared in the original number for (int k = 0; k < freq[j]; k++) str[i++] = (char)(j + '0'); // If current digit equals to the // minimum even digit then one instance of it // needs to be swapped with the minimum overall digit // i.e. append the minimum digit here if (j == minEvenDigit) str[i++] = (char)(minDigit + '0'); } // Append once instance of the minimum // even digit in the end to make the number even str[i-1] = (char)(minEvenDigit + '0'); return String.Join("",str); } // Driver code public static void Main(String[] args) { char[] str = "1023422".ToCharArray(); int len = str.Length; Console.WriteLine(getMaxEven(str, len)); } } // This code has been contributed by 29AjayKumar |
4322210
Time Complexity: O(n)
Recommended Posts:
- Form the largest palindromic number using atmost two swaps
- Find the largest number that can be formed with the given digits
- Find the largest number that can be formed by changing at most K digits
- Find the Largest Cube formed by Deleting minimum Digits from a number
- Find maximum number that can be formed using digits of a given number
- Number of times the largest perfect square number can be subtracted from N
- Minimum number of swaps required to sort an array
- Number of swaps to sort when only adjacent swapping allowed
- Find the largest good number in the divisors of given number N
- Minimum number of adjacent swaps to convert a string into its given anagram
- Largest even number possible by using one swap operation in given number
- Largest number less than N whose each digit is prime number
- Number formed by the rightmost set bit in N
- Number of triangles that can be formed
- Number of triangles that can be formed with given N points
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


