Find Three Consecutive Integers That Sum to a Given Number solution and explanation
9 more videos on this problem
Find Three Consecutive Integers That Sum to a Given Number | 2177 LeetCode | BiWeekly Contest 72CodeWithSunny · 5:09 · 622 views
2177 Find Three Consecutive Integers That Sum to a Given Number | Biweekly Contest 72 Leetcode 2176Bro Coders · 3:01 · 452 views
2177. Find Three Consecutive Integers That Sum to a Given Number | PythonLea's coding · 1:35 · 272 views
2177. Find Three Consecutive Integers That Sum to a Given NumberKai Mai · 4:05 · 168 views
2177. Find Three Consecutive Integers That Sum to a Given Number (Leetcode Medium)Programming Live with Larry · 2:55 · 144 views
Find Three Consecutive Integers That Sum to a Given Number || LeetCode Biweekly 72 c++Source Code · 0:24 · 92 views
2177. Find Three Consecutive Integers That Sum to a Given Number - Using PythonLeetcode and Interview Question · 3:24 · 84 views
Find Three Consecutive Integers That Sum to a Given Number - LeetCode 2177 - Java Solutionalgobest - Computer Science · 2:09 · 83 views
2177 Find Three Consecutive Integers That Sum to a Given NumberMath Geeks · 1:22 · 77 views
2177. Find Three Consecutive Integers That Sum to a Given Number | Short Code | LeetCode Biweekly 72ARM Preparation · 2:42 · 47 views
The 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.
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.
def sum_of_three(num):
if (num - 3) % 3 != 0:
return []
x = (num - 3) // 3
return [x, x + 1, x + 2]
print(sum_of_three(33))import java.util.*;
public class Solution {
public static List<Long> sumOfThree(long num) {
if ((num - 3) % 3 != 0) {
return Collections.emptyList();
}
long x = (num - 3) / 3;
return Arrays.asList(x, x + 1, x + 2);
}
public static void main(String[] args) {
List<Long> result = sumOfThree(33);
System.out.println(result);
}
}#include <vector>
#include <iostream>
std::vector<long long> sumOfThree(long long num) {
if ((num - 3) % 3 != 0) {
return {};
}
long long x = (num - 3) / 3;
return {x, x + 1, x + 2};
}
int main() {
auto result = sumOfThree(33);
if (result.empty()) {
std::cout << "[]" << std::endl;
} else {
std::cout << "[" << result[0] << ", " << result[1] << ", " << result[2] << "]" << std::endl;
}
return 0;
}function sumOfThree(num) {
if ((num - 3) % 3 !== 0) {
return [];
}
const x = (num - 3) / 3;
return [x, x + 1, x + 2];
}
console.log(sumOfThree(33));#include <stdio.h>
int* sumOfThree(int num, int* returnSize) {
if ((num - 3) % 3 != 0) {
*returnSize = 0;
return NULL;
}
int x = (num - 3) / 3;
*returnSize = 3;
int* result = (int*)malloc((*returnSize) * sizeof(int));
result[0] = x;
result[1] = x + 1;
result[2] = x + 2;
return result;
}
int main() {
int returnSize;
int* result = sumOfThree(33, &returnSize);
if (returnSize == 0) {
printf("[]\n");
} else {
printf("[%d, %d, %d]\n", result[0], result[1], result[2]);
free(result);
}
return 0;
}using System;
using System.Collections.Generic;
public class Solution {
public static List<long> SumOfThree(long num) {
if ((num - 3) % 3 != 0) {
return new List<long>();
}
long x = (num - 3) / 3;
return new List<long> { x, x + 1, x + 2 };
}
public static void Main() {
List<long> result = SumOfThree(33);
Console.WriteLine(String.Join(", ", result));
}
}The Python solution computes whether the adjusted number (num - 3) divides evenly by 3, then uses it to construct the solution.
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.
def find_three_consecutive(num):
if num % 3 != 0:
return []
middle = num // 3
return [middle - 1, middle, middle + 1]
print(find_three_consecutive(33))import java.util.*;
public class Solution {
public static List<Long> findThreeConsecutive(long num) {
if (num % 3 != 0) {
return Collections.emptyList();
}
long middle = num / 3;
return Arrays.asList(middle - 1, middle, middle + 1);
}
public static void main(String[] args) {
List<Long> result = findThreeConsecutive(33);
System.out.println(result);
}
}#include <vector>
#include <iostream>
std::vector<long long> findThreeConsecutive(long long num) {
if (num % 3 != 0) {
return {};
}
long long middle = num / 3;
return {middle - 1, middle, middle + 1};
}
int main() {
auto result = findThreeConsecutive(33);
if (result.empty()) {
std::cout << "[]" << std::endl;
} else {
std::cout << "[" << result[0] << ", " << result[1] << ", " << result[2] << "]" << std::endl;
}
return 0;
}function findThreeConsecutive(num) {
if (num % 3 !== 0) {
return [];
}
const middle = num / 3;
return [middle - 1, middle, middle + 1];
}
console.log(findThreeConsecutive(33));#include <stdio.h>
int* findThreeConsecutive(int num, int* returnSize) {
if (num % 3 != 0) {
*returnSize = 0;
return NULL;
}
int middle = num / 3;
int* result = (int*)malloc(3 * sizeof(int));
result[0] = middle - 1;
result[1] = middle;
result[2] = middle + 1;
*returnSize = 3;
return result;
}
int main() {
int returnSize;
int* result = findThreeConsecutive(33, &returnSize);
if (returnSize == 0) {
printf("[]\n");
} else {
printf("[%d, %d, %d]\n", result[0], result[1], result[2]);
free(result);
}
return 0;
}using System;
using System.Collections.Generic;
public class Solution {
public static List<long> FindThreeConsecutive(long num) {
if (num % 3 != 0) {
return new List<long>();
}
long middle = num / 3;
return new List<long> { middle - 1, middle, middle + 1 };
}
public static void Main() {
List<long> result = FindThreeConsecutive(33);
Console.WriteLine(String.Join(", ", result));
}
}Python's approach uses integer division after checking if num is evenly divisible, then calculates the integers.
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).
function sumOfThree(num: number): number[] {
if (num % 3) {
return [];
}
const x = Math.floor(num / 3);
return [x - 1, x, x + 1];
}func sumOfThree(num int64) []int64 {
if num%3 != 0 {
return []int64{}
}
x := num / 3
return []int64{x - 1, x, x + 1}
}impl Solution {
pub fn sum_of_three(num: i64) -> Vec<i64> {
if num % 3 != 0 {
return Vec::new();
}
let x = num / 3;
vec![x - 1, x, x + 1]
}
}Complexity comparison
| Approach | Complexity |
|---|---|
| Algebraic Approach | Time Complexity: O(1) |
| Modulo Check and Integer Division | Time Complexity: O(1) |
| Mathematics | – |
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. |
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.
