
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
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 −
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
- Related Articles
- Python Program to find the largest element in an array
- Python Program to find largest element in an array
- Golang Program to Find the Largest Element in an Array
- Program to find out the index of the most frequent element in a concealed array in Python
- Program to find largest element in an array in C++
- C# Program to find the largest element from an array
- C++ Program to Find Largest Element of an Array
- Python Program to find the largest element in a tuple
- Python Program To Find The Largest Element In A Dictionary
- Program to find smallest index for which array element is also same as index in Python
- Kth Largest Element in an Array in Python
- Program to find length of the largest subset where one element in every pair is divisible by other in Python
- Write a program to find the index of particular element in an array in javascript?
- C# Program to find the largest element from an array using Lambda Expressions
- Java program to find the largest number in an array
