Magicsheet logo

Find Three Consecutive Integers That Sum to a Given Number

Medium
100%
Updated 6/1/2025

Asked by 2 Companies

Find Three Consecutive Integers That Sum to a Given Number

1. What is this problem about?

The Find Three Consecutive Integers That Sum to a Given Number interview question is a mathematical logic task. You are given a long integer num. You need to determine if there exist three consecutive integers (x1,x,x+1x-1, x, x+1) whose sum is exactly equal to num. If they exist, you return them; otherwise, you return an empty list.

2. Why is this asked in interviews?

Companies like Amazon ask the Find Three Consecutive Integers coding problem to test basic algebraic reasoning and efficient execution. It evaluations whether a candidate can simplify a problem into a single O(1)O(1) calculation instead of trying to use a loop or search. It’s a core Math interview pattern.

3. Algorithmic pattern used

This problem uses Algebraic Simplification.

  • The Equation: (x1)+x+(x+1)=num(x-1) + x + (x+1) = num.
  • Simplified: 3x=num3x = num.
  • Conclusion: For three consecutive integers to sum to num, num must be divisible by 3.
  • Solving for x: x=num/3x = num / 3. The three integers are x1x-1, xx, and x+1x+1.

4. Example explanation

num = 33

  1. 33/3=1133 / 3 = 11. Since 33 is divisible by 3, integers exist.
  2. Middle integer is 11.
  3. Sequence: 10, 11, 12. Result: [10, 11, 12]. Check: 10+11+12=3310 + 11 + 12 = 33. Correct! num = 4
  4. 4/3=1.334 / 3 = 1.33. Not divisible. Result: [].

5. Common mistakes candidates make

  • Using a while loop: Attempting to find the integers by incrementing from 1 to num, which is O(num)O(num) and will time out for large inputs (101510^{15}).
  • Integer Overflow: Not using 64-bit integer types for the input or calculations.
  • Modulo Check: Forgetting to check num % 3 == 0 before returning a result.

6. Interview preparation tip

When a problem asks for "X consecutive numbers," always write out the algebraic sum. You'll find that the sum is always a multiple of X (if X is odd) or has a specific relationship to the middle point. This "Equation first" approach is a great time-saver.

Similar Questions