#K52202. Find Zero-Sum Subarray

    ID: 29257 Type: Default 1000ms 256MiB

Find Zero-Sum Subarray

Find Zero-Sum Subarray

You are given an array of integers. Your task is to find the first contiguous subarray whose elements sum to 0. If there is no such subarray, output an empty result.

Let \(S_i = \sum_{j=0}^{i} \text{nums}[j]\) denote the prefix sum of the array. A zero-sum subarray exists if for some indices \(i < j\), \(S_i = S_j\) or if \(S_k = 0\) for some index \(k\). The answer is the first such subarray encountered when scanning from left to right.

Input/Output: Read from stdin and print the result to stdout as described below.

Examples:

  • Input: 5\n1 2 -3 3 4 | Output: 1 2 -3
  • Input: 2\n-1 1 | Output: -1 1
  • Input: 3\n1 2 3 | Output: (empty output)

inputFormat

The first line contains an integer n representing the number of elements in the array. The second line contains n space-separated integers.

outputFormat

Print the first contiguous subarray whose sum is 0 as space-separated integers on a single line. If no such subarray exists, print an empty line.

## sample
5
1 2 -3 3 4
1 2 -3