-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathGFG_SearchInsertPosInSortedArray.cpp
55 lines (48 loc) · 1.09 KB
/
GFG_SearchInsertPosInSortedArray.cpp
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
/*
https://practice.geeksforgeeks.org/problems/search-insert-position-of-k-in-a-sorted-array/1/#
Search insert position of K in a sorted array
*/
// { Driver Code Starts
#include <bits/stdc++.h>
using namespace std;
// } Driver Code Ends
//User function Template for C++
class Solution{
public:
int searchInsertK(vector<int>Arr, int N, int k)
{
int low=0;
int high=N-1;
if(k<Arr[low]) return low;
if(k>Arr[high]) return N;
while(low<=high){
int mid = low + (high-low)/2;
if(Arr[mid]==k)
return mid;
else if(k<Arr[mid])
high=mid-1;
else
low=mid+1;
}
return low;
}
};
// { Driver Code Starts.
int main()
{
int t;
cin>>t;
while(t--)
{
int N;
cin >> N;
vector<int>Arr(N);
for(int i=0;i<N;i++)
cin>>Arr[i];
int k;
cin>>k;
Solution obj;
cout<<obj.searchInsertK(Arr, N, k)<<endl;
}
return 0;
} // } Driver Code Ends