Skip to content

Instantly share code, notes, and snippets.

@fredbr
Created March 12, 2018 21:36
Show Gist options
  • Save fredbr/0a5fc54d22874c5a3bda9cdd120400b7 to your computer and use it in GitHub Desktop.
Save fredbr/0a5fc54d22874c5a3bda9cdd120400b7 to your computer and use it in GitHub Desktop.
#include <bits/stdc++.h>
using namespace std;
const int maxn = 100010;
const int inf = 0x3f3f3f3f;
int v[maxn];
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
int n, k;
cin >> n >> k;
for (int i = 1; i <= n; i++) cin >> v[i];
int res;
if (k == 1) {
res = inf;
for (int i = 1; i <= n; i++) res = min(res, v[i]);
} else if (k == 2) {
res = max(v[1], v[n]);
} else {
res = -inf;
for (int i = 1; i <= n; i++) res = max(res, v[i]);
}
cout << res << "\n";
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment