Guess the Majority in a Hidden Array - 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(). 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.

Input & Output

Example 1 — Basic Case
$ Input: nums = [1,1,0,1,0] (hidden)
Output: 0
💡 Note: Array has three 1s and two 0s. Since 1 appears more frequently, return any index containing 1, such as index 0.
Example 2 — Tie Case
$ Input: nums = [1,0,1,0] (hidden)
Output: -1
💡 Note: Array has equal numbers of 0s and 1s (2 each). Since there's a tie, return -1.
Example 3 — Majority Zeros
$ Input: nums = [0,0,0,1,1] (hidden)
Output: 0
💡 Note: Array has three 0s and two 1s. Since 0 appears more frequently, return any index containing 0, such as index 0.

Constraints

  • 4 ≤ nums.length ≤ 105
  • nums[i] is either 0 or 1
  • You can call query() at most 2 * n times

Visualization

Tap to expand
Guess the Majority in a Hidden Array INPUT Hidden Array (nums) ? i=0 ? i=1 ? i=2 ? i=3 ? i=4 Actual: [1, 1, 0, 1, 0] ArrayReader API query(a,b,c,d): Returns 4, 2, or 0 length(): 5 Query Results Meaning 4: All same (0000 or 1111) 2: 3 same, 1 different 0: 2 same, 2 different ALGORITHM STEPS 1 Fix Reference Use index 0 as anchor q1 = query(0,1,2,3) 2 Compare Elements Query excluding idx 0 q2 = query(1,2,3,4) 3 Determine Equality If q1 == q2: nums[0]==nums[4] Track same/diff counts 4 Count Majority same > diff: return 0 diff > same: return diffIdx Example Queries query(0,1,2,3) = 2 query(1,2,3,4) = 0 Results differ --> nums[0]!=nums[4] Count: same=2, diff=1 same > diff, return idx 0 FINAL RESULT Array Revealed 1 i=0 1 i=1 0 i=2 1 i=3 0 i=4 Value Counts 1s: 3 (majority) 0s: 2 OUTPUT 0 Index 0 has value 1 1 is the majority value [OK] Valid Answer Key Insight: By comparing query results with and without index 0, we determine if other elements match nums[0]. If same result --> element equals nums[0]. Track counts to find majority without seeing actual values. Time: O(n) queries | Space: O(1) | Max 2n queries allowed TutorialsPoint - Guess the Majority in a Hidden Array | Smart Query Strategy
Asked in
Google 15 Facebook 12
12.5K Views
Medium Frequency
~35 min Avg. Time
432 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen