Classic problem #1287EasyArray
Element Appearing More Than 25% In Sorted Array
Real-world version: The Badge Filling Over a Quarter of the RosterTracked in this browser only
Read Element Appearing More Than 25% In Sorted Array on LeetCode(opens in a new tab)
The statement here is our own: the same inputs, outputs and technique.
A roster roster of badge numbers is sorted in non-decreasing order. Exactly one badge number takes up more than a quarter of the entries.
Return that badge number.
Examples
Example 1
- Input
- roster = [1, 2, 2, 3, 3, 3, 3, 4]
- Output
- 3
Eight entries mean a badge has to appear more than twice. Only 3 does, with four entries, while the two 2 entries are exactly a quarter and so fall short.
Example 2
- Input
- roster = [5, 5, 5, 5]
- Output
- 5
Every entry carries the same badge, so it fills the whole roster.
Example 3
- Input
- roster = [1, 2, 3, 4, 4, 4]
- Output
- 4
Six entries mean a badge needs at least two. Only 4 repeats at all, three times over.
Constraints
- 1 <= roster.length <= 10^4
- 0 <= roster[i] <= 10^5
- roster is sorted in non-decreasing order
- exactly one badge number takes up more than a quarter of the roster
The signature
The editor starts with this. It matches the parent problem's shape, so a solution that works here works on a standard judge unchanged.
Python
def find_special_integer(roster: list[int]) -> int:Java
public int findSpecialInteger(int[] roster)Checking your plan…
How this problem was checked
- Passed: Reference solution run against 15 test cases: 15 passed
- Passed: 3 worked examples confirmed by running the code
- Passed: 0 eight-word sequences shared with the parent problem's text
- Passed: Agrees with an independent brute-force solution on random inputs
- Passed: Test set shown to catch deliberately broken solutions
- Passed: Reviewed by a separate quality pass
- Passed: Function arity matches the parent problem's