Element Appearing More Than 25% In Sorted Array

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.

30:00

Element Appearing More Than 25% In Sorted Array
easy
Free written solution
Topics
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
Constraints:
  • 1≤arr.length≤1041 \leq \text{arr.length} \leq 10^4

  • 0≤arr[i]≤1050 \leq \text{arr}[i] \leq 10^5

Test Cases
Input (Stdin)
Expected Output (Stdout)
Execution Result

Run code to see results.

Input
arr ={"arr":[1,2,2,6,6,6,6,7,10]}

n = 9, so step = n // 4 = 2. A value appearing more than 2.25 times fills at least 3 cells in a row, so it covers both ends of any ruler of that length placed on its first copy.

the sorted array as runs of equal values (width = count); the orange ruler spans step + 1 = 3 cells, just over a quarter

25%

50%

75%

1

2 x2

6 x4

7

10

0

8

1
2
2
6
6
6
6
7
10

0

1

2

3

4

5

6

7

8

arr[i]

-

?

arr[i + 2]

-

Variables
VariableValue
step2
i-
arr[i]-
arr[i+step]-
DepthFunction Call
Stack empty
0/6