Skip to content

Commit ff2aa2d

Browse files
committed
new questions added
1 parent 93ba2b9 commit ff2aa2d

File tree

5 files changed

+43
-6
lines changed

5 files changed

+43
-6
lines changed

README.md

+2
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,8 @@ time which is a better method.
196196
- [Find k largest elements from an array](/heaps/question6.c)
197197
- [Find median in a stream of numbers](/heaps/question7.c)
198198
- [Given k-sorted lists, Find the minimum range to which at last one number belongs from every list](/heaps/question8.c)
199+
- [Print out all integers in the form of a^3+b^3 where a and b are integers between 0 and N in sorted order](/heaps/question9.c)
200+
- [Convert BST to max heap](/heaps/question10.c)
199201

200202
## Some important concepts to solve algos better
201203

heaps/question10.c

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/*
2+
Convert BST to max heap
3+
4+
METHOD1:
5+
Since BST is generally not stored in the array, so we cannot use the data structure to convert directly
6+
into the max heap as we did in min to max heap or vice versa conversion. Therefore we do a INORDER traversal
7+
of BST. It will give us numbers in ascending order. Now reversing the array will be descending order which is
8+
nothing but a MAX HEAP
9+
Time complexity: O(n)
10+
Space complexity: O(1)
11+
12+
Note in the method above we could have simply done reverse INORDER TRAVERSAL in order to avoid reversing the
13+
array. In that we traverse the RST print and then traverse LST
14+
*/

heaps/question8.c

+8-6
Original file line numberDiff line numberDiff line change
@@ -23,24 +23,26 @@ Space complexity: O(k) //storing the k elements
2323
#include <stdio.h>
2424
#include <stdlib.h>
2525

26-
void findInterval(int k, int l1[], int l2[], int l3[], int l4[],int size){
26+
void findInterval(int arr[], int k, int size){
27+
28+
2729

2830
}
2931

3032
int main(){
31-
int k, size;
33+
int k, size, arr[], elm;
3234
printf("enter the value if k\n");
3335
printf("enter the size of each array\n");
3436
scanf("%d",size);
3537
scanf("%d",&k);
3638
for(int i=0;i<k;i++){
3739
for(int i=0;i<size;i++){
38-
40+
printf("enter the %d th element\n");
41+
scanf("%d",&elm)
42+
arr[i+k*size] = elm;
3943
}
4044
}
41-
42-
43-
findInterval(k,l1,l2,l3,l4, size);
4445

46+
findInterval(arr,k,size);
4547
return 0;
4648
}

heaps/question9.c

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/*
2+
Print out all integers in the form of a^3+b^3 where a and b are integers between 0 and N in sorted order
3+
4+
METHOD1:
5+
Here all integers that lie between 0 and N will, combinations of those will be made with each other and
6+
a3+b3 will be computed for each and presented in a sorted order. Imagine it has a matrix lets say the
7+
range is between 0 to 3, then combinations are 00,01,02,03,10,11,12,13 and so on. Therefore 0^3+0^3 and so
8+
on is computed and each time lets say value is in a two dimensional array. Now the entire column of
9+
the array is picked and a min heap is made out of it. Each time min is deleted and new element from the
10+
row in serial order is inserted and minified. This way we will get all elements in sorted order
11+
Time complexity: O(n^2)logn //total elements are n^2 inserted and deleting from the min heap takes logn
12+
time.
13+
Space complexity: O(n) to store n elements in a heap. matrix wont be taken into consideration because it
14+
will not be a matrix, its just for understanding purposes. In actual implementation everything can be done
15+
on runtime
16+
17+
*/

nextquestions.md

+2
Original file line numberDiff line numberDiff line change
@@ -44,5 +44,7 @@ TODO:
4444
- building min/max heap for duplicate elements
4545
- Do BST method in question 6 for heaps
4646
- Do BST method for question 7 in heaps (question7 complete to be done)
47+
- question8 in heaps to be done still
48+
- question9 and question 10 to be done still
4749

4850

0 commit comments

Comments
 (0)