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.