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

Finding the index of the largest element in an array is a common problem. This tutorial demonstrates a unique approach where the array is encapsulated in a class and can only be accessed through specific member functions.

Problem Statement

We have a class called TestArray that contains an array not accessible by other classes. It provides two public member functions:

  • length() − returns the length of the array

  • compare(l, r, x, y) − compares sums of two subarrays

The compare() function works as follows:

  • If sum(array[l:r+1]) > sum(array[x:y+1]), it returns 1

  • If sum(array[l:r+1]) = sum(array[x:y+1]), it returns 0

  • If sum(array[l:r+1]) -1

We need to find the index of the maximum element using only these member functions ?

Algorithm

We use a binary search approach to divide the array and compare subarrays:

  • Initialize low = 0 and high = n - 1

  • While low :

    • Calculate mid = (low + high + 1) // 2

    • Compare left and right subarrays using the compare function

    • Narrow down the search space based on comparison result

Implementation

class TestArray:
    def __init__(self, array):
        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

# Test the implementation
arr_ob = TestArray([8, 4, 2, 12, 11, 8, 4, 2, 7])
result = solve(arr_ob)
print(f"Index of maximum element: {result}")
Index of maximum element: 3

How It Works

The algorithm uses binary search with the following logic:

  • Even total elements: Compare left half with right half directly

  • Odd total elements: Compare left half with right half, excluding middle element

  • If left sum > right sum, maximum is in the left half

  • If right sum > left sum, maximum is in the right half

  • If sums are equal (odd case), the middle element is the maximum

Example Walkthrough

For array [8, 4, 2, 12, 11, 8, 4, 2, 7]:

  • Initial: low=0, high=8

  • Compare left half [8,4,2,12] (sum=26) with right half [11,8,4,2,7] (sum=32)

  • Right sum is larger, so maximum is in right half: low=5

  • Continue narrowing until we find index 3 (element 12)

Conclusion

This approach demonstrates how to find the maximum element's index using only comparison operations. The binary search technique reduces time complexity while working within the constraints of the encapsulated array class.

Updated on: 2026-03-25T20:36:27+05:30

259 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements