Python Program to Find the Minimum Difference Between the Index of Two Given Elements Present in an Array

If you are new to Java and want to learn the java coding skills too fast. Try practicing the core java programs with the help of the Java basic programs list available.

Lists in Python:

Lists are one of Python’s most commonly used built-in data structures. You can make a list by putting all of the elements inside square brackets[ ] and separating them with commas. Lists can include any type of object, making them extremely useful and adaptable.

Given a list/array, the task is to find the minimum difference between the index of given two elements present in a list in Python.

Examples:

Example1:

Input:

given list =[1, 3, 7, 10, 11, 13, 5, 4, 8, 2, 4, 3, 6, 5, 21, 8, 9]
given first element =2
given second element =5

Output:

The minimum difference between the elements 2 and 5 = 3

Example2:

Input:

given list =[7, 9, 6, 8, 7, 5, 9, 4, 3, 2, 11, 19, 23, 4, 5, 8, 6, 3 ,2, 9, 7,]
given first element =7
given second element =8

Output:

The minimum difference between the elements 7 and 8 = 1

Program to Find the Minimum Difference Between the Index of Two Given Elements Present in an Array in Python

Below is the full approach for finding the minimum difference between the index of given two elements present in a list in Python.

Method #1:Using min() function,For loop and If statements(Static Input)

Idea:

The intention is to traverse the array and maintain track of the most recent occurrences of x and y.
If the current element is x, calculate the absolute difference between the current index of x and the index of the last occurrence of y and, if necessary, update the result.
If the current element is y, calculate the absolute difference between the current index of y and the index of the last occurrence of x and, if necessary, update the result.

Input:

Give the list as static input and store it in a variable.

Give the two elements as static input and store them in separate variables.

Pass the given list and two elements as arguments to the findMinDiff function.

Print the minimum difference between the given two elements.

The Exit of the Program.

Below is the implementation:

import sys


# function which accepts the given list and the given two elements as arguments
# and return the minimum difference between the given two elements
def findMinDiff(given_list, firstele, secondele):

    firstindex = secondindex = len(given_list)
    min_differ = sys.maxsize

    # traverse the given list
    for i in range(len(given_list)):

        # if the current element is `firstele`
        if given_list[i] == firstele:
            # set `firstindex` to the current index
            firstindex = i

            # if `secondele` is seen before, update the result if required
            if secondindex != len(given_list):
                min_differ = min(min_differ, abs(firstindex - secondindex))

        # if the current element is `secondele`
        if given_list[i] == secondele:
            # set `secondindex` to the current index
            secondindex = i

            # if `firstindex` is seen before, update the result if required
            if firstindex != len(given_list):
                min_differ = min(min_differ, abs(firstindex - secondindex))

    return min_differ


# Driver Code
# Give the list as static input and store it in a variable.

given_list = [1, 3, 7, 10, 11, 13, 5, 4, 8, 2, 4, 3, 6, 5, 21, 8, 9]
# Give the two elements as static input and store them in separate variables.
firstele = 2
secondele = 5
# Pass the given list and two elements as arguments to the findMinDiff function.
miniDifference = findMinDiff(given_list, firstele, secondele)

if miniDifference != sys.maxsize:
    print("The minimum difference between the elements",
          firstele, 'and', secondele, '=', miniDifference)
# if the two elements are not found then the minidifference will
# be sys.maxsize in that case print it as invalid input
else:
    print("Invalid input")

Output:

The minimum difference between the elements 2 and 5 = 3

Method #2:Using min() function,For loop and If statements(User Input)

Idea:

The intention is to traverse the array and maintain track of the most recent occurrences of x and y.
If the current element is x, calculate the absolute difference between the current index of x and the index of the last occurrence of y and, if necessary, update the result.
If the current element is y, calculate the absolute difference between the current index of y and the index of the last occurrence of x and, if necessary, update the result.

Input:

Give the list as user input using map(), split(), and list functions and store it in a variable.

Give the two elements as user input using map(), split() functions, and store them in separate variables.

Pass the given list and two elements as arguments to the findMinDiff function.

Print the minimum difference between the given two elements.

The Exit of the Program.

Below is the implementation:

import sys


# function which accepts the given list and the given two elements as arguments
# and return the minimum difference between the given two elements
def findMinDiff(given_list, firstele, secondele):

    firstindex = secondindex = len(given_list)
    min_differ = sys.maxsize

    # traverse the given list
    for i in range(len(given_list)):

        # if the current element is `firstele`
        if given_list[i] == firstele:
            # set `firstindex` to the current index
            firstindex = i

            # if `secondele` is seen before, update the result if required
            if secondindex != len(given_list):
                min_differ = min(min_differ, abs(firstindex - secondindex))

        # if the current element is `secondele`
        if given_list[i] == secondele:
            # set `secondindex` to the current index
            secondindex = i

            # if `firstindex` is seen before, update the result if required
            if firstindex != len(given_list):
                min_differ = min(min_differ, abs(firstindex - secondindex))

    return min_differ


# Driver Code
# Give the list as user input using map(), split(), and list functions
# and store it in a variable.
given_list = list(map(int, input(
    'Enter some random list elements separated by spaces = ').split()))

# Give the two elements as user input using map(), split() functions,
# and store them in separate variables.
firstele, secondele = map(int, input(
    'Enter some random two elements which are present in the list separated by spaces = ').split())
# Pass the given list and two elements as arguments to the findMinDiff function.
miniDifference = findMinDiff(given_list, firstele, secondele)

if miniDifference != sys.maxsize:
    print("The minimum difference between the elements",
          firstele, 'and', secondele, '=', miniDifference)
# if the two elements are not found then the minidifference will
# be sys.maxsize in that case print it as invalid input
else:
    print("Invalid input")

Output:

Enter some random list elements separated by spaces = 7 9 6 8 7 5 9 4 3 2 11 19 23 4 5 8 6 3 2 9 7
Enter some random two elements which are present in the list separated by spaces = 7 8
The minimum difference between the elements 7 and 8 = 1

Related Programs:

Leave a Comment