Skip to content

Commit c6f389e

Browse files
committed
value equal to index value
1 parent c9eaad7 commit c6f389e

File tree

2 files changed

+132
-0
lines changed

2 files changed

+132
-0
lines changed
Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
---
2+
id: value-equal-to-index-value
3+
title: Value Equal to Index Value Problem (Geeks for Geeks)
4+
sidebar_label: 0001 - Value Equal to Index Value
5+
tags:
6+
- Beginner
7+
- Index Values
8+
- Array
9+
- Geeks for Geeks
10+
- CPP
11+
- Python
12+
- DSA
13+
description: "This is a solution to the Value Equal to Index Value problem on Geeks for Geeks."
14+
---
15+
16+
## Problem Description
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 />
19+
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+
22+
## Examples
23+
24+
Example 1:
25+
26+
```
27+
Input:
28+
N = 5
29+
Arr[] = {15, 2, 45, 12, 7}
30+
Output: 2
31+
Explanation: Only Arr[2] = 2 exists here.
32+
```
33+
34+
Example 2:
35+
36+
```
37+
Input:
38+
N = 1
39+
Arr[] = {1}
40+
Output: 1
41+
Explanation: Here Arr[1] = 1 exists.
42+
```
43+
44+
## Your Task
45+
46+
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 satisfied. When there is no such element exists then return an empty array of length 0. For C users, you need to modify the array(arr), in place such that the indices not in the final answer should be marked 0.
47+
48+
## Constraints
49+
50+
* 1 ≤ N ≤ 105
51+
* 1 ≤ `Arr[i]` ≤ 106
52+
53+
## Problem Explanation
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 />
56+
57+
Example:
58+
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)
65+
66+
The only element that matches its index is 2 at index 2.
67+
68+
## Python Solution Code
69+
70+
```py
71+
class Solution:
72+
73+
def valueEqualToIndex(self, arr, n):
74+
result = []
75+
for i in range(n):
76+
if (arr[i] == i + 1):
77+
result.append(arr[i]);
78+
return result
79+
```
80+
81+
## C++ Solution Code
82+
83+
```cpp
84+
class Solution {
85+
public:
86+
vector<int> valueEqualToIndex(int arr[], int n) {
87+
vector<int> result;
88+
for(int i = 0; i < n; i++) {
89+
if(arr[i] == i + 1)
90+
result.push_back(arr[i]);
91+
} return result;
92+
}
93+
};
94+
```
95+
96+
## Solution Logic
97+
98+
1. Initialize an empty result list: This will store the elements that satisfy the condition.
99+
2. Iterate through the array: Use a loop to go through each element in the array.
100+
3. Check the condition: For each element, check if the element's value matches its 1-based index. Since array indexing typically starts at 0 in most programming languages, you adjust by adding 1 to the index.
101+
4. Store matching elements: If an element's value matches its 1-based index, add it to the result list.
102+
5. Return the result list: After checking all elements, return the list of elements that met the condition.
103+
104+
## Example Walkthrough
105+
106+
For the array `[15, 2, 45, 12, 7]` with length `n = 5`:
107+
108+
* Initialize an empty list: `result = []`
109+
* Loop through the array with indices from 0 to 4:
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
115+
116+
Final `result` list: `[2]`
117+
118+
## Time Complexity
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.
121+
122+
## Space Complexity
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).
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"label": "LeetCode Solutions",
3+
"position": 4,
4+
"link": {
5+
"type": "generated-index",
6+
"description": "All Geeks for Geeks solutions in one place. Sorted by difficulty. Contains problem description, solution, and explanation."
7+
}
8+
}

0 commit comments

Comments
 (0)