Value equal to index value | Practice | GeeksforGeeks

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 ).
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 satified. When there is not such element exists then return an empty array of length 0.

Expected Time Complexity: O(N)
Expected Auxiliary Space: O(1)
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
 
0
harshscode6 days ago
 vector<int> v;       for(int i=0;i<n;i++)    {        if(arr[i]==i+1)        {        v.push_back(arr[i]);               }    }          return v;   
0
bhushanaditya711 week ago
// { Driver Code Starts
#include<bits/stdc++.h>

using namespace std;


 // } Driver Code Ends
//User function template for C++
class Solution{
public:

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

// { Driver Code Starts.

int main() {
    int t;
    cin >> t;
    while (t--) {
        int n, i;
        cin >> n;
        int arr[n];
        for (i = 0; i < n; i++) {
            cin >> arr[i];
        }
        Solution ob;
        auto ans = ob.valueEqualToIndex(arr, n);
        if (ans.empty()) {
            cout << "Not Found";
        } else {
            for (int x: ans) {
                cout << x << " ";
            }
        }
        cout << "\n";
    }
    return 0;
}
  // } Driver Code Ends
0
tanmeetsinghreel1 week ago
//Python3
def valueEqualToIndex(self,arr, n): l=[] for i in range(n):     if arr[i] == i+1:         l.append(arr[i]) return(l)
0
akashmarkad22102 weeks ago
JAVA Solution
 
ArrayList<Integer> valueEqualToIndex(int arr[], int n) {       // code here       ArrayList<Integer> ans = new ArrayList<>();       int idx = 1;       for(int i = 0 ; i < n ; i++)       {           if(idx == arr[i])           {               ans.add(arr[i]);           }                      idx++;       }              return ans;   } 
0
rsbly7300952 weeks ago
class Solution{   valueEqualToIndex(arr,n){       let arr1 = []       for(let i = 0 ; i< n ; i++) {           if (arr[i] == (i + 1)){               arr1.push(arr[i]);           }       }       if(arr1.length !=0)       console.log(arr1.join(' '));       else       console.log("Not Found");   }}
0
mayank180919992 weeks ago
	vector<int> valueEqualToIndex(int arr[], int n) {
	    // code here
	    vector<int>ans;
	    for(int i=0;i<n;i++){
	        if(arr[i]==i+1){
	            ans.push_back(arr[i]);
	        }
	    }
	  return ans;
	}
	
0
antaryamisingh2523 weeks ago
 vector<int> valueEqualToIndex(int arr[], int n) {    vector<int> ans;    int idx =1;    for(int i=0; i<n; i++){        if(arr[i]==idx){            ans.push_back(idx);       }        idx++;    }    return ans;}
0
knhashasPremium3 weeks ago
//javaclass Solution {   ArrayList<Integer> valueEqualToIndex(int arr[], int n) {        ArrayList<Integer> list = new ArrayList<>();       for (int i = 0; i < n; i++) {
             if(i+1 == arr[i]){             list.add(i+1);          }      }      return list;  }   }
0
muskanjain5461 month ago
//java
 
 ArrayList<Integer> valueEqualToIndex(int arr[], int n) {       ArrayList<Integer> array = new ArrayList<Integer>();                for (int i = 0; i < n; i++) {
  if (arr[i] == i+1) {  array.add(i+1);  } } return array;   }
0
forestwise1 month ago
//javascript
 
The tests for inputs that result in an empty array are giving the error:
 
Your Output:
 
Expected Output:
Not Found
 
Is there an issue with the testing? (code below)
 
Thank you,
 
fw
 
class Solution{ 
    valueEqualToIndex(arr,n) {
        let indices = []; //define an empty array
        for(let i=1;i<=n;i++) { 
            if(arr[i-1]==i)
                indices.push(i);
        }
         
        return indices; //this returns an empty array if nothing found
    }
}
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.