Breaking

Friday 22 September 2017

Second Largest Value among N integers

            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 1:
Input:
3
100
2200
345
Output:
345
Example Input/Output 2:
Input:
6
-23
-256
-87
-90
-11019
-2
Output:
-23
Code:
C language:

#include<stdio.h>
#include <stdlib.h>
int cmpfunc(const void *a,const void *b)
{
    return (*(int*)a-*(int*)b);
}
int main()
{
int n,i;
scanf("%d",&n);
int arr[n];
for(i=0;i<n;i++)
{
    scanf("%d",&arr[i]);
}
qsort(arr,n,sizeof(int),cmpfunc);
printf("%d",arr[n-2]);
}

C++:
#include <bits/stdc++.h>
using namespace std;
int main(int argc, char** argv)
{
int n,i;
cin>>n;
int arr[n];
    for(i=0;i<n;i++)
    {
        cin>>arr[i];
    }
    sort(arr,arr+n);
    cout<<arr[n-2];
}

Java:
import java.util.*;
public class Hello {

    public static void main(String[] args) {
                   Scanner sc=new Scanner(System.in);
           int n=sc.nextInt();
           int i;
           int[] arr=new int[n];
           for(i=0;i<n;i++)
              {
                  arr[i]=sc.nextInt();
              }
             Arrays.sort(arr);
           
             System.out.print(arr[n-2]);
          }
}

Please do comment If u have any Queries!

1 comment:

  1. #python3
    l=[]
    a=int(input())
    for i in range(a):
    l.append(int(input()))

    l.sort(reverse=True)
    print(l[1])

    ReplyDelete

Like