Quick actions

cmd+k|ctrl+k

Navigation

Languages

Nth largest and smallest

Snippet info

Language

Java

Visibility

public

Author

santhosh136

Created

2020-08-31T14:23:41Z

Updated

2020-08-31T14:23:41Z

import java.util.Arrays;
import java.util.Scanner;
import java.util.TreeSet;

class Main {
    // if there is no duplicate
    // if there is duplicate use TreeSet
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        // int a[] = {1,23,-9,12,34,5,67};
        // Arrays.sort(a);
        // int n = scan.nextInt();
        // System.out.println(a[n-1]);
        
        int a[] = {1,23,-9,12,34,5,67,-9,1};
        int n = 3;
        TreeSet<Integer> set = new TreeSet<>();
        for(int i: a) set.add(i);
        int i=1;
        for(int s: set) {
            if(i==n) {System.out.println(s); break;}
            i++;
        }
        
    }
}
INFO