Program to find out the index in an array where the largest element is situated in Python


Suppose, we are given a class called 'TestArray' that contains an array that is not accessible by other classes and two public member functions length() and compare(). The function length() returns the length of the array and the function compare() returns three different values comparing various subarrays from the array. The function takes four values l, r, x, y as input and works like this −

  • if (array[l] + array[l+1]+......+array[r-1]+array[r]) > (array[x] + array[x+1]+......+array[y1]+array[y]); it returns 1

  • if (array[l] + array[l+1]+......+array[r-1]+array[r]) = (array[x] + array[x+1]+......+array[y1]+array[y]); it returns 0

  • if (array[l] + array[l+1]+......+array[r-1]+array[r]) < (array[x] + array[x+1]+......+array[y1]+array[y]); it returns -1

We have to find out the index of the maximum element in the array without accessing the array itself and using only the member functions of the class.

So, if the input is like array = [8, 4, 2, 12, 11, 8, 4, 2, 7], then the output will be 3. The largest element in the array is 12 and it is situated on index 3.

To solve this, we will follow these steps −

  • n := length()

  • low:= 0

  • high := n - 1

  • while low < high, do

    • mid := floor value of (low+high+1) / 2

    • if (low+high+1) mod 2 is same as 0, then

      • res := compare(low, mid-1, mid, high)

      • if res is same as 1, then

        • high := mid -1

      • otherwise,

        • low := mid

    • otherwise,

      • res := compare(low, mid-1, mid+1, high)

      • if res is same as 1, then

        • high := mid -1

      • otherwise when res is same as -1, then

        • low := mid -1

      • otherwise,

        • return mid

    • if high is same as low, then

      • return high

  • 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 compare(self, l, r, x, y):
      val1 = sum(i for i in self.__arr[l:r+1])
      val2 = sum(j for j in self.__arr[x:y+1])
      if val1 > val2:
         return 1
      elif val1 == val2:
         return 0
      elif val1 < val2:
         return -1

def solve(reader):
   n = reader.length()
   low,high = 0,n - 1
   while low < high:
      mid = (low+high+1)//2
      if (low+high+1)%2 == 0:
         res = reader.compare(low,mid-1,mid,high)
         if res == 1:high = mid - 1
         else:low = mid
      else:
         res = reader.compare(low,mid-1,mid+1,high)
         if res == 1:high = mid - 1
         elif res == -1:low = mid + 1
         else: return mid
      if high == low: return high
   return -1

arr_ob = TestArray([8, 4, 2, 12, 11, 8, 4, 2, 7])
print(solve(arr_ob))

Input

[8, 4, 2, 12, 11, 8, 4, 2, 7]

Output

3

Updated on: 18-May-2021

148 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements