[LeetCode] Find Three Consecutive Integers That Sum to a Given Number

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.

1
2
3
4
5
6
7
8
9
10
class Solution {
public:
vector<long long> sumOfThree(long long num) {
long long start = (num - 3) / 3;
if(start * 3 + 3 == num) {
return {start, start + 1, start + 2};
} else return {};
}
};

Author: Song Hayoung
Link: https://songhayoung.github.io/2022/02/20/PS/LeetCode/find-three-consecutive-integers-that-sum-to-a-given-number/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.