Overflow in intermediate arithmetic
Wrong move: Temporary multiplications exceed integer bounds.
Usually fails on: Large inputs wrap around unexpectedly.
Fix: Use wider types, modular arithmetic, or rearranged operations.
Move from brute-force thinking to an efficient approach using math strategy.
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 summary: 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.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Math
33
4
longest-consecutive-sequence)number-of-ways-to-buy-pens-and-pencils)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #2177: Find Three Consecutive Integers That Sum to a Given Number
class Solution {
public long[] sumOfThree(long num) {
if (num % 3 != 0) {
return new long[] {};
}
long x = num / 3;
return new long[] {x - 1, x, x + 1};
}
}
// Accepted solution for LeetCode #2177: Find Three Consecutive Integers That Sum to a Given Number
func sumOfThree(num int64) []int64 {
if num%3 != 0 {
return []int64{}
}
x := num / 3
return []int64{x - 1, x, x + 1}
}
# Accepted solution for LeetCode #2177: Find Three Consecutive Integers That Sum to a Given Number
class Solution:
def sumOfThree(self, num: int) -> List[int]:
x, mod = divmod(num, 3)
return [] if mod else [x - 1, x, x + 1]
// Accepted solution for LeetCode #2177: Find Three Consecutive Integers That Sum to a Given Number
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]
}
}
// Accepted solution for LeetCode #2177: Find Three Consecutive Integers That Sum to a Given Number
function sumOfThree(num: number): number[] {
if (num % 3) {
return [];
}
const x = Math.floor(num / 3);
return [x - 1, x, x + 1];
}
Use this to step through a reusable interview workflow for this problem.
Simulate the process step by step — multiply n times, check each number up to n, or iterate through all possibilities. Each step is O(1), but doing it n times gives O(n). No extra space needed since we just track running state.
Math problems often have a closed-form or O(log n) solution hidden behind an O(n) simulation. Modular arithmetic, fast exponentiation (repeated squaring), GCD (Euclidean algorithm), and number theory properties can dramatically reduce complexity.
Review these before coding to avoid predictable interview regressions.
Wrong move: Temporary multiplications exceed integer bounds.
Usually fails on: Large inputs wrap around unexpectedly.
Fix: Use wider types, modular arithmetic, or rearranged operations.