Element Appearing More Than 25% In Sorted Array (LeetCode 1287): Solution and Explanation

Easy
Array

In a sorted array the special value forms one block of more than n / 4 cells, so its first copy and the cell n // 4 to the right hold the same value. Scan i and return arr[i] when arr[i] == arr[i + n // 4].

Intuition

Sorting puts every copy of a value next to each other, so each distinct value is one solid block.

The special value occurs more than n / 4 times, so its block is at least n // 4 + 1 cells long. Lay a ruler of exactly that length on its first copy and both ends of the ruler land inside the block.

For any other value, the block is too short to reach from one end of the ruler to the other, so the two ends differ. The first position where the ends match gives the answer.

Approach: Quarter-distance comparison

  1. Set step = n // 4.
  2. For i from 0 while i + step < n, compare arr[i] with arr[i + step].
  3. Return arr[i] at the first match.

Step-by-step example

arr = [1,2,2,6,6,6,6,7,10]
  1. n = 9, so step = 2. The ruler covers 3 cells.
  2. i = 0: arr[0] = 1, arr[2] = 2. Different, slide.
  3. i = 1: arr[1] = 2, arr[3] = 6. Different, slide.
  4. i = 2: arr[2] = 2, arr[4] = 6. Different, slide.
  5. i = 3: arr[3] = 6, arr[5] = 6. The ends match, so 6 fills at least 3 of 9 cells. Return 6.

Code: Python, Java, C++

1class Solution:
2    def findSpecialInteger(self, arr: List[int]) -> int:
3        step = len(arr) // 4
4        for i in range(len(arr) - step):
5            if arr[i] == arr[i + step]:
6                return arr[i]
7        return -1
Press play on the visualizer and the running line lights up here.

Time and space complexity

Work as the input growsinput size n →steps
Binary search at the quarter marks O(log n)
Quarter-distance comparison O(n)
Count runs O(n)
Time

O(n)

The ruler slides at most n - n // 4 times, with one comparison each.

Space

O(1)

Only the index and step are stored.

Other approaches

Binary search at the quarter marks (time O(log n), space O(1))

The special block must cover one of the positions n / 4, n / 2 or 3n / 4. For each candidate value there, binary search its first and last index and check the count. This runs in O(log n).

Count runs (time O(n), space O(1))

Walk the array counting the current run of equal values and return the value as soon as its run exceeds n / 4. Also linear and very readable.

Common mistakes

  • Using step = n // 4 - 1 or comparing against n / 4 as a float. A value needs more than n / 4 copies, which means at least n // 4 + 1 cells, so the ends are exactly n // 4 apart.
  • Using a hash map counter. It works but ignores the sorted order, which is what makes the constant-space answer possible.
  • Forgetting n < 4. Then step = 0, every cell matches itself, and the first element is the answer, which is correct because it occurs more than 25% of the time.

Frequently asked questions

Why must the ends match only for the special value?

If arr[i] == arr[i + step] in a sorted array, every cell between them holds the same value, so that value has at least step + 1 = n // 4 + 1 copies, which is more than n / 4. Only the special value has that many.

Does the match have to happen at the first copy of the special value?

Scanning from the left, the first match does happen at its first copy, because earlier positions start in a different block. But correctness only needs some match: any match proves the value has enough copies.

How does the O(log n) version work?

A block longer than n / 4 cannot fit between two quarter marks, so it covers one of the indices n / 4, n / 2 or 3n / 4. Check those three values with two binary searches each.