HBDANA - Editorial

PROBLEM LINK:

Practice
Contest

Setter: Aakash Jaiswal

Tester: Deepansh Parmani

Editorialist: Akash Bhalotia

DIFFICULTY:

CAKEWALK

PREREQUISITES:

Sorting

PROBLEM:

Given an array of size N, find the sum of the K largest elements of the array.

QUICK EXPLANATION:

Sort the array in non-decreasing or ascending order. Find the sum of the last K elements.

EXPLANATION:

One of the simplest ways to solve this problem is to use Sorting.

As 1 \leq N \leq 2* 10^5, O(N^2) sorting algorithms like Bubble Sort, Selection Sort, Insertion Sort, etc. would result in Time Limit Exceeded (TLE). Thus we have to use faster sorting algorithms like Merge Sort, Quick Sort, Heap Sort, etc. which generally result in a time complexity of O(NlogN).

In CP (Competitive Programming), one doesn’t generally implement the full sorting algorithm, but uses in-built library sorting function of the language;

For Example:

  1. Arrays.sort() of JAVA
  2. qsort() of C
  3. sort() of C++
  1. Sort the array in ascending order. Now the K-largest elements are all populated towards the end of the array, from index (N-K+1) to index N, 1-based indexing.

  2. Using a loop, you can find the sum of the last K elements of this sorted array. A 32-bit integer datatype should be sufficient to store the sum, as 1\leq a[i]\leq 10^4 and 1\leq K \leq 20.

Another way to solve this problem, using sorting, is to sort the array in descending order and find the sum of the first K elements in it.

COMPLEXITY:

Time complexity: O(NlogN) per test case

Space Complexity: O(N)

SOLUTIONS:

Editorialist’s solution

Similar Problem:

Find the sum of the K-smallest elements of the array. Same constraints.

===============================================

ALTERNATIVE SOLUTION:

  1. O(N*K) using Looping:
Click to view

Run a loop K times. Each time find the largest element in the array. Add it to the sum. Set the element equal to -1, so that it doesn’t get selected as the largest element again.

In my approach, I have used swapping instead of setting the next largest element =-1 each time.

Solution

  1. O(N*logK) using a Min-Heap:

Heap

Click to view

Create a Min-Heap of size K. Insert the first K elements into it and each time add that element to the sum. For the next (N-K) elements, if an element is greater than the root element of the heap,

  1. Remove the root element

  2. Subtract it from the sum

  3. Add the current element to the heap

  4. Add it to the sum.

Solution

USEFUL READ-UPS AND TUTORIALS:

Don’t know a topic? No worries :slight_smile: You’ll find the links below useful.

Click to view
  1. How to get Started?

  2. Time Complexity and Big-O

  3. Sorting

  4. Heaps

General Tutorials:

There are many sites which provide excellent tutorials on data structures and algorithms. Some of them, in no particular order, are:

  1. Topcoder

  2. ICO Study Material

  3. GeeksForGeeks

  4. Codeforces

  5. HackerEarth

===============================================

BONUS:

Can we solve this problem in O(N) time?

Click to view
  1. Quickselect : Solution

  2. Worst-case linear-time

==================================================================================================

Feel free to share your approach if it differs. If you have any doubts or you feel stuck at any point, feel free to ask them below. You can also reach me at akashbhalotia\.unofficial@gmail\.com . I tried my best to make the editorial beginner-friendly. Suggestions are always welcomed :slight_smile: