Monday, 27 November 2017

Java Program to Find kth Largest Element in a Sequence


Code:

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
            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
            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:

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


More Java Programs:
















100+ Best Home Decoration Ideas For Christmas Day 2019 To Make Home Beautiful

Best gifts for Christmas Day | Greeting cards for Christmas Day | Gift your children a new gift on Christmas day This Christmas d...