Problem · Math

Find Consecutive Integer Sequences

Learn this problem
EasyIDFCFULLTIMEOA

Problem statement

Given a positive integer n, find every sequence of consecutive positive integers whose sum is equal to n.

Each multi-number sequence must contain at least two numbers. Include the single-number sequence [n] as the final sequence so the total number of returned sequences matches the total count requested by the source prompt.

Return the sequences in increasing order of their first value. Represent each sequence as a string whose numbers are joined by " + ".

Function

findConsecutiveIntegerSequences(n: int) → String[]

Examples

Example 1

n = 15return = ["1 + 2 + 3 + 4 + 5","4 + 5 + 6","7 + 8","15"]

The consecutive positive-integer sequences that sum to 15 are 1 + 2 + 3 + 4 + 5, 4 + 5 + 6, and 7 + 8. The source prompt also includes the single-number sequence 15 in the total count.

Constraints

  • n is a positive integer.

More IDFC problems

drafts saved locally
public String[] findConsecutiveIntegerSequences(int n) {
  // write your code here
}
n15
expected["1 + 2 + 3 + 4 + 5", "4 + 5 + 6", "7 + 8", "15"]
checking account