Talentd
FleetCode

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

Medium65.3% acceptance1 company

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

Asked at 1 company

Hints open one at a time

Hint 1
Notice that if a solution exists, we can represent them as x-1, x, x+1. What does this tell us about the number?
Hint 2
Notice the sum of the numbers will be 3x. Can you solve for x?
5:09
Still stuck?Read 3 approaches, or watch a walkthrough.
Open solution
Loading editor…
1class Solution:
2 def sumOfThree(self, num: int) -> List[int]:
3
Starter codePython 3Spaces: 4
Examples
Input
33
Input
4
Your code is saved on this device.Solution