Monday, March 7, 2022

Kth Largest Number in an Array

 

Program -

public class KLargest{

     public static void main(String []args){
         
        int a[] = {1,2,3,4,5,6,7,8};
        int max = 0;
        int index = 0;
        int k = 3;
        for(int j = 1; j <= k; j++)
        {
            max = a[0];
            for(int i = 0; i < a.length; i++)
            {
                if(a[i] > max)
                {
                    max = a[i];
                    index = i;
                }
            }
            a[index] = Integer.MIN_VALUE;
        }
        System.out.println(max);
     }
}

Video explanation -


 

No comments:

Post a Comment