Solution by vthorat1


  • 0
    V

    public static void main(String[] args) {
    int[] nums = {3,2,1,5,6,4};
    int k=2;

    	int output =findKthLargest(nums,k);
    	
    	System.out.println("Output is :"+output);
    }
    

    public static int findKthLargest(int[] nums, int k) {

    	for (int i = 0; i < nums.length; i++) {
    		for (int j = i + 1; j < nums.length; j++) {
    			if (nums[j] > nums[i]) {
    				int temp = nums[i];
    				nums[i] = nums[j];
    				nums[j] = temp;
    			}
    
    		}
    	}
    
    	for (int x = 0; x < nums.length; x++) {
    		System.out.println(nums[x]);
    	}
    	return nums[k - 1];
    }
    

    Output is 5.


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.