top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

Find three largest numbers in array using C and share its complexity?

+1 vote
352 views
Find three largest numbers in array using C and share its complexity?
posted Nov 8, 2017 by anonymous

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

1 Answer

0 votes

include <stdio.h>

int main() {
int arry[] = { 30, 10, 40, 24, 55 };
int third = 0, second = 0, first=0;
int i = 0,x =0;
while (i < 5)
{
x = arry[i];
if ( x > third)
{
first = second;
second = third;
third = x;
}
else if ( x > second)
{
first = second;
second = x;
}
else if ( x > first)
{
first = x;
}
i++;
}
printf ("First = %d, Second = %d, Third = %d", first,second, third);
return 0;
}

Time would be O(n)
and extra constant space O(1) to store extra elements.

answer Nov 10, 2017 by Ganesh
...