#K92702. Largest Square Formation

    ID: 38256 Type: Default 1000ms 256MiB

Largest Square Formation

Largest Square Formation

You are given a collection of sticks. Your task is to determine the side length of the largest square that can be formed using four sticks of the same length. In other words, if you have a stick length L that appears at least 4 times, you can form a square of side L.

If there are multiple stick lengths available with at least 4 occurrences, output the maximum such length. If no such length exists, output -1.

Mathematically, if we denote the frequency of a stick length \(L\) as \(f(L)\), you need to find: \[ \max\{L \mid f(L) \geq 4\} \] If no \(L\) satisfies \(f(L) \geq 4\), then output -1.

inputFormat

The input is provided via standard input (stdin) in the following format:

  1. The first line contains an integer \(n\) \(\big(1 \le n \le 10^5\big)\), representing the number of sticks.
  2. The second line contains \(n\) space-separated positive integers, each representing the length of a stick \(\big(1 \le length_i \le 10^9\big)\).

outputFormat

Output a single integer on standard output (stdout) representing the side length of the largest square that can be formed using four sticks of equal length. If no square can be formed, output -1.

## sample
5
6 6 6 6 5
6