Skip to content

Files

Latest commit

Jul 23, 2023
e1b8af0 · Jul 23, 2023

History

History

2177-Find Three Consecutive Integers That Sum to a Given Number

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
No commit message
Jul 23, 2023
No commit message
Jul 23, 2023
No commit message
Jul 23, 2023

2177. Find Three Consecutive Integers That Sum to a Given Number

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

Solutions (Rust)

1. Solution

impl Solution {
    pub fn sum_of_three(num: i64) -> Vec<i64> {
        if num % 3 == 0 {
            vec![num / 3 - 1, num / 3, num / 3 + 1]
        } else {
            vec![]
        }
    }
}