1538. Guess the Majority in a Hidden Array
https://leetcode.com/problems/guess-the-majority-in-a-hidden-array
Description
We have an integer array nums
, where all the integers in nums
are 0 or 1. You will not be given direct access to the array, instead, you will have an API ArrayReader
which have the following functions:
int query(int a, int b, int c, int d)
: where0 <= a < b < c < d < ArrayReader.length()
. The function returns the distribution of the value of the 4 elements and returns:4 : if the values of the 4 elements are the same (0 or 1).
2 : if three elements have a value equal to 0 and one element has value equal to 1 or vice versa.
0: if two element have a value equal to 0 and two elements have a value equal to 1.
int length()
: Returns the size of the array.
You are allowed to call query()
2 * n times at most where n is equal to ArrayReader.length()
.
Return any index of the most frequent value in nums
, in case of tie, return -1.
Follow up: What is the minimum number of calls needed to find the majority element?
Example 1:
Example 2:
Example 3:
Constraints:
5 <= nums.length <= 10^5
0 <= nums[i] <= 1
ac
Last updated