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

For any given matrix find the path from the start to the end which gives the maximum sum. Traverse only right or down. Example: starting index is 15 (left top) and ending index is 10 (bottom right) 15 25 30 45 25 60 70 75 10 O/P:15->45->70->75->10 sum is 215

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