Guess the Majority in a Hidden Array Problem
Description
LeetCode Problem 1538.
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.
Example 1:
Input: nums = [0,0,1,0,1,1,1,1]
Output: 5
Explanation: The following calls to the API
reader.length() // returns 8 because there are 8 elements in the hidden array.
reader.query(0,1,2,3) // returns 2 this is a query that compares the elements nums[0], nums[1], nums[2], nums[3]
// Three elements have a value equal to 0 and one element has value equal to 1 or viceversa.
reader.query(4,5,6,7) // returns 4 because nums[4], nums[5], nums[6], nums[7] have the same value.
we can infer that the most frequent value is found in the last 4 elements.
Index 2, 4, 6, 7 is also a correct answer.
Example 2:
Input: nums = [0,0,1,1,0]
Output: 0
Example 3:
Input: nums = [1,0,1,0,1,0,1,0]
Output: -1
Constraints:
5 <= nums.length <= 10^50 <= nums[i] <= 1
Follow up: What is the minimum number of calls needed to find the majority element?
Solution
Approach: Compare Every Index with Index 3
query cannot tell us any value directly, but it can tell us whether two indices hold the same value. Take two queries that differ in one index only, say query(0, 1, 2, 3) and query(0, 1, 2, i). Swapping index 3 for i changes the number of ones by nums[i] - nums[3], which is -1, 0 or 1. The result for 0, 1, 2, 3 or 4 ones is 4, 2, 0, 2, 4, and two neighbors in that list are never equal. So the two results are equal exactly when nums[i] == nums[3].
Use that to sort every index into "same as index 3" or "different from index 3":
- Index
3itself is in "same". - For
i = 4..n-1, comparequery(0, 1, 2, i)withquery(0, 1, 2, 3). - Indices
0,1and2cannot be compared that way, since they are already in the query. Use index4instead (the array has at least 5 elements): comparequery(1, 2, 3, 4),query(0, 2, 3, 4)andquery(0, 1, 3, 4)withquery(0, 1, 2, 4). Each pair swaps one of0,1,2for index3.
If both groups are the same size, there is a tie and the answer is -1. If "same" is larger, index 3 holds the majority value. Otherwise return any index from "different", for example the last one we saw.
This takes 1 + (n - 4) + 4 = n + 1 queries, well within the limit of 2n.
Complexity: O(n) time and n + 1 calls to query, with O(1) extra space.
# """
# This is the ArrayReader's API interface.
# You should not implement it, or speculate about its implementation
# """
# class ArrayReader(object):
# # Compares 4 different elements in the array
# # return 4 if the values of the 4 elements are the same (0 or 1).
# # return 2 if three elements have a value equal to 0 and one element has value equal to 1 or vice versa.
# # return 0 : if two element have a value equal to 0 and two elements have a value equal to 1.
# def query(self, a: int, b: int, c: int, d: int) -> int:
#
# # Returns the length of the array
# def length(self) -> int:
#
class Solution:
def guessMajority(self, reader: 'ArrayReader') -> int:
n = reader.length()
# Sort every index into "same value as index 3" or "different from index 3".
same, diff, diff_index = 1, 0, -1 # index 3 itself is in "same"
def record(is_same, i):
nonlocal same, diff, diff_index
if is_same:
same += 1
else:
diff += 1
diff_index = i
# Indices 4..n-1: swapping index 3 for i in query(0, 1, 2, 3) keeps the result iff nums[i] == nums[3].
base = reader.query(0, 1, 2, 3)
for i in range(4, n):
record(reader.query(0, 1, 2, i) == base, i)
# Indices 0, 1, 2: swap each one for index 3 in query(0, 1, 2, 4) the same way.
base = reader.query(0, 1, 2, 4)
record(reader.query(1, 2, 3, 4) == base, 0)
record(reader.query(0, 2, 3, 4) == base, 1)
record(reader.query(0, 1, 3, 4) == base, 2)
if same == diff:
return -1
return 3 if same > diff else diff_index