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 ).
Question
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).
Solution
Sure, here is a step-by-step solution in Python:
- Initialize an empty list to store the elements whose value is equal to their index value.
result = []
- Iterate over the array using a for loop. The enumerate function can be used to get both the index and value of each element. Remember to add 1 to the index during the comparison because the problem uses 1-based indexing.
for i, num in enumerate(Arr, start=1):
if i == num:
result.append(num)
- Return the result list.
return result
Here is the complete Python function:
def find_elements(Arr):
result = []
for i, num in enumerate(Arr, start=1):
if i == num:
result.append(num)
return result
You can call this function with your array as the argument to find the elements whose value is equal to their index value.
Similar Questions
Given a one dimensional array arr, what is the correct way of getting the number of elements in arr is
If you declare an array int arr[10];, what is the range of valid indexes for accessing elements of the array?0 to 101 to 100 to 91 to 9
Given an integer array and another integer element. The task is to find if the given element is present in array or not.
How do you find the index of the first occurrence of an element in an array?Optionsfind(element)index(element)locate(element)search(element)
What is the array index type? What is the lowest index? What is the representationof the third element in an array named a
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.