You are given an array A, of N elements. Find minimum index based distance between two elements of the array, x and y such that (x!=y).
Example 1:
Input:
N = 4
A[] = {1,2,3,2}
x = 1, y = 2
Output: 1
Explanation: x = 1 and y = 2. There are
two distances between x and y, which are
1 and 3 out of which the least is 1.
Example 2:
Input:
N = 7
A[] = {86,39,90,67,84,66,62}
x = 42, y = 12
Output: -1
Explanation: x = 42 and y = 12. We return
-1 as x and y don't exist in the array.
Your Task:
Complete the function minDist() which takes the array, n, x and y as input parameters and returns the minimum distance between x and y in the array. If no such distance is possible then return -1.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(1)
Constraints:
1 <= N <= 10^{5}
0 <= A[i], x, y <= 10^{5}

Difficulty: EASY
Tags: Data structures
Skill types: Data structures
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(1)

Starter code:


class Solution:

    def minDist(self, arr, n, x, y):

        

    

Solutions:
class Solution:

	def minDist(self, arr, n, x, y):
		if x == y:
			return -1
		id_x = -1
		id_y = -1
		mini = pow(10, 5) + 1
		for i in range(n):
			if arr[i] == x:
				id_x = i
			if arr[i] == y:
				id_y = i
			if id_x != -1 and id_y != -1:
				if abs(id_x - id_y) < mini:
					mini = abs(id_x - id_y)
		if id_x == -1 or id_y == -1:
			return -1
		return mini


class Solution:

	def minDist(self, arr, n, x, y):
		(p, ans) = (-1, float('inf'))
		for (i, num) in enumerate(arr):
			if num == x or num == y:
				if p != -1 and num != arr[p]:
					ans = min(ans, i - p)
				p = i
		if ans == float('inf'):
			return -1
		return ans


class Solution:

	def minDist(self, arr, n, x, y):
		mind = 10 ** 5
		(a, b) = (-1, -1)
		if x == y:
			return 0
		for i in range(n):
			if arr[i] == x:
				a = i
			if arr[i] == y:
				b = i
			if a != -1 and b != -1:
				mind = min(mind, abs(a - b))
		if x not in arr or y not in arr:
			return -1
		return mind


class Solution:

	def minDist(self, arr, n, x, y):
		(xind, yind, min1) = (-1, -1, float('inf'))
		for i in range(n):
			if arr[i] == x:
				xind = i
			elif arr[i] == y:
				yind = i
			if xind != -1 and yind != -1:
				min1 = min(min1, abs(xind - yind))
		if min1 == float('inf'):
			return -1
		return min1


class Solution:

	def minDist(self, arr, n, x, y):
		N = len(arr)
		lastseen = -1
		minDist = N
		for i in range(N):
			if arr[i] == x or arr[i] == y:
				if lastseen != -1 and arr[i] != arr[lastseen]:
					minDist = min(minDist, i - lastseen)
				lastseen = i
		if minDist == N:
			return -1
		else:
			return minDist


class Solution:

	def minDist(self, arr, n, x, y):
		import sys
		p = -1
		min_dist = sys.maxsize
		for i in range(len(arr)):
			if arr[i] == x or arr[i] == y:
				if p != -1 and arr[i] != arr[p]:
					min_dist = min(min_dist, i - p)
				p = i
		if min_dist == sys.maxsize:
			return -1
		return min_dist


class Solution:

	def minDist(self, arr, n, x, y):
		if n < 2:
			return -1
		ind_x = -1
		ind_y = -1
		min_dis = float('inf')
		for i in range(n):
			if arr[i] == x or arr[i] == y:
				if arr[i] == x:
					ind_x = i
					if ind_y != -1:
						min_dis = min(min_dis, abs(ind_x - ind_y))
				elif arr[i] == y:
					ind_y = i
					if ind_x != -1:
						min_dis = min(min_dis, abs(ind_x - ind_y))
		if min_dis == float('inf'):
			return -1
		else:
			return min_dis


class Solution:

	def minDist(self, arr, n, x, y):
		a = -1
		b = -1
		min_d = 99999999
		for i in range(n):
			if arr[i] == x:
				a = i
			elif arr[i] == y:
				b = i
			if a != -1 and b != -1:
				min_d = min(min_d, abs(a - b))
		if min_d == 99999999:
			return -1
		return min_d


class Solution:

	def minDist(self, arr, n, x, y):
		xi = -1
		yi = -1
		min_d = 99999999
		for i in range(n):
			if x == arr[i]:
				xi = i
			elif y == arr[i]:
				yi = i
			if xi != -1 and yi != -1:
				min_d = min(min_d, abs(xi - yi))
		if min_d == 99999999:
			return -1
		return min_d


class Solution:

	def minDist(self, arr, n, x, y):
		mindistance = float('inf')
		p1 = float('inf')
		p2 = float('inf')
		if x in arr and y in arr:
			for i in range(n):
				if x == arr[i]:
					p1 = i
				if y == arr[i]:
					p2 = i
				mindistance = min(abs(p1 - p2), mindistance)
			return mindistance
		else:
			return -1


class Solution:

	def minDist(self, arr, n, x, y):
		xi = -1
		yi = -1
		mindis = float('inf')
		for i in range(n):
			if arr[i] == x:
				xi = i
			if arr[i] == y:
				yi = i
			if xi != -1 and yi != -1:
				dis = abs(xi - yi)
				if dis < mindis:
					mindis = dis
		if mindis == float('inf'):
			return -1
		else:
			return mindis


class Solution:

	def minDist(self, arr, n, x, y):
		ind1 = -1
		ind2 = -1
		previousxoryindex = -1
		ans = 1000000
		for i in range(n):
			if arr[i] == x or arr[i] == y:
				if previousxoryindex != -1 and arr[previousxoryindex] != arr[i]:
					ans = min(ans, i - previousxoryindex)
				previousxoryindex = i
		if ans == 1000000:
			return -1
		else:
			return ans


class Solution:

	def minDist(self, arr, n, x, y):
		xind = -1
		yind = -1
		mindif = 9999999
		for i in range(n):
			if arr[i] == x:
				xind = i
			elif arr[i] == y:
				yind = i
			if xind != -1 and yind != -1:
				mindif = min(mindif, abs(xind - yind))
		if mindif == 9999999:
			return -1
		return mindif


class Solution:

	def minDist(self, array, n, x, y):
		passing = -1
		INT_MAX = 100000
		minimum = INT_MAX
		for i in range(n):
			if array[i] == x or array[i] == y:
				if passing != -1 and array[i] != array[passing]:
					minimum = min(minimum, i - passing)
				passing = i
		if minimum == INT_MAX:
			return -1
		return minimum


class Solution:

	def minDist(self, arr, n, x, y):
		if y not in arr or x not in arr:
			return -1
		a = arr.index(x)
		b = arr.index(y)
		mn = abs(a - b)
		k = min(a, b)
		for i in range(k, n):
			if x == arr[i]:
				a = i
				mn = min(mn, abs(a - b))
			elif arr[i] == y:
				b = i
				mn = min(mn, abs(a - b))
		return mn


class Solution:

	def minDist(self, arr, n, x, y):
		if x not in arr or y not in arr:
			return -1
		ix = arr.index(x)
		iy = arr.index(y)
		least = abs(ix - iy)
		start = min(ix, iy)
		for i in range(start, n):
			if arr[i] == x:
				ix = i
				least = min(least, abs(ix - iy))
			elif arr[i] == y:
				iy = i
				least = min(least, abs(ix - iy))
		return least


class Solution:

	def minDist(self, arr, n, x, y):
		(recent_x, recent_y) = (-1, -1)
		ans = 9999999
		for i in range(n):
			if arr[i] == x:
				recent_x = i
				if recent_y != -1:
					ans = min(ans, abs(recent_x - recent_y))
			elif arr[i] == y:
				recent_y = i
				if recent_x != -1:
					ans = min(ans, abs(recent_x - recent_y))
		if ans == 9999999:
			return -1
		return ans


