Created
May 24, 2014 04:58
-
-
Save zsrinivas/851004619c8f362ecd9a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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 suck 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