Find Three Consecutive Integers That Sum to a Given Number - Problem
Given an integer num, your task is to find three consecutive integers that sum to exactly num. Return these three integers as a sorted array.
If it's impossible to express num as the sum of three consecutive integers, return an empty array.
What are consecutive integers? They are integers that follow each other in sequence with a difference of 1. For example: 4, 5, 6 or -2, -1, 0.
Key insight: If three consecutive integers start at x, they are: x, x+1, x+2, and their sum is 3x + 3 = 3(x+1).
Input & Output
example_1.py โ Basic Case
$
Input:
num = 9
โบ
Output:
[2, 3, 4]
๐ก Note:
2 + 3 + 4 = 9, and these are three consecutive integers
example_2.py โ Negative Numbers
$
Input:
num = 0
โบ
Output:
[-1, 0, 1]
๐ก Note:
-1 + 0 + 1 = 0, consecutive integers can include negative numbers
example_3.py โ Impossible Case
$
Input:
num = 1
โบ
Output:
[]
๐ก Note:
No three consecutive integers can sum to 1. The equation (1-3)/3 = -2/3 is not an integer
Constraints
- -109 โค num โค 109
- The three integers must be consecutive
- Return empty array if no solution exists
Visualization
Tap to expand
Understanding the Visualization
1
Identify the pattern
Three consecutive integers: x, x+1, x+2
2
Set up equation
Sum = x + (x+1) + (x+2) = 3x + 3
3
Solve directly
If 3x + 3 = num, then x = (num-3)/3
4
Verify solution
Check if x is an integer (divisibility test)
Key Takeaway
๐ฏ Key Insight: The sum of three consecutive integers starting at x is always 3x+3, allowing us to solve directly with algebra instead of searching
๐ก
Explanation
AI Ready
๐ก Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code