#K45137. Find the Equilibrium Element in a Sorted Array

    ID: 27687 Type: Default 1000ms 256MiB

Find the Equilibrium Element in a Sorted Array

Find the Equilibrium Element in a Sorted Array

You are given an array of integers. Your task is to determine if there exists an element in the array such that the number of elements strictly less than it is equal to the number of elements strictly greater than it.

Formally, let \(A\) be the given array of \(n\) integers. We want to check if there exists an index \(i\) (with 0-based indexing) such that if the array were sorted, the following conditions hold:

  • \(n \geq 3\) (since an array with fewer than 3 elements cannot satisfy the condition),
  • \(i = n - 1 - i\) (meaning \(i = \frac{n-1}{2}\); hence, \(n\) must be odd), and
  • the element at the candidate index is unique, i.e. \(A[i-1] < A[i] < A[i+1]\) when sorted.

If such an element exists, output YES; otherwise, output NO.

Note: Duplicate elements can invalidate the uniqueness condition required.

inputFormat

The first line of input contains an integer \(T\) indicating the number of test cases.

Each test case begins with an integer \(n\) (the number of elements in the array) on a new line, followed by a line with \(n\) space-separated integers.

Example:

2
5
1 2 3 4 5
4
2 2 2 2

outputFormat

For each test case, output a single line containing YES if the required element exists, otherwise NO.

Example Output:

YES
NO
## sample
2
5
1 2 3 4 5
4
2 2 2 2
YES

NO

</p>