← 返回 amazon 的题目列表Find Majority Element in Sorted Array Using Binary Search
类型:online_judge
amazon
Given a sorted integer array, find the number that appears most frequently in the array, and determine whether this number appears more than half of the size of the array. Implement an algorithm to solve this problem, and use binary search to optimize the search for this number. Assume that each number in the array is an integer and the array is not empty.
Input
A sorted integer array nums.
Output
If there is a number whose frequency exceeds half the size of the array, return this number; otherwise, return -1.
Example
Example 1: Input: nums = [1, 1, 2, 2, 3, 3, 3] Output: 3
Example 2: Input: nums = [1, 2, 3, 4, 5] Output: -1
Constraints
The length of the array is n, 1 <= n <= 10^4.
Example
Input
1 1 2 2 3 3 3