
- 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 minimum possible difference of indices of adjacent elements in Python
Suppose we have a list of numbers nums, we can say that two numbers nums[i] ≤ nums[j] are adjacent when there is no number in between (nums[i], nums[j]) in nums. We have to find the minimum possible |j - i| such that nums[j] and nums[i] are adjacent.
So, if the input is like nums = [1, -9, 6, -6, 2], then the output will be 2, as we can see that 2 and 6 are adjacent and they are 2 indices away from each other.
To solve this, we will follow these steps −
indexes := a new map
for each index i and value x in A, do
insert i at the end of indexes[x]
ans := size of A
for each row in the list of all values of indexes, do
for i in range 0 to size of row - 2, do
ans := minimum of ans and (row[i + 1] - row[i])
vals := sort the list indexes
for k in range 0 to size of vals - 2, do
r1 := indexes[vals[k]]
r2 := indexes[vals[k + 1]]
i := j := 0
while i < size of r1 and j < size of r2, do
ans := minimum of ans and |r1[i] - r2[j]|
if r1[i] < r2[j], then
i := i + 1
otherwise,
j := j + 1
return ans
Example (Python)
Let us see the following implementation to get better understanding −
from collections import defaultdict class Solution: def solve(self, A): indexes = defaultdict(list) for i, x in enumerate(A): indexes[x].append(i) ans = len(A) for row in indexes.values(): for i in range(len(row) - 1): ans = min(ans, row[i + 1] - row[i]) vals = sorted(indexes) for k in range(len(vals) - 1): r1 = indexes[vals[k]] r2 = indexes[vals[k + 1]] i = j = 0 while i < len(r1) and j < len(r2): ans = min(ans, abs(r1[i] - r2[j])) if r1[i] < r2[j]: i += 1 else: j += 1 return ans ob = Solution() nums = [1, -9, 6, -6, 2] print(ob.solve(nums))
Input
[1, -9, 6, -6, 2]
Output
2
- Related Articles
- Program to find minimum possible integer after at most k adjacent swaps on digits in Python
- C++ program to find minimum possible difference of largest and smallest of crackers
- Program to find largest sum of non-adjacent elements of a list in Python
- Program to find sum of non-adjacent elements in a circular list in python
- Program to find minimum adjacent swaps for K consecutive ones in Python
- Program to find minimum difference between two elements from two lists in Python
- Find elements of a list by indices in Python
- Program to find minimum difference of stone games score in Python
- Program to find maximum difference of adjacent values after deleting k numbers in python
- Python program to remove elements at Indices in List
- Python Program to repeat elements at custom indices
- Program to find minimum difference of max and mins after updating elements at most three times in Python
- Program to find minimum absolute sum difference in Python
- Program to find minimum possible maximum value after k operations in python
- Maximum sum of difference of adjacent elements in C++
