Skip to main content
Back to problems
#2177
Medium Algorithms

Find three consecutive integers that sum to a given number

Math Simulation
65.3% acceptance
Feb 25, 2026
730
233
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.

Solution

Rust
Time O(1)
Space O(n)
LeetCode
solution.rs
impl Solution {
  pub fn sum_of_three(num: i64) -> Vec<i64> {
    // x + (x+1) + (x+2) = 3x+3 = num => x = (num-3)/3 = num/3 - 1
    // num must be divisible by 3
    if num % 3 != 0 {
      return vec![];
    }
    let x = num / 3 - 1;
    vec![x, x + 1, x + 2]
  }
}