Find Second Largest Element in an array in O(n)

#include <stdio.h>
#include <limits.h>                                             //includes INT_MIN

int main() 
    int n,max1=INT_MIN,max2=INT_MIN;      //initialize max1 and max2 to lowest interger value
    int a[n];

    for(int i=0;i<n;i++)
        if(a[i]>max1)                                                //if the entered number is greater than max1, set                                                                                                                                        max1 as max2 and entered number as max1
            max2 = max1;
            max1 = a[i];
        else if(max2<a[i] && a[i]<max1)                //if entered number is greater than max2 and lesser                                                                                                                                   than max1, set entered number as max2
            max2 = a[i];

    return 0;


Popular posts from this blog

C program to print the following Pattern

c++ program to print string along the diagonals of the matrix.

Kollywood Game