1317. Convert Integer to the Sum of Two No-Zero Integers #2148
-
|
Topics: No-Zero integer is a positive integer that does not contain any Given an integer
The test cases are generated so that there is at least one valid solution. If there are many valid solutions, you can return any of them. Example 1:
Example 2:
Constraints:
Hint:
|
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
|
We need to find two integers Approach
Let's implement this solution in PHP: 1317. Convert Integer to the Sum of Two No-Zero Integers <?php
/**
* @param Integer $n
* @return Integer[]
*/
function getNoZeroIntegers($n) {
for ($i = 1; $i <= $n / 2; $i++) {
if (isNoZero($i) && isNoZero($n - $i)) {
return [$i, $n - $i];
}
}
return [];
}
/**
* @param $num
* @return bool
*/
function isNoZero($num) {
while ($num > 0) {
if ($num % 10 == 0) {
return false;
}
$num = (int)($num / 10);
}
return true;
}
// Test cases
print_r(getNoZeroIntegers(2)); // Output: [1,1]
print_r(getNoZeroIntegers(11)); // Output: [2,9] or [8,3]
print_r(getNoZeroIntegers(101)); // Output: e.g. [2,99]
?>Explanation:
This approach efficiently checks all possible pairs of integers that sum to |
Beta Was this translation helpful? Give feedback.
We need to find two integers
aandbsuch that their sum equals the given integern, and neitheranorbcontains the digit '0' in their decimal representation. The solution involves checking pairs of integers that sum tonuntil we find a pair where both integers are no-zero integers.Approach
ifrom 1 ton/2, check if bothiandn - iare no-zero integers. This range ensures we cover all possible pairs without redundancy.