Find kth max and min element in array in Java

Find kth max and min element in array

Here, in this page we will discuss the program to find the kth max and min element in an array in Java . We use the concept of set in finding the kth maximum and minimum element of the array. We are giving with the size of the array along with array elements and the value of K. We have to print the maximum and the minimum element.

Find kth max and min element in array

Algorithm :

  • Take a length of the array as an input from the user followed by elements of the array.

  • Take the value of k from the user.

  • create a queue

  • Top elements will be removed if size>k

  • Top will be printed, and the top element will be the maximum of the array

Run

import java.io.*;
import java.util.*;
public
class Main {
    public
    static void main(String[] args) throws Exception {
        Scanner sc = new Scanner(System.in);
        int n, k, i;

        System.out.println("Enter the size of the array: ");
        n = 3;

        System.out.println("Enter the elements for the array: ");
        int arr[] = {1,2,4,5};


        System.out.println("Enter the value of k: ");
        k = 2;

        PriorityQueue queue = new PriorityQueue<>(Collections.reverseOrder());
        System.out.println("Kth smallest element is: ");

        for (i = 0; i < n; i++) { queue.add(arr[i]); if (queue.size() > k) {
                queue.poll();  // top elements will be removed if size>k
            }
        }
        System.out.println(queue.peek());  // top will be printed

        PriorityQueue queue1 = new PriorityQueue<>();
        System.out.println("Kth Largest element is: ");

        for (i = 0; i < n; i++) { queue1.add(arr[i]); if (queue1.size() > k) {
                queue1.poll();  // top elements will be removed if size>k
            }
        }
        System.out.println(queue1.peek());  // top will be printed
    }
}

Output

Enter the size of the array: 
Enter the elements for the array:
Enter the value of k:
Kth smallest element is:
2
Kth Largest element is:
2

5 comments on “Find kth max and min element in array in Java”


  • mstofficial19

    public static void find(int arr[], int k){
    int kMax;
    int kMin;
    for(int i=0;i<arr.length;i++) {
    for (int j = i + 1; j arr[j]) {
    int temp = arr[j];
    arr[j] = arr[i];
    arr[i] = temp;

    }
    }
    }
    for(int i=0;i<arr.length;i++){
    if(i==k-1){
    System.out.println(k+"nd/th minimum element is "+arr[i]);
    }
    if(i==arr.length-k){
    System.out.println(k+"nd/th maximum element is "+arr[i]);
    }
    }
    }


  • NAZIA KHAN

    public static void main(String[] args) {
    int[] a={1,6,5,7,2,8,4,3,2,1,9,8,9,3}; int k=3;
    Set s=new TreeSet();
    for(int i:a){
    s.add(i);
    }
    Iterator itr=s.iterator();
    int j=0;
    for(int i=0;i<=s.size()-k;i++){ //for maximun //for minimum do= i<k
    j=itr.next();
    }
    System.out.print(j);
    }


  • NAZIA KHAN

    public static void main(String[] args) {
    int[] a={1,4,3,2,5,6,6,7}; int k=3;
    Set s=new TreeSet(); // treeset stores in sorted acsending order and no duplicates
    int x=0;
    for(int i:a){
    s.add(i);
    }
    for(int i=0;i<s.size();i++){
    if(i==s.size()-k){ // for k maximum. if minimum then do i==k-1
    x=a[i];
    }
    }
    System.out.print(x);
    }