class Solution:

	def minDist(self, arr, n, x, y):
		num1 = -1
		num2 = -1
		diff = -1
		res = float('inf')
		if x not in arr:
			return -1
		if y not in arr:
			return -1
		for i in range(0, n):
			if arr[i] == x:
				num1 = i
			if arr[i] == y:
				num2 = i
			if num1 != -1 and num2 != -1:
				diff = abs(num2 - num1)
				res = min(res, diff)
		return res


import sys

class Solution:

	def minDist(self, arr, n, x, y):
		dist = sys.maxsize
		prev = -1
		for i in range(n):
			if arr[i] == x or arr[i] == y:
				if prev != -1 and arr[prev] != arr[i]:
					dist = min(dist, i - prev)
				prev = i
		if dist == sys.maxsize:
			return -1
		else:
			return dist


class Solution:

	def minDist(self, arr, n, x, y):
		ary = []
		arx = []
		diff = []
		for a in range(n):
			if arr[a] == y:
				ary.append(a + 1)
		for b in range(n):
			if arr[b] == x:
				arx.append(b + 1)
		if (len(ary) != 0) & (len(arx) != 0):
			for j in range(len(arx)):
				for k in range(len(ary)):
					diff.append(abs(arx[j] - ary[k]))
			return min(diff)
		else:
			return -1


class Solution:

	def minDist(self, arr, n, x, y):
		l = []
		l1 = []
		(a, b) = (-1, -1)
		for i in range(n):
			if arr[i] == x:
				a += 1
				l.append(i)
			if arr[i] == y:
				l1.append(i)
				b += 1
		if a == -1 or b == -1:
			return -1
		min = 1000000000
		for b1 in l1:
			for a1 in l:
				if abs(b1 - a1) < min:
					min = abs(b1 - a1)
		return min


import sys

class Solution:

	def minDist(self, arr, n, x, y):
		max_value = sys.maxsize
		if x not in arr or y not in arr:
			return -1
		if x == y:
			return 0
		x_index = -1
		y_index = -1
		for i in range(n):
			if arr[i] == x:
				x_index = i
			if arr[i] == y:
				y_index = i
			if x_index != -1 and y_index != -1:
				dist = min(max_value, abs(x_index - y_index))
				max_value = dist
		return dist


class Solution:

	def minDist(self, arr, n, x, y):
		if x not in arr or y not in arr:
			return -1
		last_index = None
		count = float('inf')
		for (i, num) in enumerate(arr):
			if num == x or num == y:
				if last_index is not None and num != arr[last_index]:
					count = min(count, i - last_index)
				last_index = i
		return count


class Solution:

	def minDist(self, arr, n, x, y):
		lx = []
		ly = []
		for i in range(0, n):
			if arr[i] == x:
				lx.append(i)
			elif arr[i] == y:
				ly.append(i)
		mindist = 100000
		for i in lx:
			for j in ly:
				mindist = min(mindist, abs(i - j))
		if mindist == 100000:
			return -1
		return mindist


class Solution:

	def minDist(self, arr, n, x, y):
		xi = -1
		yi = -1
		diff = n
		for i in range(n):
			if arr[i] == x:
				if yi > -1:
					if abs(yi - i) < diff:
						diff = abs(yi - i)
				xi = i
			if arr[i] == y:
				if xi > -1:
					if abs(i - xi) < diff:
						diff = abs(xi - i)
				yi = i
		if diff == n:
			return -1
		return diff


class Solution:

	def minDist(self, arr, n, x, y):
		mindist = n
		previndex = -1
		for i in range(n):
			if arr[i] == x or arr[i] == y:
				if previndex != -1 and arr[i] != arr[previndex]:
					mindist = min(mindist, i - previndex)
				previndex = i
		if mindist == n:
			return -1
		return mindist


class Solution:

	def minDist(self, arr, n, x, y):
		list1 = []
		list2 = []
		for i in range(0, len(arr)):
			if arr[i] == x:
				list1.append(i)
			elif arr[i] == y:
				list2.append(i)
		min_dist = 100000
		for i in range(0, len(list1)):
			for j in range(0, len(list2)):
				dist = abs(list1[i] - list2[j])
				min_dist = min(min_dist, dist)
		if min_dist == 100000:
			return -1
		return min_dist


