#K4451. Smallest Zero Sum Subset

    ID: 27547 Type: Default 1000ms 256MiB

Smallest Zero Sum Subset

Smallest Zero Sum Subset

Given an integer n and a list of n integers, your task is to determine the size of the smallest non-empty subset whose elements sum to zero. If no such subset exists, output -1.

Formally, let \( S \) be a non-empty subset of the given set such that \[ \sum_{x \in S} x = 0, \] find the minimum possible value of \(|S|\) (i.e. the number of elements in \(S\)). If there is no such subset, return -1.

Note: The input size is expected to be small (e.g. \(n \leq 20\)), so a brute-force approach of checking all possible subsets is acceptable.

inputFormat

The input consists of two lines:

  1. The first line contains a single integer \(n\) representing the number of integers.
  2. The second line contains \(n\) space-separated integers.

outputFormat

Output a single integer: the size of the smallest subset whose elements sum to zero, or -1 if no such subset exists.

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

</p>