LeetCode 2094: Finding 3-Digit Even Numbers Solution
Master LeetCode problem 2094 (Finding 3-Digit Even Numbers), a easy challenge, with our optimized solutions in Java, C++, and Python. Explore detailed explanations, test your code in our interactive editor, and prepare for coding interviews.
2094. Finding 3-Digit Even Numbers
Problem Explanation
Explanation
To solve this problem, we can generate all possible 3-digit numbers by permuting the given digits and check if each number is even. We can use a recursive approach to generate all permutations. For each permutation, we check if the number is even and has 3 digits. If it meets these conditions, we add it to our result set.
-
Algorithm:
- Generate all permutations of 3 digits from the input array.
- For each permutation, check if it forms a 3-digit even number without leading zeros.
- Add the valid numbers to a set to ensure uniqueness.
- Return the sorted list of unique even numbers.
-
Time Complexity: The time complexity of this approach is O(n!) where n is the number of digits in the input array. Generating all permutations of the digits will take factorial time complexity.
-
Space Complexity: The space complexity is O(n) to store the set of valid 3-digit even numbers.
Solution Code
import java.util.*;
class Solution {
public List<Integer> findNumbers(int[] digits) {
Set<Integer> result = new HashSet<>();
boolean[] visited = new boolean[digits.length];
generatePermutations(digits, new ArrayList<>(), visited, result);
List<Integer> resultList = new ArrayList<>(result);
Collections.sort(resultList);
return resultList;
}
private void generatePermutations(int[] digits, List<Integer> currNum, boolean[] visited, Set<Integer> result) {
if (currNum.size() == 3) {
int num = currNum.get(0) * 100 + currNum.get(1) * 10 + currNum.get(2);
if (num % 2 == 0) {
result.add(num);
}
return;
}
for (int i = 0; i < digits.length; i++) {
if (!visited[i]) {
visited[i] = true;
currNum.add(digits[i]);
generatePermutations(digits, currNum, visited, result);
currNum.remove(currNum.size() - 1);
visited[i] = false;
}
}
}
}Try It Yourself
Loading code editor...
Related LeetCode Problems
Frequently Asked Questions
How to solve LeetCode 2094 (Finding 3-Digit Even Numbers)?
This page provides optimized solutions for LeetCode problem 2094 (Finding 3-Digit Even Numbers) in Java, C++, and Python, along with a detailed explanation and an interactive code editor to test your code.
What is the time complexity of LeetCode 2094 (Finding 3-Digit Even Numbers)?
The time complexity for LeetCode 2094 (Finding 3-Digit Even Numbers) varies by solution. Check the detailed explanation section for specific complexities in Java, C++, and Python implementations.
Can I run code for LeetCode 2094 on DevExCode?
Yes, DevExCode provides an interactive code editor where you can write, test, and run your code for LeetCode 2094 in Java, C++, or Python.