Kth Largest Element in an array with kotlin

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.

For example, given [3,2,1,5,6,4] and k = 2, return 5.

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

Kotlin program to Kth Largest Element in an array


import java.util.*
fun findKthLargest(nums: IntArray, k: Int): Int {
    Arrays.sort(nums)
    return nums[nums.size - k]
}
fun main(args: Array) {
    println(findKthLargest(intArrayOf(3,2,1,5,6,4), 2))
}


Output


5

 
Join My Facebook Group
Join Group
 

About Author

Myself KarthiQ, I am the author of this blog, I know ways to write a good article but some how I donot have the skills to make it to reach people, would you like help me to reach more people By sharing this Article in the social media.

Share this Article Facebook
Comment / Suggestion Section
Point our Mistakes and Post Your Suggestions