Given an unsorted array Arr[] of N integers and a Key which is present in this array. You need to write a program to find the start index( index where the element is first found from left in the array ) and end index( index where the element is first found from right in the array ).If the key does not exist in the array then return -1 for both start and end index in this case.
Example 1:
Input:
N = 6
arr[] = { 1, 2, 3, 4, 5, 5 }
Key = 5
Output:  4 5
Explanation:
5 appears first time at index 4 and
appears last time at index 5.
(0 based indexing)
 
Example 2:
Input:
N=6
arr[] = { 6, 5, 4, 3, 1, 2 }
Key = 4
Output:  2 2 
Your Task:
Since, this is a function problem. You don't need to take any input, as it is already accomplished by the driver code. You just need to complete the function findIndex() that takes array a, integer N and integer key as parameters and returns an array of length 2 in which at first index contains the value of start index and at the second index contains the value of end index. If the key does not exist in the array then return -1 for both start and end index in this case.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(1).
Constraints:
1 ≤ N ≤ 10^{6}

Source: geeksforgeeks | Difficulty: EASY
Tags: Data structures

Starter code: #User function Template for python3



class Solution:

    def findIndex (self,a, N, key ):

        #code here.

Solutions:
class Solution:

	def findIndex(self, a, N, key):
		res = []
		for i in range(0, n):
			if a[i] == key:
				res.append(i)
		if len(res) >= 1:
			return [res[0], res[-1]]
		else:
			return [-1, -1]


class Solution:

	def findIndex(self, a, N, key):
		first_time = -1
		last_time = -1
		for i in range(N):
			if a[i] == key:
				if first_time < 0:
					first_time = i
				last_time = i
		if first_time == -1:
			return [-1, -1]
		return [first_time, last_time]


class Solution:

	def findIndex(self, a, N, key):
		first = -1
		end = -1
		loop_count = 0
		curr_index = 0
		while loop_count < N:
			if a[curr_index] == key and first < 0:
				first = curr_index
				curr_index = len(a)
			elif a[curr_index] == key:
				end = curr_index
				break
			curr_index += 1 if first < 0 else -1
			loop_count += 1
			if loop_count == N and first >= 0:
				end = first
		return [first, end]


class Solution:

	def findIndex(self, a, N, key):
		start = -1
		end = -1
		for i in range(N):
			if a[i] == key and start < 0:
				start = i
			if a[i] == key:
				end = i
		return [start, end]


class Solution:

	def findIndex(self, a, N, key):
		sol = []
		for i in range(N):
			if a[i] == key:
				li = i
				break
		for j in reversed(range(N)):
			if a[j] == key:
				ri = j
				break
		if key not in a:
			li = -1
			ri = -1
		sol.append(li)
		sol.append(ri)
		return sol


class Solution:

	def findIndex(self, array, numOfElements, key):
		start = -1
		end = -1
		for i in range(numOfElements):
			if array[i] == key and start < 0:
				start = i
			if array[i] == key:
				end = i
		return [start, end]


class Solution:

	def findIndex(self, a, N, key):
		keys = [-1, -1]
		for i in range(0, N):
			if a[i] == key:
				keys[0] = i
				break
		for j in range(N - 1, -1, -1):
			if a[j] == key:
				keys[1] = j
				break
		return keys


class Solution:

	def findIndex(self, a, N, key):
		first = -1
		last = -1
		if key in a:
			first = a.index(key)
			last = n - a[::-1].index(key) - 1
		return (first, last)


class Solution:

	def findIndex(self, a, N, key):
		z = []
		if key not in a:
			return (-1, -1)
		for i in range(N):
			if key == a[i]:
				z.append(i)
		return (min(z), max(z))


class Solution:

	def findIndex(self, a, N, key):
		ilist = []
		for (i, n) in enumerate(a):
			if key == n:
				ilist.append(i)
		if len(ilist):
			return (ilist[0], ilist[-1])
		else:
			return (-1, -1)


class Solution:

	def findIndex(self, a, N, key):
		(start, end) = (0, N - 1)
		while start < N:
			if a[start] == key:
				break
			start += 1
		if start == N:
			return (-1, -1)
		while end > -1:
			if a[end] == key:
				break
			end -= 1
		return (start, end)


class Solution:

	def findIndex(self, arr, n, k):
		first = second = -1
		for num in range(n):
			if arr[num] == k:
				if first == -1:
					first = num
				second = num
		return (first, second)


