Leetcode 2177: Find Three Consecutive Integers That Sum to a Given Number
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.
Approach: The approach involves checking if the given number is divisible by 3, then finding the three consecutive integers that sum to the number.
Observations:
• The sum of three consecutive integers is always divisible by 3.
• Check if `num` is divisible by 3, then simply return the three integers. If not, return an empty array.
Steps:
• Check if `num` is divisible by 3.
• If it is, return `num / 3 - 1`, `num / 3`, `num / 3 + 1`.
• If it is not, return an empty array.
Empty Inputs:
• The input `num` is always non-negative and within the specified range.
Large Inputs:
• The solution should handle very large numbers, as `num` can be as large as 10^15.
Special Values:
• Consider the case when `num` is 0, as the sum of three consecutive integers can't result in 0.
Constraints:
• Ensure that the solution works within the input constraints.
vector<long long> sumOfThree(long long num) {
vector<long long> ans;
if(num %3 != 0) return ans;
return {num / 3 - 1, num/ 3, num/3 + 1};
}
1 : Function Definition
vector<long long> sumOfThree(long long num) {
Define the function `sumOfThree` that takes a single long long integer `num` and returns a vector of long long integers.
2 : Variable Initialization
vector<long long> ans;
Initialize an empty vector `ans` to store the result if `num` is divisible by 3.
3 : Condition Check
if(num %3 != 0) return ans;
Check if `num` is divisible by 3. If not, return the empty vector `ans`.
4 : Return Result
return {num / 3 - 1, num/ 3, num/3 + 1};
If `num` is divisible by 3, return a vector containing three integers: `num/3 - 1`, `num/3`, and `num/3 + 1`.
Best Case: O(1)
Average Case: O(1)
Worst Case: O(1)
Description: The time complexity is constant because the solution only requires a simple modulo check and arithmetic operations.
Best Case: O(1)
Worst Case: O(1)
Description: The space complexity is constant because we are only storing a fixed number of integers.
LeetCode Solutions Library / DSA Sheets / Course Catalog |
---|
comments powered by Disqus