Given an array of N positive integers, print k largest elements from the array. 
Example 1:
Input:
N = 5, k = 2
arr[] = {12,5,787,1,23}
Output: 787 23
Explanation: First largest element in
the array is 787 and the second largest
is 23.
Example 2:
Input:
N = 7, k = 3
arr[] = {1,23,12,9,30,2,50}
Output: 50 30 23
Explanation: Three Largest element in
the array are 50, 30 and 23.
Your Task:
Complete the function kLargest() that takes the array, N and K as input parameters and returns a list of k largest element in descending order. 
Expected Time Complexity: O(N log K)
Expected Auxiliary Space: O(K)
Constraints:
1 ≤ N ≤ 10^{4}
K ≤ N
1 ≤ array[i] ≤ 10^{5}

Source: geeksforgeeks | Difficulty: EASY
Tags: Data structures

Starter code: #User function Template for python3



class Solution:

    #Function to return k largest elements from an array.

    def kLargest(self,li,n,k):

        # code here

Solutions:
import heapq

class Solution:

	def kLargest(self, li, n, k):
		pq = []
		ans = []
		n = len(li)
		for i in range(n):
			heapq.heappush(pq, -li[i])
		f = k
		while f > 0:
			a = heapq.heappop(pq)
			ans.append(-a)
			f -= 1
		return ans


class Solution:

	def kLargest(self, li, n, k):
		a = []
		l = sorted(li)
		for i in range(-1, -k - 1, -1):
			a.append(l[i])
		return a


class Solution:

	def kLargest(self, li, n, k):
		li.sort()
		c = 0
		i = -1
		a = []
		while c < k:
			a.append(li[i])
			i -= 1
			c += 1
		return a


import heapq

class Solution:

	def kLargest(self, li, n, k):
		for i in range(n):
			li[i] = li[i] * -1
		heapq.heapify(li)
		ans = []
		for i in range(k):
			ans.append(-1 * heapq.heappop(li))
		return ans


class Solution:

	def kLargest(self, li, n, k):
		op = []
		li.sort(reverse=True)
		for i in range(k):
			op.append(li[i])
		return op


class Solution:

	def kLargest(self, li, n, k):
		li.sort(reverse=True)
		return li[:k]


class Solution:

	def kLargest(self, li, n, k):
		list = []
		li.sort()
		li = li[::-1]
		for i in range(k):
			list.append(li[i])
		return list


class Solution:

	def kLargest(self, li, n, k):
		li.sort()
		return li[n - k:][::-1]


class Solution:

	def kLargest(self, li, n, k):
		l = sorted(li)[::-1]
		return l[:k]


class Solution:

	def kLargest(self, li, n, k):
		l = []
		li.sort()
		li.reverse()
		for i in range(0, k):
			l.append(li[i])
		return l


class Solution:

	def kLargest(self, li, n, k):
		li.sort()
		ans = []
		count = -1
		while k != 0:
			ans.append(li[count])
			k -= 1
			count -= 1
		return ans


class Solution:

	def kLargest(self, arr, n, k):
		arr.sort()
		arr.reverse()
		return arr[:k]


import heapq

class Solution:

	def kLargest(self, li, n, k):
		heap = []
		for value in li:
			heapq.heappush(heap, value)
			if len(heap) > k:
				heapq.heappop(heap)
		ans = []
		while len(heap) > 0:
			ans.append(heapq.heappop(heap))
		ans.reverse()
		return ans


class Solution:

	def kLargest(self, arr, n, k):
		return sorted(arr)[::-1][:k]


class Solution:

	def kLargest(self, li, n, k):
		newarr = []
		li.sort(reverse=True)
		while k > 0:
			newarr.append(li[0])
			li.pop(0)
			k -= 1
		return newarr


import heapq

class Solution:

	def kLargest(self, arr, n, k):
		heap = []
		for i in arr:
			heapq.heappush(heap, i)
			if len(heap) > k:
				heapq.heappop(heap)
		result = []
		for i in range(len(heap)):
			result.append(heapq.heappop(heap))
		result.reverse()
		return result


import heapq

class Solution:

	def kLargest(self, li, n, k):
		ans = []
		heapq.heapify(ans)
		for i in range(n):
			heapq.heappush(ans, li[i])
			if len(ans) > k:
				heapq.heappop(ans)
		main = []
		while len(ans):
			main.append(heapq.heappop(ans))
		main.reverse()
		return main


import heapq