class Solution:

	def findIndex(self, a, N, key):
		l = [-1, -1]
		nl = []
		if key not in a:
			return l
		for i in range(N):
			if key == a[i]:
				nl.append(i)
				break
		for i in range(N - 1, -1, -1):
			if key == a[i]:
				nl.append(i)
				break
		return nl


class Solution:

	def findIndex(self, a, N, key):
		start = 0
		end = N - 1
		fin1 = -1
		fin2 = -1
		while start < N and end >= 0:
			if fin1 == -1 and a[start] == key:
				fin1 = start
			if fin2 == -1 and a[end] == key:
				fin2 = end
			start += 1
			end -= 1
		return (fin1, fin2)


class Solution:

	def findIndex(self, a, N, key):
		s = -1
		e = -1
		for i in range(N):
			if a[i] == key:
				s = i
			if a[N - i - 1] == key:
				e = N - i - 1
		return (e, s)


class Solution:

	def findIndex(self, a, n, key):
		start = -1
		for i in range(n):
			if a[i] == key:
				start = i
				break
		end = start
		for i in range(n - 1, -1, -1):
			if a[i] == key:
				end = i
				break
		a = []
		a.append(start)
		a.append(end)
		return a


class Solution:

	def findIndex(self, arr, N, key):
		left = 0
		right = N - 1
		while left < len(arr):
			if arr[left] != key:
				left += 1
			else:
				break
		while right >= 0:
			if arr[right] != key:
				right -= 1
			else:
				break
		if left == len(arr) and right == -1:
			return [-1, -1]
		else:
			return [left, right]


class Solution:

	def findIndex(self, a, N, key):
		c = a.count(key)
		l = []
		k = key in a
		if k == False:
			l.append(-1)
			l.append(-1)
		for i in range(1, c + 1):
			d = a.index(key)
			l.append(d)
			a[d] = 0
		return (l[0], l[-1])


class Solution:

	def findIndex(self, a, N, key):
		ans = []
		n = len(a)
		if key in a:
			ans.append(a.index(key))
			a.reverse()
			ans.append(n - 1 - a.index(key))
			return ans
		else:
			return [-1, -1]


class Solution:

	def findIndex(self, a, N, key):
		if key not in a:
			s = -1
			t = -1
		else:
			for i in range(0, n):
				if a[i] == key:
					s = i
					break
			for i in range(1, n + 1):
				if a[-i] == key:
					t = n - i
					break
		l = [s, t]
		return l


class Solution:

	def findIndex(self, a, N, key):
		if key not in a:
			return [-1, -1]
		res = []
		for i in range(N):
			if a[i] == key:
				res.append(i)
		return [res[0], res[-1]]


class Solution:

	def findIndex(self, a, N, key):
		start_index = -1
		end_index = -1
		index_list = []
		key_count = a.count(key)
		if key_count == 0:
			pass
		elif key_count == 1:
			start_index = end_index = a.index(key)
		else:
			start_index = a.index(key)
			for i in range(key_count - 1):
				a.remove(key)
				a.insert(0, 0)
			end_index = a.index(key)
		index_list.append(start_index)
		index_list.append(end_index)
		return index_list


class Solution:

	def findIndex(self, a, N, key):
		b = []
		if key not in a:
			return (-1, -1)
		for i in range(n):
			if a[i] == key:
				b.append(i)
			else:
				continue
		return (min(b), max(b))


class Solution:

	def findIndex(self, a, N, key):
		result = []
		min_index = N - 1
		max_index = 0
		element_not_found = True
		for i in range(N):
			if a[i] == key:
				if i < min_index:
					min_index = i
					element_not_found = False
				if i > max_index:
					max_index = i
					element_not_found = False
		if element_not_found:
			result.append(-1)
			result.append(-1)
		else:
			result.append(min_index)
			result.append(max_index)
		return result


class Solution:

	def findIndex(self, a, N, key):
		i = 0
		j = N - 1
		ans = [-1, -1]
		left = True
		right = True
		for i in range(N):
			if a[i] == key and left:
				left = False
				ans[0] = i
				break
		for i in range(N - 1, -1, -1):
			if a[i] == key and right:
				right = False
				ans[1] = i
				break
		return ans


class Solution:

	def findIndex(self, a, N, key):
		firstFoundIdx = -1
		lastFoundIdx = -1
		i = 0
		isFinish = False
		while not isFinish:
			if firstFoundIdx == -1:
				if a[i] == key:
					firstFoundIdx = i
					i = N - 1
				elif i == N - 1:
					isFinish = True
				else:
					i += 1
			elif a[i] == key:
				lastFoundIdx = i
				break
			elif i == 0:
				isFinish = True
			else:
				i -= 1
		return [firstFoundIdx, lastFoundIdx]


