Sum of Consecutive Integers
JavaScript
medium
mins
Given a number n, determine whether it can be expressed as the sum of two or more consecutive positive integers. Return true or false.
Examples
Input: n = 9 Output: true Explanation: 9 = 2 + 3 + 4
Input: n = 15 Output: true Explanation: 15 = 4 + 5 + 6 or 1 + 2 + 3 + 4 + 5
Input: n = 10 Output: true Explanation: 10 = 1 + 2 + 3 + 4
Input: n = 8 Output: false Explanation: 8 cannot be expressed as sum of consecutive integers
Input: n = 1 Output: false Explanation: 1 cannot be expressed as sum of two or more consecutive integers
Constraints
1 ⤠n ⤠10^9 Return false for: - n = 1 (requires at least 2 consecutive integers) - n < 1
Test Cases
Base Cases
n = 1n = 2n = 3
Valid Sums
n = 9n = 15n = 10n = 21
Invalid Sums
n = 8n = 4n = 16
Edge Cases
n = 1n = 2n = 100
Large Numbers
n = 1000n = 10000
Companies:
google
amazon
microsoft
meta
Solve Similar questions š„
Want to upskill? Explore our courses!
Namaste DSA
Master DSA from scratch with numerous problems, and expert guidance.
Namaste React
Wanna dive deep into React and become Frontend Expert? Learn with me now!
Namaste Frontend System Design
The most comprehensive and detailed course for frontend system design.
Namaste Node.js
Wanna dive deep into Node.js? Enroll into `Namaste Node.js` now!
