Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // https://leetcode.com/discuss/interview-question/1527195/Google-Interview-Questions
- #include <iostream>
- #include <bits/stdc++.h>
- typedef long long ll;
- using namespace std;
- int main()
- {
- int n; cin>>n;
- vector<int>v(n);
- for(int i=0;i<n;i++) cin>>v[i];
- int k; cin>>k;
- int l=0, r=n-1;
- int cnt = n-k+1, p = 0, ans = -1;
- while(l<=r){
- if(abs(v[r]) > abs(v[l])){
- ans=v[r];
- r--;
- }
- else{
- ans=v[l];
- l++;
- }
- p++;
- if(p==cnt) break;
- }
- cout<<ans;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement