Watch 10 video solutions for Find Three Consecutive Integers That Sum to a Given Number, a medium level problem involving Math, Simulation. This walkthrough by CodeWithSunny has 612 views views. Want to try solving it yourself? Practice on FleetCode or read the detailed text solution.
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 <= 1015Problem Overview: You receive an integer num and must return three consecutive integers whose sum equals that value. If no such integers exist, return an empty array. The key observation is how sums behave when numbers are consecutive.
Approach 1: Algebraic Approach (O(1) time, O(1) space)
Three consecutive integers can be written as x - 1, x, and x + 1. Their sum becomes (x - 1) + x + (x + 1) = 3x. The problem reduces to checking whether the given number is divisible by 3. If num % 3 != 0, no valid triple exists because the sum of three consecutive integers must always be a multiple of 3. When it is divisible, compute the middle value as x = num / 3 and return [x - 1, x, x + 1]. This approach relies purely on algebra and avoids any iteration, making it constant time and space. Problems involving number patterns like this commonly appear under math reasoning.
Approach 2: Modulo Check and Integer Division (O(1) time, O(1) space)
This method implements the same mathematical insight but structures the logic more explicitly for code readability. First check whether num % 3 == 0. If not, return an empty list immediately. If the condition passes, compute the middle integer using integer division: mid = num // 3. The three required numbers are then mid - 1, mid, and mid + 1. This version emphasizes the validation step before constructing the result array. It fits well in implementations where you want clear conditional logic rather than relying solely on algebraic derivation. The process mirrors simple simulation logic but still executes in constant time.
Recommended for interviews: The algebraic observation is what interviewers expect. Recognizing that three consecutive integers sum to 3x immediately reduces the problem to a divisibility check. A brute-force search would show basic understanding but misses the core mathematical pattern. Demonstrating the num % 3 insight shows strong problem‑solving and pattern recognition skills.
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Algebraic Approach | O(1) | O(1) | Best approach. Direct mathematical insight using the formula for consecutive integers. |
| Modulo Check and Integer Division | O(1) | O(1) | When you want explicit validation logic before constructing the result. |