Skip to content

Commit 41a2e2d

Browse files
committed
new concept added
1 parent 59083e2 commit 41a2e2d

File tree

3 files changed

+129
-0
lines changed

3 files changed

+129
-0
lines changed

README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,10 @@ For eg storing the minimum so far in another stack so that each time when a numb
6565
- Sometimes to find largest element in an array, min heap can be made, for few first elements, and each
6666
time comparisons can be done with remaining element to eliminate the minimum elements.
6767
- Methods where min and max heap can be applied BST can also be used (depends on question)
68+
- Sometimes a combination of min heap and max heap can be used to solve questions. Min heap can contain
69+
maximum elements from array and max heap can contain minimum elements from array (this can be known at runtime without sorting the array). (refer question 7)
70+
- Since each data structure has its own significance, sometimes it is good to use multiple data structures
71+
like min heap + max heap + linked list as BST and so on for some algos to perform series of operations
6872

6973
# Topic0: Programming Questions
7074

@@ -186,6 +190,7 @@ time comparisons can be done with remaining element to eliminate the minimum ele
186190
- [Find a max element in a min-heap](/heaps/question4.c)
187191
- [Build a min-heap and write algo to delete an arbitrary element](/heaps/question5.c)
188192
- [Find k largest elements from an array](/heaps/question6.c)
193+
- [Find median in a stream of numbers](/heaps/question7.c)
189194

190195
## Some important concepts to solve algos better
191196

@@ -223,6 +228,8 @@ time comparisons can be done with remaining element to eliminate the minimum ele
223228
- 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.
224229
- For Binary Search tree implementation using an array, space complexity is O(2^n), but using linked list, it
225230
is O(n)
231+
- Stream of numbers mean the numbers are coming one after the other and for each input change you have to
232+
find what is stated in the question.
226233
227234
# Topic1: Introduction
228235

heaps/question6.c

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,5 +126,84 @@ int main(){
126126

127127
return 0;
128128
}
129+
//=============================================================================================
130+
//METHOD4
131+
#include <stdio.h>
132+
#include <stdlib.h>
133+
#define MAX 100
134+
135+
void swap(int *a, int *b){
136+
int temp = *a;
137+
*a = *b;
138+
*b = temp;
139+
}
140+
141+
void minHeapify(int *arr,int i, int size){
142+
int left = 2*i+1,right=2*i+2,smallest, heapSize = size;
143+
if(left <= heapSize-1 && arr[i] < arr[left]){
144+
smallest = i;
145+
}else{
146+
smallest = left;
147+
}
148+
if(right <= heapSize-1 && arr[right] < arr[smallest]){
149+
smallest = right;
150+
}
151+
if(smallest <= heapSize - 1 && smallest !=i){
152+
swap(&arr[smallest],&arr[i]);
153+
minHeapify(arr,smallest,size);
154+
}
155+
}
129156

157+
void makeMinHeapForK(int *arr, int k, int size){
158+
int index;
159+
if(k%2==0){
160+
index = k/2-1;
161+
}else{
162+
index = k/2;
163+
}
164+
for(int i=index; i>=0;i--){
165+
minHeapify(arr,i,k);
166+
}
167+
}
168+
169+
void display(int arr[], int size){
170+
printf("first %d largest elements are ...\n", size);
171+
for(int i=0; i<size;i++){
172+
printf("%d\t", arr[i]);
173+
}
174+
printf("\n");
175+
}
176+
177+
void getKLargest(int *arr, int k, int size){
178+
int temp;
179+
for(int i=k; i<size;i++){
180+
if(arr[i]>arr[0]){
181+
temp = arr[i];
182+
arr[i] = arr[0];
183+
arr[0] = temp;
184+
minHeapify(arr,0,k);
185+
}
186+
}
187+
188+
display(arr,k);
189+
}
190+
191+
int main(){
192+
int arr[MAX], size,k,temp;
193+
printf("Enter the array size\n");
194+
scanf("%d",&size);
195+
for(int i=0; i<size;i++){
196+
printf("enter the %d th element\n", i);
197+
scanf("%d",&arr[i]);
198+
}
199+
200+
printf("Enter the value of k\n");
201+
scanf("%d",&k);
202+
203+
makeMinHeapForK(arr,k,size);
204+
205+
getKLargest(arr,k,size);
206+
207+
return 0;
208+
}
130209

heaps/question7.c

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/*
2+
Find median in a stream of numbers
3+
4+
Stream of numbers mean the numbers are coming one after the other and for each input change you have to
5+
find what is stated in the question.
6+
7+
METHOD1:
8+
You can apply merge sort to sort the elements each time, anf find median, in case of even numbers median
9+
is middle two elements and odd numbers median is the middle element. But since for each input we will have
10+
to apply merge sort on the previous inputted numbers as well. So here insertion sort will work better, as
11+
once applied we will just have to apply it for the 1 input that is coming.
12+
Time complexity: O(n^2) //for first sort for rest of the inputs it can be done in O(n) for each input
13+
Space complexity: O(1)
14+
15+
METHOD2:
16+
Creating a BST. In this case since elements are streaming we will make sure that at any point the
17+
number of elements in the left sub tree and right sub tree are equal or atmost have a difference of 1.
18+
If they are equal, root element in the median, if they are not equal, then if the LST has more elements
19+
then max element (traverse to left and then to right most) in this tree is the second median, in case
20+
RST has more then least element in this tree is the second median. Each time when the difference in the
21+
number of elements increases to more than 1, then whichever side is dominant (if right, pick the least from
22+
right, if left pick highest from left) and substitute in place of the root and make the root to be inserted
23+
again as another element. This was the tree will be kind of equal at both the ends.
24+
Time complexity: O(n) //Worst case if LST and RST are both skewed, average case can be logn also
25+
Space complexity: O(n) //linked list representation of BST but it can take a lot of space because of pointers
26+
but using array is not efficient because in worst case if the three is skewed array will take up 2^n time.
27+
28+
METHOD3:
29+
Maintain two heaps, one min heap and one max heap. Insert min half elements from the array in the max heap and
30+
max half in the min heap. Now root of the min heap and root of the max heap will be the medians.
31+
They represent positions of two elements while in the sorted array. (dry run it). The positions of other
32+
elements does not really matter. When element is inserted in max heap, if the element is lesser than root,
33+
only then it will be inserted otherwise it will be inserted in the min heap to enusre that max heap contains
34+
only the lower half of the elements. Now if at this stage the min heap has more than 1 element greater than
35+
the max heap, min heap root will be deleted and min heapify will be run on it and this root will now be
36+
inserted into max heap and maxheapify will run to make sure that both the heaps are balanced at any stage.
37+
Time complexity: O(logn) + O(logn) + O(logn) //inserting in max heap + removing from max heap + Inserting in min
38+
At any point in worst case O (logn) will be the complexity. For n elements it will be nlogn
39+
Space complexity: O(n) //since elements are coming in stream we cannot do both min and max heap in the same
40+
data structure, so we will have to make separate data structures.
41+
42+
43+
*/

0 commit comments

Comments
 (0)