class Solution:

	def minDist(self, arr, n, x, y):
		if x not in arr or y not in arr:
			return -1
		c = 10 ** 9 + 7
		b = -1
		d = -1
		for i in range(len(arr)):
			if arr[i] == x:
				if d != -1 and i - d < c:
					c = i - d
				b = i
			if arr[i] == y:
				if b != -1 and i - b < c:
					c = i - b
				d = i
		return c


class Solution:

	def minDist(self, arr, n, x, y):
		x_index = -1
		y_index = -1
		min_dist = float('inf')
		for i in range(n):
			if arr[i] == x:
				x_index = i
				if y_index != -1:
					min_dist = min(min_dist, abs(x_index - y_index))
			elif arr[i] == y:
				y_index = i
				if x_index != -1:
					min_dist = min(min_dist, abs(x_index - y_index))
		if min_dist == float('inf'):
			return -1
		return min_dist


class Solution:

	def minDist(self, arr, n, x, y):
		min_dist = float('inf')
		x_idx = -1
		y_idx = -1
		for i in range(n):
			if arr[i] == x:
				x_idx = i
				if y_idx != -1:
					min_dist = min(min_dist, abs(x_idx - y_idx))
			elif arr[i] == y:
				y_idx = i
				if x_idx != -1:
					min_dist = min(min_dist, abs(y_idx - x_idx))
		if min_dist == float('inf'):
			return -1
		return min_dist


class Solution:

	def minDist(self, arr, n, x, y):
		p = -1
		min_dist = 99999999999
		for i in range(n):
			if arr[i] == x or arr[i] == y:
				if p != -1 and arr[i] != arr[p]:
					min_dist = min(min_dist, i - p)
				p = i
		if min_dist == 99999999999:
			return -1
		return min_dist


class Solution:

	def minDist(self, arr, n, x, y):
		if x not in arr or y not in arr:
			return -1
		xi = [i for i in range(n) if arr[i] == x]
		yi = [i for i in range(n) if arr[i] == y]
		res = 0
		for i in range(len(xi)):
			for j in range(len(yi)):
				k = abs(xi[i] - yi[j])
				if i == 0 and j == 0:
					res = k
				else:
					res = min(res, k)
		return res


import sys

class Solution:

	def minDist(self, arr, n, x, y):
		dist = sys.maxsize
		(a, b) = (-1, -1)
		for i in range(0, n):
			if arr[i] == x:
				a = i
			if arr[i] == y:
				b = i
			if a != -1 and b != -1:
				dist = min(dist, abs(b - a))
		return dist if dist != sys.maxsize else -1


class Solution:

	def minDist(self, arr, n, x, y):
		x_idx = []
		y_idx = []
		for i in range(0, n):
			if arr[i] == x:
				x_idx.append(i)
			if arr[i] == y:
				y_idx.append(i)
		min = 1000000
		for i in x_idx:
			for j in y_idx:
				diff = abs(i - j)
				if diff < min:
					min = diff
		if min == 1000000:
			return -1
		else:
			return min


import sys

class Solution:

	def minDist(self, arr, n, x, y):
		min_distance = sys.maxsize
		id1 = -1
		id2 = -1
		for i in range(n):
			if arr[i] == x:
				id1 = i
			elif arr[i] == y:
				id2 = i
			if id1 != -1 and id2 != -1:
				min_distance = min(min_distance, abs(id1 - id2))
		if id1 == -1 or id2 == -1:
			return -1
		else:
			return min_distance


class Solution:

	def minDist(self, arr, n, x, y):
		l = []
		prev = -1
		for i in range(n):
			if arr[i] == x or arr[i] == y:
				if prev != -1 and arr[i] != arr[prev]:
					l.append(i - prev)
				prev = i
		if prev == -1 or len(l) == 0:
			return -1
		return min(l)


