[Updated] Goldman Sachs Aptitude Test Questions and Answers
Practice List of TCS Digital Coding Questions !!!
Take 50+ FREE!! Online Data Interpretation Mock test to crack any Exams.

Program Discussion :: Array

Home > Programs > Array

330 / 30

Find the 2nd largest element in array, without sorting and swap in the middle element.

Answer:

import java.util.*;
import java.io.*;
class secondlarele{
    public static void main(String args[])throws IOException{
        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
        int n=Integer.parseInt(br.readLine());
        int[]a=new int[n];
        for(int i=0;ifbig)
        {
            int temp=fbig;
            fbig=sbig;
            sbig=temp;
        }
        for(int i=2;ifbig)
            {
                sbig=fbig;
                fbig=a[i];
            }
            else if(a[i]>sbig)
            {
                sbig=a[i];
            }
        }
        System.out.println("first largest element:" fbig);
        System.out.println("second largest element:" sbig);
    }
}

Asked In :: Global Edge

Post Your Answer Here:

Language:

Post Your Reply Here: