Element Appearing More Than 25% In Sorted Array (LeetCode 1287): Solution and Explanation
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
- Set step = n // 4.
- For i from 0 while i + step < n, compare arr[i] with arr[i + step].
- Return arr[i] at the first match.
Step-by-step example
arr = [1,2,2,6,6,6,6,7,10]
- n = 9, so step = 2. The ruler covers 3 cells.
- i = 0: arr[0] = 1, arr[2] = 2. Different, slide.
- i = 1: arr[1] = 2, arr[3] = 6. Different, slide.
- i = 2: arr[2] = 2, arr[4] = 6. Different, slide.
- 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 -11class Solution {
2 public int findSpecialInteger(int[] arr) {
3 int step = arr.length / 4;
4 for (int i = 0; i + step < arr.length; i++) {
5 if (arr[i] == arr[i + step]) {
6 return arr[i];
7 }
8 }
9 return -1;
10 }
11}1class Solution {
2public:
3 int findSpecialInteger(vector<int>& arr) {
4 int n = arr.size(), step = n / 4;
5 for (int i = 0; i + step < n; i++) {
6 if (arr[i] == arr[i + step]) {
7 return arr[i];
8 }
9 }
10 return -1;
11 }
12};Time and space complexity
O(n)
The ruler slides at most n - n // 4 times, with one comparison each.
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.