Given an integer num, return three consecutive integers (as a sorted array) that sum to num. If num cannot be expressed as the sum of three consecutive integers, return an empty array.
Example 1:
Input: num = 33 Output: [10,11,12] Explanation: 33 can be expressed as 10 + 11 + 12 = 33. 10, 11, 12 are 3 consecutive integers, so we return [10, 11, 12].
Example 2:
Input: num = 4 Output: [] Explanation: There is no way to express 4 as the sum of 3 consecutive integers.
Constraints:
0 <= num <= 1015When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
To solve this problem using the brute force approach, you systematically test every possible group of three numbers that follow each other in order, checking whether their combined total matches your target number. Since you want three numbers where each is one greater than the previous one, you can just start trying different groups of three until you find the exact group that adds up correctly.
Here's how the algorithm would work step-by-step:
def sum_of_three_consecutive(target_number):
# We set an upper bound because any number past target must sum to more than target
for starting_number in range(target_number):
current_total = starting_number + (starting_number + 1) + (starting_number + 2)
if current_total == target_number:
# Return the sequence immediately once the exact matching sum is found
return [starting_number, starting_number + 1, starting_number + 2]
elif current_total > target_number:
# Stop early since any further numbers will only exceed the target value
break
# Return an empty list if no three consecutive numbers can form the target sum
return []
Instead of guessing combinations, we can use a simple mathematical shortcut based on the relationship between three numbers in a row. By dividing the target number into three equal parts, we can instantly find the middle number and the two numbers directly next to it.
Here's how the algorithm would work step-by-step:
def sum_three_integers(target_number):
# Division checks if the target can be evenly split into three consecutive integers.
if target_number % 3 != 0:
return []
middle_number = target_number // 3
first_number = middle_number - 1
final_number = middle_number + 1
# Combining the sequence values reconstructs the unique consecutive triplet.
return [first_number, middle_number, final_number]| Case | How to Handle |
|---|---|
| Input num is not divisible by 3 | Return an empty array since consecutive integers summing to num cannot exist if num is not a multiple of 3. |
| Negative numbers for num | The mathematical formula handles negative sums seamlessly by returning the correct negative consecutive sequence. |
| Zero as the input num | Returns the valid sequence minus one, zero, and one whose sum equals zero. |
| Extremely large integer values near the 64-bit limit | Use 64-bit integers to prevent arithmetic overflow during division and subtraction operations. |
| Small integer inputs like 1 or 2 | Division yields a non-integer or incorrect sequence, which is caught by checking if num is divisible by 3. |
| Cases where no valid three consecutive integers exist | Return an empty array as explicitly required by the problem statement. |