MCPcopy Create free account
hub / github.com/TheAlgorithms/Go / FindKthMax

Function FindKthMax

math/kthnumber.go:11–14  ·  view source on GitHub ↗

FindKthMax returns the kth large element given an integer slice with nil `error` if found and returns -1 with `error` `search.ErrNotFound` if not found. NOTE: The `nums` slice gets mutated in the process.

(nums []int, k int)

Source from the content-addressed store, hash-verified

9// with nil `error` if found and returns -1 with `error` `search.ErrNotFound`
10// if not found. NOTE: The `nums` slice gets mutated in the process.
11func FindKthMax(nums []int, k int) (int, error) {
12 index := len(nums) - k
13 return kthNumber(nums, index)
14}
15
16// FindKthMin returns kth small element given an integer slice
17// with nil `error` if found and returns -1 with `error` `search.ErrNotFound`

Callers 1

TestFindKthMaxFunction · 0.85

Calls 1

kthNumberFunction · 0.85

Tested by 1

TestFindKthMaxFunction · 0.68