Find Three Consecutive Integers That Sum to a Given Number - Solution & Explanation
Problem Statement
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 <= 1015
Approach Overview
Problem 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 1: Algebraic Approach
This approach utilizes the property of consecutive integers. If we let the three consecutive integers be x, x+1, x+2, their sum is 3x + 3. To find these integers, we set up the equation: 3x + 3 = num. Solve this equation for x to see if it results in an integer value, as the integers are required to be consecutive.
The code solves the sum of three consecutive integers using algebra. After checking the divisibility condition, it calculates the smallest integer x and constructs the consecutive numbers.
Complexity
Time Complexity: O(1)
Space Complexity: O(1)
Approach 2: Modulo Check and Integer Division
This approach also involves understanding the sum of consecutive integers: (x) + (x + 1) + (x + 2) = num, simplifying to 3x + 3 = num. Modify the equation slightly to 3x = num - 3. Check if num-3 is divisible by 3 and apply integer division to find x.
This C code checks if num can be divided evenly by 3 and computes the middle integer as the average of the sum.
Complexity
Time Complexity: O(1)
Space Complexity: O(1)
Approach 3: Mathematics
Assume the three consecutive integers are x-1, x, and x+1. Their sum is 3x, so num must be a multiple of 3. If num is not a multiple of 3, it cannot be represented as the sum of three consecutive integers, and we return an empty array. Otherwise, let x = \frac{num}{3}, then x-1, x, and x+1 are the three consecutive integers whose sum is num.
The time complexity is O(1), and the space complexity is O(1).
Code
Python
Java
C++
Go
TypeScript
Rust
JavaScript
Complexity Comparison
| Approach | Complexity |
|---|---|
| Algebraic Approach | Time Complexity: O(1) |
| Modulo Check and Integer Division | Time Complexity: O(1) |
| Mathematics | — |
Detailed Complexity Analysis
| 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. |
Video Solution
Find Three Consecutive Integers That Sum to a Given Number | 2177 LeetCode | BiWeekly Contest 72 • CodeWithSunny • 622 views views
Watch 9 more video solutions →Frequently Asked Questions
Is Find Three Consecutive Integers That Sum to a Given Number easy or hard?
Find Three Consecutive Integers That Sum to a Given Number Python/Java solution
How to solve Find Three Consecutive Integers That Sum to a Given Number in O(1)?
What is the best approach for Find Three Consecutive Integers That Sum to a Given Number?
Is Find Three Consecutive Integers That Sum to a Given Number asked at Google/Amazon/Meta?
What data structure is used in Find Three Consecutive Integers That Sum to a Given Number?
What is the time complexity of Find Three Consecutive Integers That Sum to a Given Number?
Ready to solve this problem?
Practice Find Three Consecutive Integers That Sum to a Given Number with our built-in code editor and test cases.
Practice on FleetCode