Guess the Majority in a Hidden Array — LeetCode 1538 Python Solution

MediumLeetCode PremiumArrayMathInteractive
Problem
#1538
Reading time
9 min

The problem

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): where 0 <= a < b < c < d < ArrayReader.length().

Example

Input
nums = [0,0,1,0,1,1,1,1]
Output
5
Explanation
The following calls to the API

Python solution

Python
# """
# 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()
        x = reader.query(0, 1, 2, 3)
        a, b = 1, 0
        k = 0
        for i in range(4, n):
            if reader.query(0, 1, 2, i) == x:
                a += 1
            else:
                b += 1
                k = i

        y = reader.query(0, 1, 2, 4)
        if reader.query(1, 2, 3, 4) == y:
            a += 1
        else:
            b += 1
            k = 0
        if reader.query(0, 2, 3, 4) == y:
            a += 1
        else:
            b += 1
            k = 1
        if reader.query(0, 1, 3, 4) == y:
            a += 1
        else:
            b += 1
            k = 2

        if a == b:
            return -1
        return 3 if a > b else k

Complexity

MeasureComplexity
TimeO(n), where n is the length of the array
SpaceO(1) auxiliary

Pattern: Math and Number Theory

Find the closed form, the invariant, or the modular identity — and skip the loop entirely. LeetCode 1538. Guess the Majority in a Hidden Array is filed here on both counts: the reference solution below belongs to the algorithm family this hub collects, and LeetCode tags it Math.

The math and number theory guide has the Python template for the pattern and the 485 LeetCode problems that use it.

Related problems

Frequently asked questions

How hard is LeetCode 1538. Guess the Majority in a Hidden Array?
LeetCode 1538. Guess the Majority in a Hidden Array is rated Medium on LeetCode.
What is the time complexity of LeetCode 1538. Guess the Majority in a Hidden Array?
The Python solution on this page runs in O(n), where n is the length of the array.
What is the space complexity of LeetCode 1538. Guess the Majority in a Hidden Array?
The Python solution on this page uses O(1) auxiliary space.
What topics does LeetCode 1538. Guess the Majority in a Hidden Array cover?
LeetCode 1538. Guess the Majority in a Hidden Array is tagged Array, Math and Interactive on LeetCode.
Is LeetCode 1538. Guess the Majority in a Hidden Array a premium problem?
Yes. LeetCode 1538. Guess the Majority in a Hidden Array is a LeetCode Premium problem, so the full statement and test cases require a paid LeetCode subscription.

Stuck on problems like this in a live interview?

Stealth Interview is a desktop app for macOS and Windows. It reads the problem off your screen and returns a working solution with a step-by-step explanation and its time and space complexity — invisible to screen sharing.

Get Stealth Interview