3483. Unique 3-Digit Even Numbers LeetCode Solution
In this guide, you will get 3483. Unique 3-Digit Even Numbers LeetCode Solution with the best time and space complexity. The solution to Unique -Digit Even Numbers problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.
Table of Contents
- Problem Statement
- Complexity Analysis
- Unique -Digit Even Numbers solution in C++
- Unique -Digit Even Numbers solution in Java
- Unique -Digit Even Numbers solution in Python
- Additional Resources
Problem Statement of Unique -Digit Even Numbers
You are given an array of digits called digits. Your task is to determine the number of distinct three-digit even numbers that can be formed using these digits.
Note: Each copy of a digit can only be used once per number, and there may not be leading zeros.
Example 1:
Input: digits = [1,2,3,4]
Output: 12
Explanation: The 12 distinct 3-digit even numbers that can be formed are 124, 132, 134, 142, 214, 234, 312, 314, 324, 342, 412, and 432. Note that 222 cannot be formed because there is only 1 copy of the digit 2.
Example 2:
Input: digits = [0,2,2]
Output: 2
Explanation: The only 3-digit even numbers that can be formed are 202 and 220. Note that the digit 2 can be used twice because it appears twice in the array.
Example 3:
Input: digits = [6,6,6]
Output: 1
Explanation: Only 666 can be formed.
Example 4:
Input: digits = [1,3,5]
Output: 0
Explanation: No even 3-digit numbers can be formed.
Constraints:
3 <= digits.length <= 10
0 <= digits[i] <= 9
Complexity Analysis
- Time Complexity: O(n^3)
- Space Complexity: O(n^3)
3483. Unique 3-Digit Even Numbers LeetCode Solution in C++
class Solution {
public:
int totalNumbers(vector<int>& digits) {
unordered_set<int> nums;
vector<int> perm = digits;
ranges::sort(perm);
do {
const int a = perm[0];
const int b = perm[1];
const int c = perm[2];
if (a != 0 && c % 2 == 0)
nums.insert(a * 100 + b * 10 + c);
} while (next_permutation(perm.begin(), perm.end()));
return nums.size();
}
};
/* code provided by PROGIEZ */
3483. Unique 3-Digit Even Numbers LeetCode Solution in Java
class Solution {
public int totalNumbers(int[] digits) {
Set<Integer> nums = new HashSet<>();
int[] perm = digits.clone();
Arrays.sort(perm);
do {
final int a = perm[0];
final int b = perm[1];
final int c = perm[2];
if (a != 0 && c % 2 == 0) {
nums.add(a * 100 + b * 10 + c);
}
} while (nextPermutation(perm));
return nums.size();
}
private boolean nextPermutation(int[] nums) {
final int n = nums.length;
// From the back to the front, find the first num < nums[i + 1].
int i;
for (i = n - 2; i >= 0; --i)
if (nums[i] < nums[i + 1])
break;
if (i < 0)
return false;
// From the back to the front, find the first num > nums[i] and swap it with
// nums[i].
for (int j = n - 1; j > i; --j)
if (nums[j] > nums[i]) {
swap(nums, i, j);
break;
}
// Reverse nums[i + 1..n - 1].
reverse(nums, i + 1, n - 1);
return true;
}
private void reverse(int[] nums, int l, int r) {
while (l < r)
swap(nums, l++, r--);
}
private void swap(int[] nums, int i, int j) {
final int temp = nums[i];
nums[i] = nums[j];
nums[j] = temp;
}
}
// code provided by PROGIEZ
3483. Unique 3-Digit Even Numbers LeetCode Solution in Python
class Solution:
def totalNumbers(self, digits: list[int]) -> int:
nums = set()
for a, b, c in itertools.permutations(digits, 3):
if a != 0 and c % 2 == 0:
nums.add(a * 100 + b * 10 + c)
return len(nums)
# code by PROGIEZ
Additional Resources
- Explore all LeetCode problem solutions at Progiez here
- Explore all problems on LeetCode website here
Happy Coding! Keep following PROGIEZ for more updates and solutions.