Given an array Arr of N positive integers. Your task is to find the elements whose value is equal to that of its index value ( Consider 1-based indexing ).
Note: There can be more than one element in the array which have the same value as its index. You need to include every such element's index. Follows 1-based indexing of the array. 
Example 1:
Input:
N = 5
Arr[] = {15, 2, 45, 12, 7}
Output: 2
Explanation: Only Arr[2] = 2 exists here.
Example 2:
Input: 
N = 1
Arr[] = {1}
Output: 1
Explanation: Here Arr[1] = 1 exists.
Your Task:  
You don't need to read input or print anything. Your task is to complete the function valueEqualToIndex() which takes the array of integers arr[] and n as parameters and returns an array of indices where the given conditions are satisfied. When there is no such element exists then return an empty array of length 0.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(1)
 
Constraints:
1 ≤ N ≤ 10^{5}
1 ≤ Arr[i] ≤ 10^{6}

Source: geeksforgeeks | Difficulty: EASY
Tags: Data structures

Complete search

Starter code: #User function Template for python3

class Solution:



	def valueEqualToIndex(self,arr, n):

		# code here

Solutions:
class Solution:

	def valueEqualToIndex(self, arr, n):
		final = []
		for (i, ele) in enumerate(arr):
			if ele == i + 1:
				final.append(ele)
		return final


class Solution:

	def valueEqualToIndex(self, arr, n):
		ans = []
		for i in range(n):
			if arr[i] == i + 1:
				ans.append(arr[i])
		return ans


class Solution:

	def valueEqualToIndex(self, arr, n):
		li = []
		for i in range(0, n):
			if arr[i] == i + 1:
				li.append(arr[i])
		if len(li) == 0:
			return []
		else:
			return li


class Solution:

	def valueEqualToIndex(self, arr, n):
		mylist = []
		i = 1
		for a in arr:
			if i == a:
				mylist.append(a)
			i = i + 1
		return mylist


class Solution:

	def valueEqualToIndex(self, arr, n):
		val = []
		for i in range(0, n):
			if arr[i] == i + 1:
				val.append(arr[i])
		return val


class Solution:

	def valueEqualToIndex(self, arr, n):
		indarr = []
		count = 0
		for i in arr:
			count += 1
			if i == count:
				indarr.append(count)
		return indarr


class Solution:

	def valueEqualToIndex(self, arr, n):
		val = []
		for (i, n) in enumerate(arr, start=1):
			if i == n:
				val.append(n)
		return val


class Solution:

	def valueEqualToIndex(self, arr, n):
		y = []
		z = []
		for i in range(n):
			if arr[i] == i + 1:
				y.append(arr[i])
		return y


class Solution:

	def valueEqualToIndex(self, arr, n):
		result = []
		for i in range(len(arr)):
			if arr[i] == i + 1:
				result.append(arr[i])
		return result


class Solution:

	def valueEqualToIndex(self, arr, n):
		output = []
		for i in range(0, n):
			if arr[i] == i + 1:
				output.append(arr[i])
		if len(output) > 0:
			return output
		else:
			return ['Not Found']


class Solution:

	def valueEqualToIndex(self, arr, n):
		sol = []
		i = 0
		while i < n:
			if i + 1 == arr[i]:
				sol.append(i + 1)
			i += 1
		return sol


class Solution:

	def valueEqualToIndex(self, arr, n):
		output = []
		for index in range(n):
			if arr[index] == index + 1:
				output.append(arr[index])
		return output


class Solution:

	def valueEqualToIndex(self, arr, n):
		ar2 = []
		c = 1
		for i in arr:
			if i == c:
				ar2.append(i)
			c += 1
		return ar2


class Solution:

	def valueEqualToIndex(self, arr, n):
		lis = list()
		for i in range(0, n):
			if arr[i] == i + 1:
				lis.append(arr[i])
		return lis


class Solution:

	def valueEqualToIndex(self, arr, n):
		r = []
		v = sorted(arr)
		n = len(arr)
		for i in range(1, len(arr) + 1):
			if i == arr[i - 1]:
				r.append(i)
		return r


class Solution:

	def valueEqualToIndex(self, arr, n):
		flag = 0
		w = 0
		a = []
		b = []
		for i in range(len(arr)):
			if arr[i] == i + 1:
				w = arr[i]
				flag += 1
				a.append(arr[i])
		if flag > 0:
			return a
		else:
			return b


class Solution:

	def valueEqualToIndex(self, arr, n):
		res = []
		for (i, e) in enumerate(arr):
			if i + 1 == e:
				res.append(e)
		return res


class Solution:

	def valueEqualToIndex(self, arr, n):
		return [value for (index, value) in enumerate(arr) if value == index + 1]


class Solution:

	def valueEqualToIndex(self, arr, n):
		answer = []
		for (i, num) in enumerate(arr):
			if num == i + 1:
				answer.append(num)
		return answer


class Solution:

	def valueEqualToIndex(self, arr, n):
		newarr = []
		index = 0
		i = 0
		for index in range(n):
			if arr[index] == index + 1:
				newarr.append(index + 1)
				i = i + 1
		return newarr


class Solution:

	def valueEqualToIndex(self, arr, n):
		list = []
		temp = 1
		for i in arr:
			if i == temp:
				list.append(i)
			temp += 1
		return list


class Solution:

	def valueEqualToIndex(self, arr, n):
		ans = []
		for (idx, num) in enumerate(arr):
			if idx + 1 == num:
				ans.append(num)
		return ans


class Solution:

	def valueEqualToIndex(self, arr, n):
		if n == 0:
			return -1
		if n == 1:
			if n[0] == 1:
				return 1
			else:
				return -1
		if n > 1:
			indexes = []
			for (ind, value) in enumerate(arr):
				i = ind + 1
				if i == value:
					indexes.append(i)
			return indexes


class Solution:

	def valueEqualToIndex(self, arr, n):
		arr = list(arr)
		output = []
		index = 1
		while index <= n:
			if index == arr[index - 1]:
				output.append(index)
			index += 1
		return output


class Solution:

	def valueEqualToIndex(self, arr, n):
		arr1 = []
		for i in range(len(arr)):
			if arr[i] == i + 1:
				arr1.append(arr[i])
			else:
				continue
		return arr1