FastPrepUnique Integers That Sum to Zero

Unique Integers That Sum to Zero

NVIDIA logoNVIDIA● EasyINTERNOA

Problem statement

Given an integer n, return any array of exactly n distinct integers whose sum is 0.

The values may appear in any order. Every returned value must fit a signed 32-bit integer. Any array that meets these rules is accepted; it does not need to match the examples.

Function

sumZero(n: int) → int[]

Examples

Example 1

n = 5return = [-1,1,-2,2,0]

These five integers are distinct. The pairs -1 + 1 and -2 + 2 each sum to 0, and the remaining value is 0.

Example 2

n = 4return = [-1,1,-2,2]

There are four distinct integers, and their sum is -1 + 1 - 2 + 2 = 0.

Example 3

n = 1return = [0]

A one-element array must contain 0 to have sum 0.

Constraints

  • 1 <= n <= 100.

More NVIDIA problems

See NVIDIA hiring insights
Saved
public int[] sumZero(int n) {
    // Write your code here.
}
n5
reference[-1,1,-2,2,0]
Checking account…