Find Three Consecutive Integers That Sum to a Given Number - Problem

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.

Note: Consecutive integers are integers that follow each other in order without gaps. For example: 1, 2, 3 or -2, -1, 0.

Input & Output

Example 1 — Basic Case
$ Input: num = 33
Output: [10,11,12]
💡 Note: Three consecutive integers 10, 11, 12 sum to 33: 10 + 11 + 12 = 33
Example 2 — Small Number
$ Input: num = 4
Output: []
💡 Note: No three consecutive integers can sum to 4. The minimum sum is 0+1+2=3, next is 1+2+3=6
Example 3 — Negative Numbers
$ Input: num = 0
Output: [-1,0,1]
💡 Note: Three consecutive integers -1, 0, 1 sum to 0: -1 + 0 + 1 = 0

Constraints

  • -1015 ≤ num ≤ 1015

Visualization

Tap to expand
Find Three Consecutive Integers That Sum to 3333Target Sum101112Three Consecutive Integers[10,11,12]Result Array10 + 11 + 12 = 33 ✓
Understanding the Visualization
1
Input
Given target sum num = 33
2
Process
Find x, x+1, x+2 where sum equals target
3
Output
Return [10, 11, 12] or empty array if impossible
Key Takeaway
🎯 Key Insight: Three consecutive integers x, x+1, x+2 sum to 3x+3, so check if (num-3) is divisible by 3
Asked in
Google 15 Amazon 12 Microsoft 8
23.4K Views
Medium Frequency
~15 min Avg. Time
892 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen