Find three consecutive integers that sum to a given number

May 22, 2023

math-and-geometry

Problem URL: Find three consecutive integers that sum to a given number

We will check whether the number is divisible by 3. If it is, we can return the three consecutive numbers. Otherwise, we will return empty arry.

class Solution:
    def sumOfThree(self, num: int) -> List[int]:
        if num % 3 != 0:
            return []

        n = num // 3;
        return [n-1, n, n+1]

Time complexity: O(1)
Space complexity: O(1)