class Solution:

	def minDist(self, arr, n, x, y):
		if x == y:
			return -1
		x_id = -1
		y_id = -1
		min_dif = 999999
		for i in range(n):
			if arr[i] == x:
				x_id = i
			if arr[i] == y:
				y_id = i
			if x_id != -1 and y_id != -1 and (abs(x_id - y_id) < min_dif):
				min_dif = abs(x_id - y_id)
		if min_dif == 999999:
			return -1
		return min_dif


class Solution:

	def minDist(self, arr, n, x, y):
		if x == y:
			return -1
		mini_dis = 1000000000.0
		(ind1, ind2) = (-1, -1)
		for i in range(n):
			if arr[i] == x:
				ind1 = i
			if arr[i] == y:
				ind2 = i
			if ind1 != -1 and ind2 != -1:
				mini_dis = min(mini_dis, abs(ind1 - ind2))
		if ind1 == -1 or ind2 == -1:
			return -1
		return mini_dis


class Solution:

	def minDist(self, arr, n, x, y):
		a = []
		b = []
		c = 0
		d = 1
		if x != y:
			if x not in arr or y not in arr:
				return -1
			else:
				for i in range(len(arr)):
					if arr[i] == x:
						a.append(i)
					elif arr[i] == y:
						b.append(i)
				for i in a:
					for j in b:
						if abs(i - j) > 0:
							if d == 1:
								c = abs(i - j)
								d = 2
							elif c > abs(i - j):
								c = abs(i - j)
		return c


class Solution:

	def minDist(self, arr, n, x, y):
		last_x = -1
		last_y = -1
		min_dist = float('inf')
		for i in range(n):
			if arr[i] == x:
				last_x = i
				if last_y != -1:
					min_dist = min(min_dist, abs(last_x - last_y))
			elif arr[i] == y:
				last_y = i
				if last_x != -1:
					min_dist = min(min_dist, abs(last_x - last_y))
		if min_dist == float('inf'):
			return -1
		return min_dist


class Solution:

	def minDist(self, arr, n, x, y):
		xee = -1
		yee = -1
		mee = 99999
		for i in range(n):
			if arr[i] == x:
				xee = i
			elif arr[i] == y:
				yee = i
			if xee != -1 and yee != -1:
				mee = min(mee, abs(xee - yee))
		if mee == 99999:
			return -1
		return mee


import math

class Solution:

	def minDist(self, arr, n, x, y):
		x_indices = []
		y_indices = []
		for i in range(n):
			if arr[i] == x:
				x_indices.append(i)
			if arr[i] == y:
				y_indices.append(i)
		min_distance = math.inf
		for i in x_indices:
			for j in y_indices:
				distance = max(i, j) - min(i, j)
				if distance < min_distance:
					min_distance = distance
		if min_distance == math.inf:
			return -1
		return min_distance


import sys

class Solution:

	def minDist(self, arr, n, x, y):
		prevposxory = -1
		ans = sys.maxsize
		for i in range(n):
			if arr[i] == x or arr[i] == y:
				if prevposxory != -1 and arr[prevposxory] != arr[i]:
					ans = min(ans, abs(prevposxory - i))
				prevposxory = i
		if ans == sys.maxsize:
			return -1
		else:
			return ans


class Solution:

	def minDist(self, arr, n, x, y):
		lis1 = []
		lis2 = []
		for i in range(len(arr)):
			if arr[i] == x:
				lis1.append(i)
		for i in range(len(arr)):
			if arr[i] == y:
				lis2.append(i)
		min_ = 10000000
		if lis1 == [] or lis2 == []:
			return -1
		for i in lis1:
			for j in lis2:
				min_ = min(min_, abs(i - j))
		return min_


class Solution:

	def minDist(self, arr, n, x, y):
		xind = -1
		yind = -1
		mindif = pow(10, 5) + 1
		for i in range(n):
			if arr[i] == x:
				xind = i
			elif arr[i] == y:
				yind = i
			if xind != -1 and yind != -1:
				mindif = min(mindif, abs(xind - yind))
		if mindif == pow(10, 5) + 1:
			return -1
		else:
			return mindif


