Type Here to Get Search Results !

LeetCode Kth Largest Element in an Array Solution | CodingHumans |

0

 

Kth Largest Element in an Array 


Find the kth largest element in an unsorted array. Note that it is the kth largest element in the sorted order, not the kth distinct element.

Logic

we sort the array first and print the kth largest element counting from largest element

Example 1:


Input: [3,2,1,5,6,4] and k = 2


Output: 5


Example 2:


Input: [3,2,3,1,2,4,5,5,6] and k = 4


Output: 4


Note:

You may assume k is always valid, 1 ≤ k ≤ array's length.




Recommended: Please try your approach on your integrated development environment (IDE) first, before moving on to the solution.

Few words from CodingHumans : Don't Just copy paste the solution, try to analyze the problem and solve it without looking by taking the the solution as a hint or a reference . Your understanding of the solution matters.

HAVE A GOOD DAY 😁



Solution

( Java )


class Solution {

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

        Arrays.sort(nums);

        return (nums[(nums.length-1)-(k-1)]);

    }

}

If you have any doubts regarding this problem or  need the solution in other programming languages then join the Discussion  below .

Post a Comment

0 Comments

Top Post Ad

Below Post Ad