Second largest element from an array

PROBLEM :

Given an array, return second  largest element from an array.

Input:

The first line of input contains an integer T denoting the number of test cases.
The first line of each test case is N,N is the size of array.
The second line of each test case contains N input C[i].

Output:

Print the second largest element.

Constraints:

1 = T = 50
1 = N = 500
1 = C[i] = 1200

Example:

Input
2
5
89 24 75 11 23
6
56 42 21 23 65 20

Output
75
56

--------------------------------------------------------------------------------
SIMPLE c++ IMPLEMENTATION :
--------------------------------------------------------------------------------

#include<iostream>
using namespace std;
int main()
 {
int a[100],no,ele,j,max,smax ;
cin>>no ;

while(no--)
{
   cin>>ele ;
   for(j=0;j<ele;j++)
       cin>>a[j] ;
     
   max=a[0] ;
   for(j=0;j<ele;j++)
       {
           if(a[j]>max)
           max=a[j] ;
       }
     
   smax=0 ;
   for(j=0;j<ele;j++)
      {
          if(a[j]>smax)
           if(a[j]<max)
               smax=a[j] ;
             
      }
     
      cout<<smax<<endl ;
}
return 0;
}

---------------------------------------------------------------------------------

Comments

Popular posts from this blog

Count ways to N'th Stair(Order does not matter)

Replace all ‘0’ with ‘5’ in an input Integer

Chocolate Distribution Problem

Remove characters from the first string which are present in the second string

Primality Test ( CodeChef Problem code: PRB01 )