Finding the Maximum Distance between Elements using Python


Introduction

The list data structure deals with the elements of different data types like integer numbers, float number or string. The elements need to be defined within the square brackets separated by comma. The Python language is primarily composed of different data structures and from it the list data structure is the most used one. The lists can hold elements of different data types and when they are assigned with some values they cannot be changed. In this article, we will be finding the maximum distance between the elements.

Maximum Distance between the Elements

The elements in the list are identified using the index value and based on this index value, the maximum distance is calculated. Let’s take an example of a list data structure,

List1 = [1, 3, 5, 1, 5, 7, 8]

The distance is found for the same element which is located at a maximum distance and in the above case, the element in index 0 is 1 and the same element is located at the index value of 3. So, the maximum distance between the elements is 3.

Approach

Approach 1: Using the numpy module

Approach 2: Using the iteration method

Approach 1: To find the Maximum distance between Elements in Python using the Numpy Module

The list is declared with seven integer elements and the distance value is calculated using the numpy.

Algorithm

Step 1 :The input list is initialized with seven integer data types along with the module.

Step 2 :The variable named “maximum_distance” is assigned the value of zero.

Step 3 :With the help of a unique function, the unique elements are found in the list by going through every element of the list and storing it in a variable.

Step 4 :Set the variable named “maximum_distance” to zero.

Step 5 :The method of finding the distance needs the location of the elements, based on which the element’s maximum distance can be calculated.

Step 6 :The distance is calculated by taking the difference between the max and min functions of the input list.

Step 7 :The output is printed.

Example

#import the module
import numpy as np
#initializing the list with the same elements at different index values
List1 = [1, 3, 5, 2, 1, 7, 8]
#function is used to get the specific element of the list
val = np.unique(List1)
#Storing the element value as ‘0’
maximum_dis = 0
#for loop will iterate through the list
for uni in val:
#To get the distance, the where the function is used to get the index value
	location = np.where(np.array(List1) == uni)[0]
#The distance returns the difference between the max and min index value
	dis = np.max(location) - np.min(location)
#When the ‘dis’ is greater than the maximum distance, the value is returned.
	if dis > maximum_dis:
		maximum_dis = dis
#Print functions return the final value
print("Maximum distance obtained in the given list:", maximum_dis)

Output

Maximum distance obtained in the given list: 4

Approach 2: Python Program to find the Maximum distance between Elements using the Iteration Method

The list data structure is used in the case to initialize the input with a set of integer elements and to find the maximum distance between the elements, the elements with the same value at different indexes are initialized and the maximum distance is calculated.

Algorithm

Step 1 :Initialize the list.

Step 2 :The variable named “maximum_distance” is assigned the value of zero.

Step 3 :The nested for loops are used, the for loop will iterate through the given list to get the length of the list.

Step 4 :Next the second for loop will iterate through each element to find the maximum distance between the elements of the list.

Step 5 :The abs() function will return the absolute value of the indices (a,b).

Step 6 :Then the final element which is present at a maximum distance is found and the element’s distance is returned.

Example

def element_max(gt):
    nonduplicate_elems = gt
    mt = 0
    for x in nonduplicate_elems:
        nd = [m for m, r in enumerate(gt) if r == x]
        t = max(nd) - min(nd)
        if t > mt:
            mt = t
    return mt
gt = [4, 9, 6, 2, 9, 1]
mt = element_max(gt)
print("The longest distance is:", mt)

Output

The longest distance is: 3

Conclusion

The Python list can have elements in all forms like positive and negative values. There are different approaches explained to deal with the abs() − an absolute function, a unique method to get the unique values, and finally the where the function to get the location of the elements indice. These methods will help the programmer to find the maximum distance between elements in the defined values.

Updated on: 07-Aug-2023

243 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements