2094. Finding 3-Digit Even Numbers #1675
-
Topics: You are given an integer array You need to find all the unique integers that follow the given requirements:
For example, if the given Return a sorted array of the unique integers. Example 1:
Example 2:
Example 3:
Constraints:
Hint:
|
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
We need to find all unique 3-digit even numbers that can be formed using the digits from a given array. The numbers must not have leading zeros and must be even. The solution involves checking each possible 3-digit even number to see if it can be formed using the available digits in the given array. Approach
Let's implement this solution in PHP: 2094. Finding 3-Digit Even Numbers <?php
/**
* @param Integer[] $digits
* @return Integer[]
*/
function findEvenNumbers($digits) {
$original_freq = array_count_values($digits);
$result = array();
for ($num = 100; $num <= 998; $num += 2) {
$num_str = (string)$num;
$h = intval($num_str[0]);
$t = intval($num_str[1]);
$u = intval($num_str[2]);
// Check if the hundreds digit is zero
if ($h == 0) {
continue;
}
$candidate_digits = array($h, $t, $u);
$candidate_freq = array_count_values($candidate_digits);
$valid = true;
foreach ($candidate_freq as $digit => $count) {
if (!isset($original_freq[$digit]) || $original_freq[$digit] < $count) {
$valid = false;
break;
}
}
if ($valid) {
array_push($result, $num);
}
}
return $result;
}
// Example usage:
$digits1 = array(2, 1, 3, 0);
print_r(findEvenNumbers($digits1)); // Output: [102, 120, 130, 132, 210, 230, 302, 310, 312, 320]
$digits2 = array(2, 2, 8, 8, 2);
print_r(findEvenNumbers($digits2)); // Output: [222, 228, 282, 288, 822, 828, 882]
$digits3 = array(3, 7, 5);
print_r(findEvenNumbers($digits3)); // Output: []
?> Explanation:
This approach efficiently checks each possible candidate using the frequency map, ensuring that we only consider valid numbers while avoiding duplicates and leading zeros. |
Beta Was this translation helpful? Give feedback.
We need to find all unique 3-digit even numbers that can be formed using the digits from a given array. The numbers must not have leading zeros and must be even. The solution involves checking each possible 3-digit even number to see if it can be formed using the available digits in the given array.
Approach