Skip to content

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

  • Time: $O(n)$
  • Space: $O(1)$
1
2
3
4
5
6
7
8
9
class Solution {
 public:
  vector<long long> sumOfThree(long long num) {
    if (num % 3)
      return {};
    const long x = num / 3;
    return {x - 1, x, x + 1};
  }
};
1
2
3
4
5
6
7
8
class Solution {
  public long[] sumOfThree(long num) {
    if (num % 3 != 0)
      return new long[] {};
    final long x = num / 3;
    return new long[] {x - 1, x, x + 1};
  }
}
1
2
3
4
5
6
class Solution:
  def sumOfThree(self, num: int) -> list[int]:
    if num % 3:
      return []
    x = num // 3
    return [x - 1, x, x + 1]