Ask question, find answer on any topic in real time from people around the world. Have a question ? Ask now. Know an Answer share your Knowledge to world.PrepJunk is the online community that has Junk of answers!

Write program to find 2-nd largest element in an array with single loop.

0

Write program to find 2-nd largest element in an array with single loop.

asked Mar 29, 2012 in Technical Interiew by anonymous
    

1 Answer

0

Function

//program to find second largest element
            int secondMaxElement(int a[], int len) 
    {
                int max = INT_MIN, secondMax = INT_MIN, i;
                for(i = 0; i < len; i++) {
                    if(a[i] > max) {
                        secondMax = max;
                        max = a[i];
                    }
                    else if(a[i] > secondMax && a[i] < max)
                        secondMax = a[i];
                }
                return secondMax;
            }
answered Mar 29, 2012 by anonymous
edited Mar 29, 2012