You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: dsa-solutions/gfg-solutions/0001-value-equal-to-index-value.md
+13-13Lines changed: 13 additions & 13 deletions
Original file line number
Diff line number
Diff line change
@@ -15,7 +15,7 @@ description: "This is a solution to the Value Equal to Index Value problem on Ge
15
15
16
16
## Problem Description
17
17
18
-
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 ).<br />
18
+
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).
19
19
20
20
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.
21
21
@@ -52,16 +52,16 @@ You don't need to read input or print anything. Your task is to complete the fun
52
52
53
53
## Problem Explanation
54
54
55
-
The problem involves finding elements in an array whose values match their index positions, considering 1-based indexing. This means if an element's value matches its position in the array when counting from 1 (not 0), it should be included in the result.<br />
55
+
The problem involves finding elements in an array whose values match their index positions, considering 1-based indexing. This means if an element's value matches its position in the array when counting from 1 (not 0), it should be included in the result.
56
56
57
57
Example:
58
58
59
59
* Given an array [15, 2, 45, 12, 7]:
60
-
- At 0-based index 0, the value is 15 (not equal to 1)
61
-
- At 0-based index 1, the value is 2 (equal to 2)
62
-
- At 0-based index 2, the value is 45 (not equal to 3)
63
-
- At 0-based index 3, the value is 12 (not equal to 4)
64
-
- At 0-based index 4, the value is 7 (not equal to 5)
60
+
- At 1-based index 1, the value is 15 (not equal to 1)
61
+
- At 1-based index 2, the value is 2 (equal to 2)
62
+
- At 1-based index 3, the value is 45 (not equal to 3)
63
+
- At 1-based index 4, the value is 12 (not equal to 4)
64
+
- At 1-based index 5, the value is 7 (not equal to 5)
65
65
66
66
The only element that matches its index is 2 at index 2.
67
67
@@ -108,17 +108,17 @@ For the array `[15, 2, 45, 12, 7]` with length `n = 5`:
108
108
* Initialize an empty list: `result = []`
109
109
* Loop through the array with indices from 0 to 4:
110
110
- Check element at index 0 (1-based index 1): 15 ≠ 1
111
-
Check element at index 1 (1-based index 2): 2 = 2 (add to result)
112
-
Check element at index 2 (1-based index 3): 45 ≠ 3
113
-
Check element at index 3 (1-based index 4): 12 ≠ 4
114
-
Check element at index 4 (1-based index 5): 7 ≠ 5
111
+
- Check element at index 1 (1-based index 2): 2 = 2 (add to result)
112
+
- Check element at index 2 (1-based index 3): 45 ≠ 3
113
+
- Check element at index 3 (1-based index 4): 12 ≠ 4
114
+
- Check element at index 4 (1-based index 5): 7 ≠ 5
115
115
116
116
Final `result` list: `[2]`
117
117
118
118
## Time Complexity
119
119
120
-
The time complexity of this solution is O(N) where N is the number of elements in the array. This is because the solution involves a single pass through the array to check each element against its 1-based index.
120
+
The time complexity of this solution is $O(N)$ where N is the number of elements in the array. This is because the solution involves a single pass through the array to check each element against its 1-based index.
121
121
122
122
## Space Complexity
123
123
124
-
The space complexity is O(1) for the input space, and O(K) for the output space, where K is the number of elements matching the condition. In the worst case, if all elements match, the space used by the result list will be O(N).
124
+
The space complexity is $O(1)$ for the input space, and $O(K)$ for the output space, where K is the number of elements matching the condition. In the worst case, if all elements match, the space used by the result list will be $O(N)$.
0 commit comments