Handle Score
@Ibrahim Nash 6560
@blackshadows 6400
@mb1973 5777
@Quandray 5271
@akhayrutdinov 5111
@saiujwal13083 5074
@kirtidee18 4356
@sanjay05 3762
@mantu_singh 3638
@gfgaccount 3601
@marius_valentin_dragoi 3525
Complete Leaderboard
Mega Sale
Basic Accuracy: 39.56% Submissions: 369 Points: 1

Mr. Geek is a greedy seller. He has a stock of N laptops which comprises of both useful and useless laptops. Now, he wants to organize a sale to clear his stock of useless laptops. The prices of N laptops are Ai each consisting of positive and negative integers (-ve denoting useless laptops). In a day, he can sell atmost M laptops. Mr. Geek being a greedy seller want to earn maximum profit out of this sale. So, help him maximizing his profit by selling useless laptops.


Example 1:

N=4, M=3
A[] = {-6, 0, 35, 4}
Geek sells the laptops with price -6 and
earns Rs. 6 as profit.


Example 2:

N=5, M=10
A[] = {1, -2, -3, -4, 5}
Geek sells the laptops with price -2,
-3 and -4 and earns Rs. 9 as profit.


Your Task:  
You don't need to read input or print anything. Your task is to complete the function maxProfit() which takes the array A[], its size N and an integer M as inputs and returns the maximum profit Mr. Geek can earn in a single day.


Expected time Complexity: O(N)
Expected time Complexity: O(1)


1 ≤ N ≤ 105
1 ≤ M ≤ 105
-106 ≤ Ai ≤ 106

to report an issue on this page.


We strongly recommend solving this problem on your own before viewing its editorial. Do you still want to view the editorial?


All Submissions

My Submissions:

Login to access your submissions.

Mega Sale

Output Window