class Solution:

	def findIndex(self, a, N, key):
		(found_l, found_r) = (-1, -1)
		for i in range(N):
			if a[i] == key:
				found_l = i
				break
		for j in range(N - 1, -1, -1):
			if a[j] == key:
				found_r = j
				break
		return (found_l, found_r)


class Solution:

	def findIndex(self, a, N, key):
		key_present = []
		if key not in a:
			return (-1, -1)
		else:
			for i in range(len(a)):
				if key == a[i]:
					key_present.append(i)
			return (key_present[0], key_present[-1])


class Solution:

	def findIndex(self, a, N, key):
		lst = []
		for i in a:
			if i == key:
				lst.append(a.index(i))
				break
		if len(lst) == 0:
			lst.append(-1)
			lst.append(-1)
		else:
			for i in range(N - 1, -1, -1):
				if a[i] == key:
					lst.append(i)
					break
		return lst


class Solution:

	def findIndex(self, a, N, key):
		ar = []
		if key in a:
			for i in range(N):
				if a[i] == key:
					ar.append(i)
		else:
			ar.append(-1)
		(first, last) = (ar[0], ar[-1])
		return (first, last)


class Solution:

	def findIndex(self, a, N, key):
		y = []
		aa = a[::-1]
		for i in range(len(a)):
			if a[i] == key:
				y.append(i)
				break
		for i in range(len(aa)):
			if aa[i] == key:
				y.append(N - i - 1)
				break
		if len(y) == 0:
			return ['-1', '-1']
		else:
			return y


class Solution:

	def findIndex(self, a, N, key):
		b = []
		if key not in a:
			b.append(-1)
			b.append(-1)
			return b
		p = a.index(key)
		a.reverse()
		q = a.index(key)
		b.append(p)
		b.append(len(a) - q - 1)
		return b


class Solution:

	def findIndex(self, a, N, key):
		p = []
		for i in range(N):
			if key == a[i]:
				p.append(i)
		if len(p) == 0:
			return [-1, -1]
		q = [p[0], p[-1]]
		return q


class Solution:

	def findIndex(self, a, N, key):
		i = 0
		j = N - 1
		ans = [-1, -1]
		left = True
		right = True
		while i < N or j >= 0:
			if a[i] == key and left:
				left = False
				ans[0] = i
			if a[j] == key and right:
				right = False
				ans[1] = j
			i += 1
			j -= 1
			if not left and (not right):
				return ans
		return ans


class Solution:

	def findIndex(self, a, N, key):
		start = -1
		end = -1
		i = 0
		isFinish = False
		while not isFinish:
			if start == -1:
				if a[i] == key:
					start = i
					i = N - 1
				elif i == N - 1:
					isFinish = True
				else:
					i += 1
			elif a[i] == key:
				end = i
				break
			elif i == 0:
				isFinish = True
			else:
				i -= 1
		return [start, end]


class Solution:

	def findIndex(self, arr, N, key):
		(start, end) = (-1, -1)
		for i in range(N):
			if arr[i] == key:
				if start == -1:
					start = i
				if end < i:
					end = i
		if start == -1:
			return [-1, -1]
		else:
			return [start, end]


class Solution:

	def findIndex(self, a, N, key):
		t = []
		for i in range(N):
			if key == a[i]:
				t.append(i)
		l = len(t)
		d = []
		if l == 0:
			d.append(-1)
			d.append(-1)
			return d
		d.append(t[0])
		d.append(t[l - 1])
		return d


class Solution:

	def findIndex(self, a, N, key):
		flag = 0
		firstIndex = -99
		secondIndex = -99
		for i in range(N):
			if a[i] == key:
				firstIndex = i
				flag = 1
				break
		for i in range(N - 1, -1, -1):
			if a[i] == key:
				secondIndex = i
				break
		if flag == 0:
			return (-1, -1)
		return (firstIndex, secondIndex)


class Solution:

	def findIndex(self, a, N, key):
		startIndex = 0
		endIndex = 0
		for i in range(N):
			if a[i] == key:
				startIndex = i
				break
			else:
				startIndex = -1
		for i in range(N - 1, 0, -1):
			if a[i] == key:
				endIndex = i
				break
			else:
				endIndex = startIndex
		return (startIndex, endIndex)


