Skip to content

Commit 4c3fa13

Browse files
committed
new question added
1 parent 72d73d5 commit 4c3fa13

File tree

3 files changed

+48
-2
lines changed

3 files changed

+48
-2
lines changed

README.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,9 @@ For eg storing the minimum so far in another stack so that each time when a numb
6262
### Heaps (methods that can be applied):
6363
- A large heap can be delcared and only a small portion of it can always be included in the heap operations.
6464
- Whenever a heap is built, swapping etc. will be there if elements do not follow the heap property (max or min)
65-
65+
- Sometimes to find largest element in an array, min heap can be made, for few first elements, and each
66+
time comparisons can be done with remaining element to eliminate the minimum elements.
67+
- Methods where min and max heap can be applied BST can also be used (depends on question)
6668

6769
# Topic0: Programming Questions
6870

@@ -183,6 +185,7 @@ For eg storing the minimum so far in another stack so that each time when a numb
183185
- [Write a program for heap sort](/heaps/question3.c)
184186
- [Find a max element in a min-heap](/heaps/question4.c)
185187
- [Build a min-heap and write algo to delete an arbitrary element](/heaps/question5.c)
188+
- [Find k largest elements from an array](/heaps/question6.c)
186189

187190
## Some important concepts to solve algos better
188191

@@ -218,6 +221,8 @@ For eg storing the minimum so far in another stack so that each time when a numb
218221
- There is an O(n) time algo to convert an element into a heap. So no need to sort as sorting take O(nlogn)
219222
- Recursion adds to the space complexity as well as time complexity.
220223
- In a max heap, finding min, deleting random element or searching an element will take O(n) time because here max heap is as good as an array.
224+
- For Binary Search tree implementation using an array, space complexity is O(2^n), but using linked list, it
225+
is O(n)
221226
222227
# Topic1: Introduction
223228

heaps/question6.c

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
Find k largest elements from an array
3+
4+
METHOD1:
5+
Find max element from the array. Now output that element and replace it with the last element and decrease the
6+
size of the array. Again find the max element and repeat the steps above. Keep doing it k times.
7+
Time complexity: O(kn) //assuming k is less than n
8+
Space complexity: O(1)
9+
10+
METHOD2:
11+
Sort the array and display the last k elements.
12+
Time complexity: O(nlogn)
13+
Space complexity: O(1)
14+
15+
METHOD3:
16+
Build a max heap from the given array. Delete max k times and print
17+
Time complexity: O(n) + O(klogn) //n for building the heap klogn as for deleting max k times. If k is almost
18+
equal to n here, time complexity will become O(nlogn), if small then O(n)
19+
Space complexity: O(1)
20+
21+
METHOD4:
22+
Create a binary search tree from the given elements. Find the maximum each time from the BST and remove it
23+
fromt the tree and repeat it k times.
24+
Time complexity: O(n^2) + O(kn) //n^2 to make the binary search tree as in worst case the tree can be skewed.
25+
n to search for each element. Since it is done k times O(kn)
26+
Space complexity: O(2^n) OR O(n) //BST using array or linked list
27+
28+
METHOD5:
29+
Make a min heap using the first k elements in the array and assume that they are the largest k elements in the
30+
array. Now from k+1th element start comparing each n-k remaining elements with the root of the heap and replace
31+
if the root of the heap is smaller than that element and apply heapify again.
32+
Repeat these steps for all n-k remaining elements and in the end the min-heap will have k largest elements
33+
from the array
34+
Time complexity: O(k) + O((n-k)logk) //k for building the heap having k elements and (n-k)logk elements because
35+
that many time minheapify will be called
36+
Space complexity: O(1) //min heap and max heap do not require additional data structure. Same array can
37+
be manipulated unless mentioned in the question
38+
*/

nextquestions.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,4 +41,7 @@ TODO:
4141
- question 8, 9 implementation for stacks and queues to be done
4242
- Program to find then the stock should be sold in order to gain max profit
4343
- insert key in heap question2 to be done
44-
- building min/max heap for duplicate elements
44+
- building min/max heap for duplicate elements
45+
- Do BST method in question 6 for heaps
46+
47+

0 commit comments

Comments
 (0)