Java Program to Find kth Largest Element in a Sequence

This is a java program to find kth largest element form the given sequence of numbers. We find the kth largest by sorting the sequence first and then returning the element at position N-k, which qualifies as the kth largest element of the sequence.

Here is the source code of the Java Program to Find kth Largest Element in a Sequence. The Java program is successfully compiled and run on a Windows system. The program output is also shown below.

//This is a java program to find kth largest element in randomly generated sequence
import java.util.Random;
import java.util.Scanner;
 
public class Kth_Largest 
{
    static int N = 20;
    static int []sequence = new int[N];
    public static void sort()
    {
        System.out.println("The Sequence is: ");
        for(int i=0; i<N; i++)
            System.out.print(sequence[i] + " ");
        System.out.println();
 
        int i, j, temp;
        for (i = 1; i< N; i++) 
        {
            j = i;
            temp = sequence[i];    
            while (j > 0 && temp < sequence[j-1])
            {
                sequence[j] = sequence[j-1];
                j = j-1;
            }
            sequence[j] = temp;            
        }        
    }
 
    public static void main(String args[])
    {
        Random random = new Random();
 
        for(int i=0; i<N; i++)
            sequence[i] = Math.abs(random.nextInt(100));
 
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter the kth largest to find");
        int k = sc.nextInt();
 
        sort();
        System.out.println(k+"th largest element is " + sequence[N-k-1]);
        sc.close();
    }
}

Output:

$ javac Kth_Largest.java
$ java Kth_Largest
 
Enter the kth largest to find
5
The Sequence is: 
77 20 91 48 29 55 2 53 29 7 20 91 78 21 87 81 49 53 77 1 
5th largest element is 77

Related posts:

Java Program to Check Whether a Given Point is in a Given Polygon
Guide to Mustache with Spring Boot
Collect a Java Stream to an Immutable Collection
Giới thiệu SOAP UI và thực hiện test Web Service
Từ khóa throw và throws trong Java
Java Program to Construct a Random Graph by the Method of Random Edge Selection
Using a Custom Spring MVC’s Handler Interceptor to Manage Sessions
Java Program for Topological Sorting in Graphs
A Guide to WatchService in Java NIO2
Get and Post Lists of Objects with RestTemplate
Guide to Spring 5 WebFlux
Java Program to Implement Dijkstra’s Algorithm using Queue
Cachable Static Assets with Spring MVC
List Interface trong Java
Receive email using IMAP
Loại bỏ các phần tử trùng trong một ArrayList như thế nào trong Java 8?
Java Program to Solve Travelling Salesman Problem for Unweighted Graph
Java Program to Implement Warshall Algorithm
Guide to Guava Multimap
Guide to @JsonFormat in Jackson
Binary Numbers in Java
Java Program to Implement Gale Shapley Algorithm
Java Program to Remove the Edges in a Given Cyclic Graph such that its Linear Extension can be Found
Build a REST API with Spring and Java Config
Introduction to Spring Cloud CLI
Java Program to Implement Double Order Traversal of a Binary Tree
Apache Commons Collections MapUtils
Java Program to Construct an Expression Tree for an Infix Expression
Guide to Selenium with JUnit / TestNG
Java Program to Implement Johnson’s Algorithm
Java Program to Implement Circular Singly Linked List
Converting Strings to Enums in Java