class Solution:

	def kLargest(self, arr, n, k):
		heap = []
		for i in arr:
			heapq.heappush(heap, -1 * i)
		result = []
		for i in range(k):
			result.append(-1 * heapq.heappop(heap))
		return result


from heapq import *

class Solution1:

	def kLargest(self, arr, n, k):
		maxheap = []
		for i in arr:
			heappush(maxheap, -i)
		ans = []
		for i in range(k):
			ans.append(-heappop(maxheap))
		return ans

class Solution:

	def kLargest(self, arr, n, k):
		arr = sorted(arr, reverse=True)
		return arr[:k]


from heapq import *

class Solution:

	def kLargest(self, arr, n, k):
		maxheap = []
		for i in arr:
			heappush(maxheap, -i)
		ans = []
		for i in range(k):
			ans.append(-heappop(maxheap))
		return ans


class Solution:

	def kLargest(self, li, n, k):
		m = sorted(li)
		p = m[-k:][::-1]
		return p


class Solution:

	def kLargest(self, li, n, k):
		list_a = sorted(li, reverse=True)
		return list_a[:k]


class Solution:

	def kLargest(self, li, n, k):
		x = sorted(li)
		x.reverse()
		lis = []
		for i in range(k):
			lis.append(x[i])
		return lis


import heapq

class Solution:

	def kLargest(self, li, n, k):
		return heapq.nlargest(k, li)


import heapq

class Solution:

	def kLargest(self, li, n, k):
		minh = []
		heapq.heapify(minh)
		ans = []
		for i in li:
			heapq.heappush(minh, i)
			if len(minh) > k:
				heapq.heappop(minh)
		while minh:
			x = heapq.heappop(minh)
			ans.append(x)
		return ans[::-1]


import heapq

class Solution:

	def kLargest(self, li, n, k):
		heapq._heapify_max(li)
		klargest = list()
		while k:
			element = heapq._heappop_max(li)
			klargest.append(element)
			k = k - 1
		return klargest


class Solution:

	def kLargest(self, li, n, k):
		li = sorted(li)[::-1]
		n = []
		for i in range(k):
			n += [li[i]]
		return n


import heapq

class Solution:

	def kLargest(self, li, n, k):
		l = []
		l2 = []
		for i in li:
			heapq.heappush(l, -i)
		while k != 0:
			l2.append(-heapq.heappop(l))
			k -= 1
		return l2


from heapq import heapify, heappush, heappop

class Solution:

	def kLargest(self, a, n, k):
		h = []
		heapify(h)
		for el in a:
			heappush(h, el)
			if len(h) > k:
				heappop(h)
		res = []
		while h:
			res.append(heappop(h))
		return sorted(res, reverse=True)


import heapq

class Solution:

	def kLargest(self, arr, n, k):
		queue = []
		for i in range(n):
			heapq.heappush(queue, arr[i])
		want = n - k
		while want > 0:
			heapq.heappop(queue)
			want -= 1
		return sorted(queue, reverse=True)


class Solution:

	def kLargest(self, li, n, k):
		i = 0
		t = []
		li = sorted(li)
		m = -1
		while i < k:
			t.append(li[m])
			m = m - 1
			i = i + 1
		return t


class Solution:

	def kLargest(self, li, n, k):
		li = sorted(li)
		li = li[::-1]
		return li[0:k]


class Solution:

	def kLargest(self, li, n, k):
		s = sorted(li)
		return s[n - k:][::-1]


class Solution:

	def kLargest(self, li, n, k):
		li = sorted(li)
		l = []
		while k != 0:
			l.append(li.pop())
			k -= 1
		return l


import heapq

class Solution:

	def kLargest(self, arr, n, k):
		li = []
		for i in range(len(arr)):
			heapq.heappush(li, arr[i])
			if len(li) > k:
				heapq.heappop(li)
		li.sort(reverse=1)
		return li


import heapq as hq

class Solution:

	def kLargest(self, li, n, k):
		hq.heapify(li)
		return hq.nlargest(k, li)


class Solution:

	def kLargest(self, li, n, k):
		ans = []
		li.sort()
		c = 0
		for i in range(n - 1, -1, -1):
			c += 1
			if c > k:
				break
			ans.append(li[i])
		return ans


class Solution:

	def kLargest(self, li, n, k):
		li.sort()
		a = li
		a = a[::-1]
		b = []
		for i in range(k):
			b.append(a[i])
		return b


class Solution:

	def kLargest(self, li, n, k):
		ar = []
		li.sort()
		i = n - 1
		p = 0
		while p < k:
			ar.append(li[i])
			i = i - 1
			p = p + 1
		return ar


