Value equal to index value Geeks For Geeks
TIME O(N) [GFG TIME : 2.6/4.4]
SPACE O(N) [Due To Use Of ArrayList ]
class Solution {
ArrayList<Integer> valueEqualToIndex(int arr[], int n) {
ArrayList<Integer> aa=new ArrayList<Integer>();
if(n==1){
if(arr[0]==1){
aa.add(1);
return aa;
}
else{
aa.add(0);
return aa;
}
}
else {
for(int i=0;i<n;i++){
if(i==arr[i]-1){
aa.add(i+1);
}
}
}
return aa;
}
}
Thanks for reading.
Comments
Post a Comment