-
Notifications
You must be signed in to change notification settings - Fork 0
/
SearchInsertPosition_STL.cpp
60 lines (48 loc) · 1.17 KB
/
SearchInsertPosition_STL.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
56
57
58
59
60
#include<bits/stdc++.h>
using namespace std;
void finding(vector<int> &nums, int target){
int flag=0;
int vecsize=nums.size();
for(int i=0;i<nums.size();++i){
if(nums[i]==target){
cout<<i<<endl;
flag = 1;
cout<<flag;
}
}
//When the element is not found, the flag is 0
//checking the corner cases, i.e. Highest or lowest target
if(flag==0){
if(nums[0]>target){
cout<<0;
flag =1;
}
else if(nums[vecsize-1]<target){
cout<<vecsize;
flag=1;
}
}
//if the target is in middle somehwere
if(flag==0){
for(int i=0;i<nums.size();++i){
if(nums[i]<target&&nums[i+1]>target){
int pos = i+1;
cout<<pos;
}
}
}
}
int main(){
vector<int> nums;
int N;
cin>>N;//Size of the vector
//Input the values in the vector
for(int i=0;i<N;++i){
int x;
cin>>x;
nums.push_back(x);
}
int target;
cin>>target;
finding(nums,target);
}