Skip to content

Instantly share code, notes, and snippets.

@praneethreddymallupally
Created June 22, 2020 07:09
Show Gist options
  • Save praneethreddymallupally/3997ec9e8247c41db99b5c9bc4c73401 to your computer and use it in GitHub Desktop.
Save praneethreddymallupally/3997ec9e8247c41db99b5c9bc4c73401 to your computer and use it in GitHub Desktop.
Odd-even Subsequences
#include < bits/stdc++.h >
using namespace std;
const int N = 2e5 + 1;
bool check(int x, int cur)
{
int ans = 0;
for(int i = 1; i <= n; i++)
{
if(!cur)
{
ans++;
cur ^= 1;
}
else
{
if(a[i] <= x)
{
ans++;
cur ^= 1;
}
}
}
return ans >= k;
}
int binsearch(int lo, int hi)
{
while(lo < hi)
{
int mid = (lo + hi) / 2;
if(check(mid, 0) || check(mid, 1))
hi = mid;
else
lo = mid + 1;
}
return lo;
}
int main()
{
IOS;
int n, k;
int a[N];
cin >> n >> k;
for(int i = 1; i <= n; i++)
cin >> a[i];
int ans = binsearch(1, 1e9);
cout << ans;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment