Leetcode 2177: Find Three Consecutive Integers That Sum to a Given Number

grid47
grid47
Exploring patterns and algorithms
Apr 3, 2024 3 min read

Given an integer num, your task is to determine if it can be expressed as the sum of three consecutive integers. If it can, return these integers as a sorted array. If not, return an empty array.
Problem
Approach
Steps
Complexity
Input: The input consists of a single integer `num`.
Example: 27
Constraints:
• 0 <= num <= 10^15
Output: The output is either an array containing the three consecutive integers that sum to `num` or an empty array if no such triplet exists.
Example: [8, 9, 10]
Constraints:
• The output must be a sorted array of three consecutive integers or an empty array.
Goal: The goal is to find if `num` can be written as the sum of three consecutive integers.
Steps:
• Check if `num` is divisible by 3.
• If `num` is divisible by 3, calculate the three consecutive integers using `num / 3 - 1`, `num / 3`, and `num / 3 + 1`.
• If not, return an empty array.
Goal: The input `num` should be within the given bounds.
Steps:
• 0 <= num <= 10^15
Assumptions:
• The integer `num` is non-negative.
Input: 27
Explanation: The number 27 can be expressed as 8 + 9 + 10. These are three consecutive integers that sum to 27.

Link to LeetCode Lab


LeetCode Solutions Library / DSA Sheets / Course Catalog
comments powered by Disqus