Skip to content

Instantly share code, notes, and snippets.

@vinay13
Created February 16, 2014 00:06
Show Gist options
  • Save vinay13/9027213 to your computer and use it in GitHub Desktop.
Save vinay13/9027213 to your computer and use it in GitHub Desktop.
second largest no w/o sorting
//find the second best
#include<stdio.h>
int main()
{
int a[7]={1,8,5,7,10,11,2};
int max,sec_max;
if(a[1]<a[0])
{
max=a[0];
sec_max=a[1];
}
else
{
max=a[1];
sec_max=a[0];
}
for(int i=2;i<7;i++)
{
if(a[i]>max)
{
sec_max=max;
max=a[i];
}
if(a[i]<max && a[i]>sec_max)
{
sec_max=a[i];
}
}
printf("Second largest element is %d \n%d",sec_max,max);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment