Welcome to Subscribe On Youtube
Formatted question description: https://leetcode.ca/all/2177.html
2177. Find Three Consecutive Integers That Sum to a Given Number (Medium)
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
Similar Questions:
Solution 1. Math
Let x
be the first number of the 3 consecutive numbers. Then num = x + x + 1 + x + 2 = 3 * x + 3
. Since x
is an integer, num
must be multiple of 3
, and x = num / 3 - 1
.
-
// OJ: https://leetcode.com/problems/find-three-consecutive-integers-that-sum-to-a-given-number/ // Time: O(1) // Space: O(1) class Solution { public: vector<long long> sumOfThree(long long num) { if (num % 3) return {}; long x = num / 3 - 1; return {x, x + 1, x + 2}; } };
-
class Solution: def sumOfThree(self, num: int) -> List[int]: a, b = divmod(num, 3) return [] if b else [a - 1, a, a + 1] ############ # 2177. Find Three Consecutive Integers That Sum to a Given Number # https://leetcode.com/problems/find-three-consecutive-integers-that-sum-to-a-given-number/ class Solution: def sumOfThree(self, num: int) -> List[int]: if num % 3 != 0: return [] mid = num // 3 return [mid - 1, mid, mid + 1]
-
class Solution { public long[] sumOfThree(long num) { if (num % 3 != 0) { return new long[] {}; } long x = num / 3; return new long[] {x - 1, x, x + 1}; } }
-
func sumOfThree(num int64) []int64 { if num%3 != 0 { return []int64{} } x := num / 3 return []int64{x - 1, x, x + 1} }
-
function sumOfThree(num: number): number[] { if (num % 3) { return []; } const x = Math.floor(num / 3); return [x - 1, x, x + 1]; }
Or one-liner
-
// OJ: https://leetcode.com/problems/find-three-consecutive-integers-that-sum-to-a-given-number/ // Time: O(1) // Space: O(1) class Solution { public: vector<long long> sumOfThree(long long num) { return num % 3 ? vector<long long>{} : vector<long long>{num / 3 - 1, num / 3, num / 3 + 1}; } };
-
class Solution: def sumOfThree(self, num: int) -> List[int]: a, b = divmod(num, 3) return [] if b else [a - 1, a, a + 1] ############ # 2177. Find Three Consecutive Integers That Sum to a Given Number # https://leetcode.com/problems/find-three-consecutive-integers-that-sum-to-a-given-number/ class Solution: def sumOfThree(self, num: int) -> List[int]: if num % 3 != 0: return [] mid = num // 3 return [mid - 1, mid, mid + 1]
-
class Solution { public long[] sumOfThree(long num) { if (num % 3 != 0) { return new long[] {}; } long x = num / 3; return new long[] {x - 1, x, x + 1}; } }
-
func sumOfThree(num int64) []int64 { if num%3 != 0 { return []int64{} } x := num / 3 return []int64{x - 1, x, x + 1} }
-
function sumOfThree(num: number): number[] { if (num % 3) { return []; } const x = Math.floor(num / 3); return [x - 1, x, x + 1]; }
Discuss
https://leetcode.com/problems/find-three-consecutive-integers-that-sum-to-a-given-number/discuss/1783380/C%2B%2B-Math-one-liner