-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDay47.java
More file actions
35 lines (27 loc) · 971 Bytes
/
Copy pathDay47.java
File metadata and controls
35 lines (27 loc) · 971 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
/*Value equal to index value
Given an array arr. 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.
Examples:
Input: arr[] = [15, 2, 45, 4 , 7]
Output: [2, 4]
Explanation: Here, arr[2] = 2 exists here and arr[4] = 4 exists here.
Input: arr[] = [1]
Output: [1]
Explanation: Here arr[1] = 1 exists.
*/
/* import java.util.*;
class Solution {
public List<Integer> valueEqualToIndex(List<Integer> nums) {
List<Integer> result = new ArrayList<>();
for (int i = 0; i < nums.size(); i++) {
if (nums.get(i) == i + 1) {
result.add(i + 1); // 1-based index
}
}
return result;
}
}
*/
/* ⏱ Complexity
Time: O(n)
Space: O(1) (excluding output list)*/