Skip to content

Instantly share code, notes, and snippets.

@zsrinivas
Last active August 29, 2015 14:01
Show Gist options
  • Save zsrinivas/b52d958d44d916a5f483 to your computer and use it in GitHub Desktop.
Save zsrinivas/b52d958d44d916a5f483 to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
#include <strings.h>
#define MAXnMAX 10000
int main(int argc, char const *argv[])
{
int maxlosers[MAXnMAX],pint,temp,max,nextmax;
int n,k=1,i;
scanf("%d", &n);
scanf("%d%d", &max,&nextmax);
if (max<nextmax)/*one comparision*/
{
temp=max;
max=nextmax;
nextmax=temp;
}
maxlosers[0]=nextmax;
for (i = 2; i < n; ++i)
{
scanf("%d", &pint);
if (max<pint)/*n-3 comparisions*/
{
k=0;
maxlosers[k++]=max;
max=pint;
}
else
maxlosers[k++]=pint;
}
nextmax=maxlosers[0];
/*
check for k, this will always give an output such that k<=log2(n)
printf("%d %d", k,(int)log2(n));
*/
for (i = 1; i < k; ++i)/*log2(n) comparisons*/
if (maxlosers[i]>nextmax)
nextmax=maxlosers[i];
printf("%d\n%d\n", max,nextmax);/*Total comparisions are n+log(n)-2*/
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment