Skip to main content

Largest Even Number - Solution & Explanation

EasyString4 min read
Practice this problem

Problem Statement

You are given a string s consisting only of the characters '1' and '2'.

You may delete any number of characters from s without changing the order of the remaining characters.

Return the largest possible resultant string that represents an even integer. If there is no such string, return the empty string "".

 

Example 1:

Input: s = "1112"

Output: "1112"

Explanation:

The string already represents the largest possible even number, so no deletions are needed.

Example 2:

Input: s = "221"

Output: "22"

Explanation:

Deleting '1' results in the largest possible even number which is equal to 22.

Example 3:

Input: s = "1"

Output: ""

Explanation:

There is no way to get an even number.

 

Constraints:

  • 1 <= s.length <= 100
  • s consists only of the characters '1' and '2'.

Approach Overview

Problem Overview: You are given a string containing numeric digits. Rearrange the digits to form the largest possible number that is even. If no arrangement produces an even number, return an empty string. The key constraint: the final digit must be even (0, 2, 4, 6, or 8).

Approach 1: Permutation Brute Force (O(n!))

The most direct idea is to generate every permutation of the digits and check which permutations form an even number. For each permutation, verify that the last digit is even and keep track of the maximum numeric value. This guarantees the correct answer because it explores the entire search space. However, generating all permutations takes O(n!) time and O(n) recursion space, which becomes infeasible even for moderately sized strings. This approach mainly helps demonstrate the core constraint: the last digit determines whether the number is even.

Approach 2: Greedy Digit Placement (Sorting) (O(n log n))

A much better strategy uses a greedy observation: to maximize the number, the digits should appear in descending order, but the final digit must be even. First scan the string and collect all digits. Identify the smallest even digit because placing the smallest even digit at the end preserves larger digits in more significant positions. Sort the remaining digits in descending order and append the chosen even digit at the end. Sorting dominates the cost, resulting in O(n log n) time with O(n) space for storing digits. This greedy construction ensures the number remains even while maximizing its overall value.

Approach 3: Counting Digits Optimization (O(n))

Since digits range only from 0–9, you can avoid sorting by counting digit frequencies. Iterate through the string once and maintain a frequency array of size 10. Choose the smallest even digit with a non-zero count for the final position and decrement its count. Then rebuild the number by appending digits from 9 down to 0 according to their frequencies, and finally append the selected even digit. The counting pass and reconstruction both take linear time, giving O(n) time and O(1) auxiliary space. This approach leverages simple string processing and basic greedy reasoning.

Recommended for interviews: The greedy approach is typically expected. It shows that you recognize the key constraint (the last digit controls parity) and can structure the number to maximize higher place values. Mentioning the brute force permutation approach shows problem exploration, but implementing the greedy or counting-based method demonstrates stronger algorithmic thinking and efficient array manipulation.

Solution

Code

Python

Java

C++

Go

TypeScript

Try this approach in the editor →

Detailed Complexity Analysis

ApproachTimeSpaceWhen to Use
Permutation Brute ForceO(n!)O(n)Conceptual baseline to understand the even-digit constraint
Greedy with SortingO(n log n)O(n)General solution when digits must be rearranged for maximum value
Digit Counting (Optimal)O(n)O(1)Best when digits are limited (0–9) and you want linear performance

Video Solution

3798. Largest Even Number (Leetcode Easy) • Programming Live with Larry • 213 views views

Watch 5 more video solutions →

Frequently Asked Questions

Is Largest Even Number easy or hard?
Largest Even Number is generally considered an easy problem. The core insight is recognizing that only the final digit determines whether the number is even. Once that constraint is handled, constructing the maximum value becomes straightforward.
Largest Even Number Python/Java solution
In Python or Java, iterate through the string to count digits, choose the smallest even digit for the last position, and build the remaining digits in descending order. Python commonly uses lists or collections.Counter, while Java uses an int[10] array for digit frequencies.
How to solve Largest Even Number in O(n)?
Use a digit frequency array of size 10. Scan the string once to count occurrences of each digit. Choose the smallest even digit for the final position, reduce its frequency, then rebuild the number by appending digits from 9 down to 0 based on remaining counts. This avoids sorting and runs in linear time.
What is the best approach for Largest Even Number?
The greedy digit placement approach is the most practical. Identify the smallest even digit to place at the last position, then arrange the remaining digits in descending order to maximize the number. This ensures the number remains even while keeping larger digits in higher place values. Time complexity is O(n log n) with sorting, or O(n) using digit counting.
Is Largest Even Number asked at Google/Amazon/Meta?
Variants of digit rearrangement and greedy number construction appear in interviews at large tech companies. The problem tests understanding of digit constraints, greedy ordering, and efficient string or array manipulation rather than advanced data structures.
What data structure is used in Largest Even Number?
Most solutions use simple arrays or strings. The optimal approach uses a fixed-size frequency array (size 10) to count digits. This allows efficient reconstruction of the number without sorting.
What is the time complexity of Largest Even Number?
The optimal solution runs in O(n) time using a frequency count of digits from 0 to 9. After counting digits, reconstruct the number in descending order and place the chosen even digit at the end. A simpler implementation using sorting runs in O(n log n) time.

Ready to solve this problem?

Practice Largest Even Number with our built-in code editor and test cases.

Practice on FleetCode