Facebook Pixel

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 = 1
  • n = 2
  • n = 3

Valid Sums

  • n = 9
  • n = 15
  • n = 10
  • n = 21

Invalid Sums

  • n = 8
  • n = 4
  • n = 16

Edge Cases

  • n = 1
  • n = 2
  • n = 100

Large Numbers

  • n = 1000
  • n = 10000

Companies:

google
amazon
microsoft
meta

Solve Similar questions 🔥