Problem solution · C++

Guess the Majority in a Hidden Array

Guess the Majority in a Hidden Array: a C++ solution using direct simulation. Learn the idea, check the complexity, and read the full code, with credit to walkccc LeetCode Solutions.

Technique
Direct simulation
Source
walkccc LeetCode Solutions
Length
70 lines
Start with the idea.

Try the problem first. If you get stuck, read the approach below, then write your own solution. The full code is at the bottom.

Approach

Direct simulation

For Guess the Majority in a Hidden Array, the implementation follows the problem’s operations directly while maintaining only the state needed for the next decision.

  1. Translate each rule into one explicit state update.
  2. Maintain the invariant after every processed item.
  3. Return the accumulated state once all relevant input has been handled.

Code notes

  • 70 lines of C++ from the credited upstream file 1538.cpp.
  • The implementation visibly relies on sequence storage.
  • 3 loop blocks detected.

Complexity

Count the number and nesting of passes over the input, then include the maintained containers in the memory estimate.

Check the problem constraints before deciding whether this complexity will pass.

Source

Code and credit

This code comes from walkccc LeetCode Solutions by P.-Y. Chen (walkccc) and is used under the MIT licence.

Full codeGuess the Majority in a Hidden Array · C++C++
Use this to learn the idea, then write your own version.
/** * // This is the ArrayReader's API interface. * // You should not implement it, or speculate about its implementation * class ArrayReader { *  public: *   // Compares 4 different elements in the array *   // Returns 4 if the values of the 4 elements are the same (0 or 1). *   // Returns 2 if three elements have a value equal to 0 and one element has *   //           value equal to 1 or vice versa. *   // Returns 0 if two element have a value equal to 0 and two elements have *   //           a value equal to 1. *   int query(int a, int b, int c, int d); * *   // Returns the length of the array *   int length(); * }; */ class Solution { public:  int guessMajority(ArrayReader& reader) {    const int n = reader.length();    const int query0123 = reader.query(0, 1, 2, 3);    const int query1234 = reader.query(1, 2, 3, 4);    // the number of numbers that are same as `nums[0]`    int zeros = 1;    // the number of numbers that are different from `nums[0]`    int nonZeros = 0;    // any index i s.t. nums[i] != nums[0]    int indexNot0 = -1;     // Find which group nums[1..3] belong to.    for (int i = 1; i <= 3; ++i) {      vector<int> abcd = getABCD(i);      if (reader.query(abcd[0], abcd[1], abcd[2], abcd[3]) == query1234) {        ++zeros;      } else {        ++nonZeros;        indexNot0 = i;      }    }     // Find which group nums[4..n) belong to.    for (int i = 4; i < n; ++i)      if (reader.query(1, 2, 3, i) == query0123) {        ++zeros;      } else {        ++nonZeros;        indexNot0 = i;      }     if (zeros == nonZeros)      return -1;    if (zeros > nonZeros)      return 0;    return indexNot0;  }  private:  // Returns [0..4] except i.  vector<int> getABCD(int i) {    vector<int> abcd{0};    for (int j = 1; j <= 3; ++j)      if (j != i)        abcd.push_back(j);    abcd.push_back(4);    return abcd;  }}; 

Did this explanation save you time? I'm a Grade 11 student building this free library to make difficult algorithms easier to understand.

Buy me a coffee ↗