Saturday, September 23, 2017

Second Largest Value among N integers

The program must accept N integers and print the second largest value among the N integers.

Input Format:
The first line denotes the value of N. Next N lines will contain the N integer values.

Output Format:
The first line contains the second largest integer.

Boundary Conditions: 2 <= N <= 100 The value of the integers will be from -999999 to 999999.

Example Input/Output:
Input:
3
100
2200
345
Output:
345

Solution:
#include <iostream>
using namespace std;
int main()
{
 int a,n,f,s;
 cin>>n;
 for(int i=0;i<n;i++)
 {
     cin>>a;
     i==0?f=a:f<a?s=f,f=a:a<f&&a>s?s=a:s=s;
 }
 cout<<s;
}
Share: