math and geometry May 22, 2023

Find three consecutive integers that sum to a given number

Time O(1) Space O(1) Open original problem

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)