Element Appearing More Than 25% In Sorted Array - Problem
Given an integer array arr sorted in non-decreasing order, there is exactly one integer in the array that occurs more than 25% of the time.
Return that integer.
Input & Output
Example 1 — Basic Case
$
Input:
arr = [1,2,2,6,6,6,6,10]
›
Output:
6
💡 Note:
Array length is 8, so 25% is 2 elements. Element 6 appears 4 times, which is more than 25% (4 > 2), so return 6.
Example 2 — Different Majority Element
$
Input:
arr = [1,1,2,2,3,3,3,3,3]
›
Output:
3
💡 Note:
Array length is 9, so 25% is 2.25, meaning we need more than 2 elements. Element 3 appears 5 times (5 > 2.25), so return 3.
Example 3 — Minimum Case
$
Input:
arr = [1,1,1,2]
›
Output:
1
💡 Note:
Array length is 4, so 25% is 1 element. Element 1 appears 3 times (3 > 1), so return 1.
Constraints
- 1 ≤ arr.length ≤ 104
- -106 ≤ arr[i] ≤ 106
- arr is sorted in non-decreasing order
Visualization
Tap to expand
Understanding the Visualization
1
Input
Sorted array with one element appearing > 25%
2
Process
Count occurrences and check threshold
3
Output
Return the majority element
Key Takeaway
🎯 Key Insight: In a sorted array, the majority element forms consecutive groups, making counting efficient
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code