class Solution:

	def kLargest(self, li, n, k):
		li = sorted(li)
		a = []
		p = 0
		q = len(li) - 1
		for i in range(q, 0, -1):
			p += 1
			if p <= k:
				a.append(li[i])
		return a


import heapq

class Solution:

	def kLargest(self, li, n, k):
		heapq.heapify(li)
		for i in range(n - k):
			heapq.heappop(li)
		li.sort()
		li.reverse()
		return li


class Solution:

	def kLargest(self, li, n, k):
		li.sort()
		x = li[n - k:]
		x.reverse()
		return x


import heapq

class Solution:

	def kLargest(self, li, n, k):
		if k > n:
			return li.sort(reverse=True)
		li.sort(reverse=True)
		return li[0:k]


class Solution:

	def kLargest(self, li, n, k):
		li.sort()
		c = []
		for i in range(k):
			c.append(li[-1])
			li.pop()
		return c


from heapq import *

class Solution:

	def kLargest(self, li, n, k):
		mi = []
		for i in li:
			if len(mi) < k:
				heappush(mi, i)
			else:
				heappush(mi, i)
				heappop(mi)
		return sorted(mi)[::-1]


import heapq

class Solution:

	def kLargest(self, li, n, k):
		mylist = []
		heapq.heapify(mylist)
		for i in range(n):
			heapq.heappush(mylist, li[i])
			if len(mylist) > k:
				heapq.heappop(mylist)
		res = []
		while len(mylist) > 0:
			res.append(heapq.heappop(mylist))
		res.reverse()
		return res


import heapq

class Solution:

	def kLargest(self, li, n, k):
		heap = []
		list1 = []
		for i in li:
			heapq.heappush(heap, i)
			if len(heap) > k:
				heapq.heappop(heap)
		while len(heap) > 0:
			list1.insert(0, heapq.heappop(heap))
		return list1


import heapq

class Solution:

	def kLargest(self, li, n, k):
		a = []
		heapq.heapify(a)
		for i in range(n):
			heapq.heappush(a, li[i])
			if len(a) > k:
				heapq.heappop(a)
		a.sort(reverse=True)
		return a


import heapq

class Solution:

	def kLargest(self, li, n, k):
		kq = []
		for i in range(k):
			heapq.heappush(kq, li[i])
		for k in li[k:]:
			heapq.heappush(kq, max(k, heapq.heappop(kq)))
		return sorted(kq)[::-1]


class Solution:

	def kLargest(self, li, n, k):
		a = []
		if len(set(li)) == 1:
			return [-1]
		li = sorted(li)
		for i in range(1, k + 1):
			i = i * -1
			a.append(li[i])
		return a


import heapq

class Solution:

	def kLargest(self, li, n, k):
		l = []
		size = 0
		for i in range(n):
			heapq.heappush(l, li[i])
			size = size + 1
			if size > k:
				heapq.heappop(l)
				size = size - 1
		l.sort(reverse=True)
		return l


class Solution:

	def kLargest(self, li, n, k):
		li.sort()
		list_return = []
		for i in li[:-k - 1:-1]:
			list_return.append(i)
		return list_return


class Solution:

	def kLargest(self, li, n, k):
		from heapq import heapify, heappush, heappop
		h = []
		heapify(h)
		for i in li:
			heappush(h, i)
			if len(h) > k:
				heappop(h)
		h.sort(reverse=True)
		return h


class Solution:

	def kLargest(self, li, n, k):
		from heapq import heapify, heappush, heappop
		kt = []
		for i in range(n):
			heappush(kt, -li[i])
		i = 0
		ans = []
		while i < k:
			ans.append(-heappop(kt))
			i += 1
		return ans


class Solution:

	def kLargest(self, li, n, k):
		count = 0
		li.sort(reverse=True)
		return li[:k:1]


class Solution:

	def maxheapify(self, arr, n, i):
		while True:
			largest = i
			left = 2 * i + 1
			right = 2 * i + 2
			if left < n and arr[left] > arr[largest]:
				largest = left
			if right < n and arr[right] > arr[largest]:
				largest = right
			if largest != i:
				(arr[i], arr[largest]) = (arr[largest], arr[i])
				i = largest
			else:
				break

	def kLargest(self, arr, n, k):
		for i in range(n // 2 - 1, -1, -1):
			self.maxheapify(arr, n, i)
		result = []
		for i in range(n - 1, n - k - 1, -1):
			result.append(arr[0])
			(arr[0], arr[i]) = (arr[i], arr[0])
			self.maxheapify(arr, i, 0)
		return result