Skip to content

Instantly share code, notes, and snippets.

@flyfire
Created October 5, 2013 15:35
Show Gist options
  • Select an option

  • Save flyfire/6842371 to your computer and use it in GitHub Desktop.

Select an option

Save flyfire/6842371 to your computer and use it in GitHub Desktop.
#include <iostream>
using namespace std;
void min_max(int a[], int size, int &min, int &max) {
int start;
if (size % 2) {
min = max = a[0];
start = 1;
}
else {
if (a[0] > a[1]) {
min = a[0];
max = a[1];
}
else {
min = a[1];
max = a[0];
}
start = 2;
}
for (int i = start; i < size; i += 2) {
int tmin, tmax;
if (a[i] > a[i + 1]) {
tmin = a[i + 1];
tmax = a[i];
}
else {
tmin = a[i];
tmax = a[i + 1];
}
if (tmin < min) {
min = tmin;
}
if (tmax > max) {
max = tmax;
}
}
}
int main() {
int a[] = {2, 3, 0, 34, 9, 1, -12, 84, 5, 8, 1, 7};
int size = sizeof(a) / sizeof(*a);
int min, max;
min_max(a, size, min, max);
cout << min << " " << max << endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment