Program to find out the index of the most frequent element in a concealed array in Python


Suppose, we are given a class called 'TestArray' that contains an private array which can only contain values 0 or 1; and two public member functions length() and query(). The function length() returns the length of the array and the function query() returns three different values comparing various values in the array. The function takes four values p, q, r, s as input and works like this −

  • if all the four values in the given indexes of the array are either 0 or 1, it returns 4.

  • else if any three values in the given indexes of the array are the same and the fourth value is different, then it returns 2.

  • else if the array contains two values 0 and two values 1 in the given indexes of the array, then it returns 0.

We have to find out the index of the element that is the most frequent in the array without accessing the array itself and using only the member functions of the class. If there are same number of 0 and 1 in the array, it returns the value -1.

So, if the input is like array = [0, 1, 1, 0, 1, 1, 1, 0]., then the output will be 2 In index 2 of the array, the value is 1 which is the most frequent value in the array. Similarly, answers 1, 4, 5, 6 are also true because the indexes also contain the value 1.

To solve this, we will follow these steps −

  • n:= length()

  • groupA := 1

  • groupB : = 0

  • aIdx := null

  • bIdx := null

  • first:= := query(0, 1, 2, 3)

  • second := query(0, 1, 2, 4)

  • for i in range 4 to n, do

    • if query(0, 1, 2, i) is same as first, then

      • groupA:= groupA+1

      • aIdx := i

    • otherwise,

      • groupB:= groupB+1

      • bIdx := i

  • for i in range 0 to 2, do

    • nxt := new list

    • for v in range 1 to 4, do

      • if v is not same as i, then

        • add v at the end of nxt

    • if query(values of nxt) is same as second, then

      • groupA:= groupA + 1

      • aIdx := i

    • otherwise,

      • groupB:= groupB+1

      • bIdx := i

  • if groupA>groupB, then

    • return aIdx

  • otherwise, if groupB>groupA, then

    • return aIdx

  • otherwise,

    • return -1

Example (Python)

Let us see the following implementation to get better understanding −

 Live Demo

class TestArray:
   def __init__(self, array) -> None:
      self.__arr = array

   def length(self):
      return len(self.__arr)

   def query(self, p, q, r, s):
      val = self.__arr[p] + self.__arr[q] + self.__arr[r] + self.__arr[s]
      if val == 4 or val == 0:
         return 4
      elif val == 1 or val == 3:
         return 2
      elif val == 2:
         return 0

def solve(reader):
   n,groupA,groupB,aIdx,bIdx=reader.length(),1,0,None,None
   first,second=reader.query(0,1,2,3),reader.query(0,1,2,4)
   for i in range(4,n):
      if reader.query(0,1,2,i)==first:
         groupA,aIdx=groupA+1,i
      else:
         groupB,bIdx=groupB+1,i
   for i in range(3):
      nxt=[v for v in [0,1,2,3,4] if v!=i]
      if reader.query(*nxt)==second:
         groupA,aIdx=groupA+1,i
      else:
         groupB,bIdx=groupB+1,i
   return aIdx if groupA>groupB else bIdx if groupB>groupA else -1

arr_ob = TestArray([0, 1, 1, 0, 1, 1, 1, 0])
print(solve(arr_ob))

Input

[0, 1, 1, 0, 1, 1, 1, 0]

Output

2

Updated on: 18-May-2021

160 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements