We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
1 parent 676d737 commit 069c616Copy full SHA for 069c616
a.out
10.8 KB
value-equal-to-index-value.cpp
@@ -0,0 +1,30 @@
1
+// http://practice.geeksforgeeks.org/problems/value-equal-to-index-value/0
2
+#include <bits/stdc++.h>
3
+using namespace std;
4
+
5
+// 1 based indexing
6
+int search(int arr[], int n){
7
+ int i=1, j = n;
8
+ int mid;
9
+ while(i<j){
10
+ mid = (i+j)/2;
11
+ if(arr[mid] == mid) return mid;
12
+ if(arr[mid] < mid) i = mid+1;
13
+ else j = mid - 1;
14
+ }
15
+ return -1;
16
+}
17
+int main(){
18
+ int t,n;
19
+ cin>>t;
20
+ while(t--){
21
+ cin>>n;
22
+ int arr[100];
23
+ for(int i=1;i<=n;i++) cin >> arr[i];
24
+ sort(arr,arr+n+1);
25
+ int index = search(arr,n);
26
+ if(index == -1) cout << "Not Found\n";
27
+ else cout << index << endl;
28
29
+ return 0;
30
0 commit comments