Problem · Math

Get Sequence Sum

Learn this problem
EasyIBM logoIBMFULLTIMEOA
See IBM hiring insights

Problem statement

Given integers i, j, and k with i ≤ j and k ≤ j, form a sequence that:

  1. Starts at i and increases by 1 until it reaches j.
  2. Then decreases by 1 from j - 1 until it reaches k.

The value j appears once. Return the sum of the complete sequence as a long.

Function

getSequenceSum(i: int, j: int, k: int) → long

Examples

Example 1

i = 5j = 9k = 6return = 56

The sequence is 5, 6, 7, 8, 9, 8, 7, 6. Its sum is 56.

Constraints

  • -10^8 ≤ i ≤ j ≤ 10^8
  • -10^8 ≤ k ≤ j
  • The resulting sum fits in a signed 64-bit integer.

More IBM problems

drafts saved locally
public long getSequenceSum(int i, int j, int k) {
    // Write your code here
}
i5
j9
k6
expected56
checking account