class Solution:

	def findIndex(self, a, N, key):
		low = -1
		up = -1
		for i in range(N):
			if a[i] == key:
				if low == -1:
					low = i
					up = i
				else:
					up = i
		return [low, up]


class Solution:

	def findIndex(self, arr, n, key):
		ans = []
		for i in range(0, n):
			if arr[i] == key:
				ans.append(i)
		if len(ans) >= 1:
			return [ans[0], ans[-1]]
		else:
			return [-1, -1]


class Solution:

	def findIndex(self, a, N, key):
		if key not in a:
			return (-1, -1)
		else:
			pos1 = a.index(key)
			pos2 = 0
			for i in range(len(a) - 1, pos1 - 1, -1):
				if a[i] == key:
					pos2 = i
					break
			return (pos1, pos2)


class Solution:

	def findIndex(self, a, N, key):
		b = [0] * len(a)
		for i in range(len(a)):
			if a[i] == key:
				b[i] += 1
		c = ''.join([str(i) for i in b])
		return (c.find('1'), c.rfind('1'))


class Solution:

	def findIndex(self, a, N, key):
		start_index = -1
		end_index = -1
		for i in range(N):
			if a[i] == key:
				start_index = i
				break
		for i in range(N - 1, -1, -1):
			if a[i] == key:
				end_index = i
				break
		return [start_index, end_index]


class Solution:

	def findIndex(self, a, N, key):
		start_in = -1
		end_in = -1
		for i in range(N):
			if a[i] == key:
				start_in = i
				break
		for i in range(N - 1, -1, -1):
			if a[i] == key:
				end_in = i
				break
		return [end_in, start_in][::-1]


class Solution:

	def findIndex(self, a, N, key):
		s_ind = -1
		e_ind = -1
		for i in range(N):
			if a[i] == key:
				e_ind = i
				if s_ind == -1:
					s_ind = i
		return [s_ind, e_ind]


class Solution:

	def findIndex(self, a, N, key):
		ans = []
		for x in range(0, len(a)):
			if a[x] == key:
				ans.append(x)
				break
		if len(ans) == 0:
			ans.append(-1)
		for y in range(0, len(a)):
			if a[len(a) - 1 - y] == key:
				ans.append(len(a) - 1 - y)
				break
		if len(ans) == 1:
			ans.append(-1)
		return ans


class Solution:

	def findIndex(self, a, N, key):
		result = [-1, -1]
		i = 0
		encontrado_izda = False
		encontrado_drcha = False
		while i < N:
			if a[i] == key and (not encontrado_izda):
				result[0] = i
				encontrado_izda = True
			if a[N - 1 - i] == key and (not encontrado_drcha):
				result[1] = N - 1 - i
				encontrado_drcha = True
			i += 1
		return result


class Solution:

	def findIndex(self, a, N, key):
		if a.count(key) == 0:
			return (-1, -1)
		ans = []
		for i in range(n):
			if a[i] == key:
				ans.append(i)
		(first, last) = (ans[0], ans[-1])
		return (first, last)


class Solution:

	def findIndex(self, a, N, key):
		if key not in a:
			return (-1, -1)
		n = len(a)
		l = []
		for i in range(n):
			if a[i] == key:
				l.append(i)
		return (min(l), max(l))


class Solution:

	def findIndex(self, a, N, key):
		flag = False
		l1 = []
		for i in range(N):
			if a[i] == key:
				l1.append(i)
				flag = True
		if flag == False:
			l1.append(-1)
		return [l1[0], l1[-1]]


class Solution:

	def findIndex(self, a, N, key):
		ans = []
		first = -1
		for i in range(N):
			if a[i] == key:
				first = i
				break
		last = -1
		for i in range(N - 1, -1, -1):
			if a[i] == key:
				last = i
				break
		ans.append(first)
		ans.append(last)
		return ans


class Solution:

	def findIndex(self, a, N, key):
		ls = [-1, -1]
		for i in range(len(a)):
			if ls[0] == -1:
				if a[i] == key:
					ls[0] = i
					ls[1] = i
			elif a[i] == key:
				ls[1] = i
		return ls


class Solution:

	def findIndex(self, a, N, key):
		ar = []
		for i in range(N):
			if a[i] == key:
				ar.append(i)
				break
		for j in range(N - 1, -1, -1):
			if a[j] == key:
				ar.append(j)
				return ar
		return (-1, -1)