Answer:
def max_n(arr, n):
arr.sort()
i = len(arr)-n
return arr[i:]
Explanation:
Define a function called max_n that takes two parameters, an array and an integer
Sort the array
In order to get the last n largest values, we need to slice the array. To do that we need a starting point. Our starting point of slicing will be the "lentgh of the array - n". That means, if we start slicing from that index to the last in the sorted array, we will get the last n largest values.
Assume array is 10, 2, 444, 91 initially.
When we sort it, it becomes 2, 10, 91, 444
Now let's say we want last 2 largest values, our sliced array index should start from 2 and go until the end
i = 4 - 2 = 2 → arr[2:] will return us 91 and 444