Find a Fixed Point (Value equal to index) in a given array.

 

Given an array Arr of N positive integers. Your task is to find the element whose value is equal to that of its index value.


Output:  returns an array of indices where the given conditions are satisfied. When there is not such element exists then return an empty array of length 0.

Note: There can be more than one element in the array which have same value as their index. You need to include every such element's index. Follows 1-based indexing of the array.

Constraints:
1 ≤ N ≤ 105
1 ≤ Arr[i] ≤ 106


Solution Hunt:

// Here we are giving the function only.

            vector<int> valueEqualToIndex(int arr[], int n) 
{      
       vector<int> k;
       for(int i=0;i<n;i++)
       {
           if(arr[i]==i+1)
           {
               
               k.push_back(arr[i]);
               
           }
           
        }
       
       return k;
       
}

            







Post a Comment

0 Comments