Skip to main content

Find Three Consecutive Integers That Sum to a Given Number - Solution & Explanation

MediumMathSimulation14 min readAsked at: Fpt
Practice this problem

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.

Code

C

C++

Java

Python

C#

JavaScript

Complexity

Time Complexity: O(1)
Space Complexity: O(1)

Try this approach in the editor →

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.

Code

C

C++

Java

Python

C#

JavaScript

Complexity

Time Complexity: O(1)
Space Complexity: O(1)

Try this approach in the editor →

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

Try this approach in the editor →

Complexity Comparison

ApproachComplexity
Algebraic Approach

Time Complexity: O(1)
Space Complexity: O(1)

Modulo Check and Integer Division

Time Complexity: O(1)
Space Complexity: O(1)

Mathematics—

Detailed Complexity Analysis

ApproachTimeSpaceWhen to Use
Algebraic ApproachO(1)O(1)Best approach. Direct mathematical insight using the formula for consecutive integers.
Modulo Check and Integer DivisionO(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?
LeetCode classifies this problem as Medium, but the difficulty mainly comes from spotting the mathematical relationship. Once you realize the sum of three consecutive integers equals 3x, the solution becomes a straightforward constant-time calculation.
Find Three Consecutive Integers That Sum to a Given Number Python/Java solution
In Python or Java, compute mid = num // 3 after verifying num % 3 == 0. Return [mid - 1, mid, mid + 1]. If the modulo check fails, return an empty list or array. The implementation is only a few lines and runs in O(1) time.
How to solve Find Three Consecutive Integers That Sum to a Given Number in O(1)?
Check whether the given number is divisible by 3 using num % 3. If it is not divisible, no valid triple exists. If it is divisible, compute mid = num / 3 and return [mid - 1, mid, mid + 1]. The computation uses constant arithmetic operations, giving O(1) time complexity.
What is the best approach for Find Three Consecutive Integers That Sum to a Given Number?
The optimal approach uses an algebraic observation. Three consecutive integers can be written as x-1, x, and x+1, whose sum equals 3x. If num is divisible by 3, the middle integer is num/3 and the result is [x-1, x, x+1]. This solution runs in O(1) time and O(1) space.
Is Find Three Consecutive Integers That Sum to a Given Number asked at Google/Amazon/Meta?
This problem represents a common interview pattern where mathematical properties simplify brute-force thinking. Similar math-based reasoning questions appear in interviews at companies like Amazon, Google, and Microsoft, especially for testing pattern recognition and number manipulation.
What data structure is used in Find Three Consecutive Integers That Sum to a Given Number?
No complex data structure is required. The solution relies on simple arithmetic and returns a small array or list containing three integers. The core concept comes from mathematical reasoning rather than data structure manipulation.
What is the time complexity of Find Three Consecutive Integers That Sum to a Given Number?
The optimal solution runs in O(1) time because it performs only a modulo check and constant arithmetic operations. No loops or recursion are required. Space complexity is also O(1) since only three integers are returned.

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