Talentd
FleetCode

1287. Element Appearing More Than 25% In Sorted Array

Easy61.5% acceptance4 companies

Given an integer array sorted in non-decreasing order, there is exactly one integer in the array that occurs more than 25% of the time, return that integer.

Example 1:

Input: arr = [1,2,2,6,6,6,6,7,10]
Output: 6

Example 2:

Input: arr = [1,1]
Output: 1

Constraints:

  • 1 <= arr.length <= 104
  • 0 <= arr[i] <= 105

Asked at 4 companies

Topics

Hints open one at a time

Hint 1
Divide the array in four parts [1 - 25%] [25 - 50 %] [50 - 75 %] [75% - 100%]
Hint 2
The answer should be in one of the ends of the intervals.
Hint 3
In order to check which is element is the answer we can count the frequency with binarySearch.
25:35
Still stuck?Read 3 approaches, or watch a walkthrough.
Open solution
Loading editor…
1class Solution:
2 def findSpecialInteger(self, arr: List[int]) -> int:
3
Starter codePython 3Spaces: 4
Examples
Input
[1,2,2,6,6,6,6,7,10]
Input
[1,1]
Your code is saved on this device.Solution