{"id":10667,"date":"2021-09-30T16:00:38","date_gmt":"2021-09-30T10:30:38","guid":{"rendered":"https:\/\/python-programs.com\/?p=10667"},"modified":"2021-11-22T18:34:25","modified_gmt":"2021-11-22T13:04:25","slug":"python-program-to-find-the-minimum-difference-between-the-index-of-two-given-elements-present-in-an-array","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-program-to-find-the-minimum-difference-between-the-index-of-two-given-elements-present-in-an-array\/","title":{"rendered":"Python Program to Find the Minimum Difference Between the Index of Two Given Elements Present in an Array"},"content":{"rendered":"
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<\/a> available.<\/p>\n Lists in Python:<\/strong><\/p>\n Lists are one of Python\u2019s 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.<\/p>\n 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.<\/p>\n Examples:<\/strong><\/p>\n Example1:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Example2:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Below is the full approach for finding the minimum difference between the index of given two elements present in a list in Python.<\/p>\n Idea:<\/strong><\/p>\n The intention is to traverse the array and maintain track of the most recent occurrences of x and y. Input:<\/strong><\/p>\n Give the list as static input and store it in a variable.<\/p>\n Give the two elements as static input and store them in separate variables.<\/p>\n Pass the given list and two elements as arguments to the findMinDiff function.<\/p>\n Print the minimum difference between the given two elements.<\/p>\n The Exit of the Program.<\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Idea:<\/strong><\/p>\n The intention is to traverse the array and maintain track of the most recent occurrences of x and y. Input:<\/strong><\/p>\n Give the list as user input using map(), split(), and list functions and store it in a variable.<\/p>\n Give the two elements as user input using map(), split() functions, and store them in separate variables.<\/p>\n Pass the given list and two elements as arguments to the findMinDiff function.<\/p>\n Print the minimum difference between the given two elements.<\/p>\n The Exit of the Program.<\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Related Programs<\/strong>:<\/p>\n 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\u2019s most commonly used built-in data structures. You can make a list by putting all of …<\/p>\ngiven list =[1, 3, 7, 10, 11, 13, 5, 4, 8, 2, 4, 3, 6, 5, 21, 8, 9]\r\ngiven first element =2\r\ngiven second element =5<\/pre>\n
The minimum difference between the elements 2 and 5 = 3<\/pre>\n
given list =[7, 9, 6, 8, 7, 5, 9, 4, 3, 2, 11, 19, 23, 4, 5, 8, 6, 3 ,2, 9, 7,]\r\ngiven first element =7\r\ngiven second element =8<\/pre>\n
The minimum difference between the elements 7 and 8 = 1<\/pre>\n
Program to Find the Minimum Difference Between the Index of Two Given Elements Present in an Array in Python<\/h2>\n
\n
Method #1:Using min() function,For loop and If statements(Static Input)<\/h3>\n
\nIf 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.
\nIf 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.<\/p>\nimport sys\r\n\r\n\r\n# function which accepts the given list and the given two elements as arguments\r\n# and return the minimum difference between the given two elements\r\ndef findMinDiff(given_list, firstele, secondele):\r\n\r\n firstindex = secondindex = len(given_list)\r\n min_differ = sys.maxsize\r\n\r\n # traverse the given list\r\n for i in range(len(given_list)):\r\n\r\n # if the current element is `firstele`\r\n if given_list[i] == firstele:\r\n # set `firstindex` to the current index\r\n firstindex = i\r\n\r\n # if `secondele` is seen before, update the result if required\r\n if secondindex != len(given_list):\r\n min_differ = min(min_differ, abs(firstindex - secondindex))\r\n\r\n # if the current element is `secondele`\r\n if given_list[i] == secondele:\r\n # set `secondindex` to the current index\r\n secondindex = i\r\n\r\n # if `firstindex` is seen before, update the result if required\r\n if firstindex != len(given_list):\r\n min_differ = min(min_differ, abs(firstindex - secondindex))\r\n\r\n return min_differ\r\n\r\n\r\n# Driver Code\r\n# Give the list as static input and store it in a variable.\r\n\r\ngiven_list = [1, 3, 7, 10, 11, 13, 5, 4, 8, 2, 4, 3, 6, 5, 21, 8, 9]\r\n# Give the two elements as static input and store them in separate variables.\r\nfirstele = 2\r\nsecondele = 5\r\n# Pass the given list and two elements as arguments to the findMinDiff function.\r\nminiDifference = findMinDiff(given_list, firstele, secondele)\r\n\r\nif miniDifference != sys.maxsize:\r\n print(\"The minimum difference between the elements\",\r\n firstele, 'and', secondele, '=', miniDifference)\r\n# if the two elements are not found then the minidifference will\r\n# be sys.maxsize in that case print it as invalid input\r\nelse:\r\n print(\"Invalid input\")\r\n<\/pre>\n
The minimum difference between the elements 2 and 5 = 3<\/pre>\n
Method #2:Using min() function,For loop and If statements(User Input)<\/h3>\n
\nIf 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.
\nIf 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.<\/p>\nimport sys\r\n\r\n\r\n# function which accepts the given list and the given two elements as arguments\r\n# and return the minimum difference between the given two elements\r\ndef findMinDiff(given_list, firstele, secondele):\r\n\r\n firstindex = secondindex = len(given_list)\r\n min_differ = sys.maxsize\r\n\r\n # traverse the given list\r\n for i in range(len(given_list)):\r\n\r\n # if the current element is `firstele`\r\n if given_list[i] == firstele:\r\n # set `firstindex` to the current index\r\n firstindex = i\r\n\r\n # if `secondele` is seen before, update the result if required\r\n if secondindex != len(given_list):\r\n min_differ = min(min_differ, abs(firstindex - secondindex))\r\n\r\n # if the current element is `secondele`\r\n if given_list[i] == secondele:\r\n # set `secondindex` to the current index\r\n secondindex = i\r\n\r\n # if `firstindex` is seen before, update the result if required\r\n if firstindex != len(given_list):\r\n min_differ = min(min_differ, abs(firstindex - secondindex))\r\n\r\n return min_differ\r\n\r\n\r\n# Driver Code\r\n# Give the list as user input using map(), split(), and list functions\r\n# and store it in a variable.\r\ngiven_list = list(map(int, input(\r\n 'Enter some random list elements separated by spaces = ').split()))\r\n\r\n# Give the two elements as user input using map(), split() functions,\r\n# and store them in separate variables.\r\nfirstele, secondele = map(int, input(\r\n 'Enter some random two elements which are present in the list separated by spaces = ').split())\r\n# Pass the given list and two elements as arguments to the findMinDiff function.\r\nminiDifference = findMinDiff(given_list, firstele, secondele)\r\n\r\nif miniDifference != sys.maxsize:\r\n print(\"The minimum difference between the elements\",\r\n firstele, 'and', secondele, '=', miniDifference)\r\n# if the two elements are not found then the minidifference will\r\n# be sys.maxsize in that case print it as invalid input\r\nelse:\r\n print(\"Invalid input\")\r\n<\/pre>\n
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\r\nEnter some random two elements which are present in the list separated by spaces = 7 8\r\nThe minimum difference between the elements 7 and 8 = 1<\/pre>\n
\n