class Solution:

	def minDist(self, arr, n, x, y):
		min_dictance = float('inf')
		x_index = -1
		y_index = -1
		distance = None
		for index in range(0, n):
			if arr[index] == x:
				x_index = index
			if arr[index] == y:
				y_index = index
			if x_index != -1 and y_index != -1:
				distance = y_index - x_index if y_index - x_index >= 0 else (y_index - x_index) * -1
			if distance is not None:
				if distance < min_dictance:
					min_dictance = distance
		if distance == None:
			return -1
		else:
			return min_dictance


class Solution:

	def minDist(self, arr, n, x, y):
		dist = 0
		min_dist = n
		for i in range(n):
			if arr[i] == x:
				x_index = i
			if arr[i] == y:
				y_index = i
			try:
				dist = abs(y_index - x_index)
				min_dist = min(min_dist, dist)
			except:
				pass
		if dist == 0:
			return -1
		else:
			return min_dist


class Solution:

	def minDist(self, arr, n, x, y):
		ind_x = None
		ind_y = None
		num_dis = abs(x - y)
		ind_dis = None
		result_set = None
		for i in range(n):
			if arr[i] == x:
				ind_x = i
			elif arr[i] == y:
				ind_y = i
			if ind_x != None and ind_y != None:
				if result_set == None:
					result_set = (ind_x, ind_y)
				elif abs(result_set[0] - result_set[1]) > abs(ind_x - ind_y):
					result_set = (ind_x, ind_y)
		if ind_x == None or ind_y == None:
			return -1
		return abs(result_set[0] - result_set[1])


class Solution:

	def minDist(self, arr, n, x, y):
		if x not in arr or y not in arr:
			return -1
		m = []
		s = []
		for i in range(n):
			if arr[i] == x:
				m.append(i)
			if arr[i] == y:
				s.append(i)
		mi = 9999999999
		for i in m:
			for j in s:
				mi = min(mi, abs(i - j))
		return mi


import math

class Solution:

	def minDist(self, arr, n, x, y):
		if x not in arr or y not in arr:
			return -1
		xd = -1
		yd = -1
		val = math.inf
		for i in range(0, len(arr)):
			if arr[i] == x:
				xd = i
			if arr[i] == y:
				yd = i
			if xd != -1 and yd != -1:
				val = min(abs(xd - yd), val)
		return val


import math

class Solution:

	def minDist(self, arr, n, x, y):
		leftX = -math.inf
		leftY = -math.inf
		minDist = math.inf
		for i in range(n):
			if arr[i] == x:
				leftX = i
				if abs(i - leftY) < minDist:
					minDist = abs(i - leftY)
			if arr[i] == y:
				leftY = i
				if abs(i - leftX) < minDist:
					minDist = abs(i - leftX)
		if leftX < 0 or leftY < 0:
			return -1
		else:
			return minDist


class Solution:

	def minDist(self, arr, n, x, y):
		ix = -1
		iy = -1
		d = 0
		l = []
		for i in range(len(arr)):
			if arr[i] == x:
				ix = i
			if arr[i] == y:
				iy = i
			if ix != -1 and iy != -1:
				d = abs(ix - iy)
				l.append(d)
		if l:
			return min(l)
		else:
			return -1


class Solution:

	def minDist(self, arr, n, x, y):
		l = arr
		lx = []
		ly = []
		if x not in arr or y not in arr:
			return -1
		for i in range(len(arr)):
			if l[i] == x:
				lx.append(i)
			if l[i] == y:
				ly.append(i)
		min1 = 2 * len(arr)
		for i in lx:
			for j in ly:
				diff = i - j
				if diff < 0:
					diff = -1 * diff
				if diff < min1:
					min1 = diff
		return min1


class Solution:

	def minDist(self, arr, n, x, y):
		prev_x = -1
		prev_y = -1
		min_dist = float('inf')
		for i in range(n):
			if arr[i] == x:
				if prev_y != -1:
					min_dist = min(min_dist, i - prev_y)
				prev_x = i
			elif arr[i] == y:
				if prev_x != -1:
					min_dist = min(min_dist, i - prev_x)
				prev_y = i
		if min_dist == float('inf'):
			return -1
		else:
			return min_dist