Skip to content

Commit

Permalink
new concept added
Browse files Browse the repository at this point in the history
  • Loading branch information
arorarahul committed Apr 22, 2017
1 parent 59083e2 commit 41a2e2d
Show file tree
Hide file tree
Showing 3 changed files with 129 additions and 0 deletions.
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,10 @@ For eg storing the minimum so far in another stack so that each time when a numb
- Sometimes to find largest element in an array, min heap can be made, for few first elements, and each
time comparisons can be done with remaining element to eliminate the minimum elements.
- Methods where min and max heap can be applied BST can also be used (depends on question)
- Sometimes a combination of min heap and max heap can be used to solve questions. Min heap can contain
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)
- Since each data structure has its own significance, sometimes it is good to use multiple data structures
like min heap + max heap + linked list as BST and so on for some algos to perform series of operations

# Topic0: Programming Questions

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

## Some important concepts to solve algos better

Expand Down Expand Up @@ -223,6 +228,8 @@ time comparisons can be done with remaining element to eliminate the minimum ele
- 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.
- For Binary Search tree implementation using an array, space complexity is O(2^n), but using linked list, it
is O(n)
- Stream of numbers mean the numbers are coming one after the other and for each input change you have to
find what is stated in the question.
# Topic1: Introduction
Expand Down
79 changes: 79 additions & 0 deletions heaps/question6.c
Original file line number Diff line number Diff line change
Expand Up @@ -126,5 +126,84 @@ int main(){

return 0;
}
//=============================================================================================
//METHOD4
#include <stdio.h>
#include <stdlib.h>
#define MAX 100

void swap(int *a, int *b){
int temp = *a;
*a = *b;
*b = temp;
}

void minHeapify(int *arr,int i, int size){
int left = 2*i+1,right=2*i+2,smallest, heapSize = size;
if(left <= heapSize-1 && arr[i] < arr[left]){
smallest = i;
}else{
smallest = left;
}
if(right <= heapSize-1 && arr[right] < arr[smallest]){
smallest = right;
}
if(smallest <= heapSize - 1 && smallest !=i){
swap(&arr[smallest],&arr[i]);
minHeapify(arr,smallest,size);
}
}

void makeMinHeapForK(int *arr, int k, int size){
int index;
if(k%2==0){
index = k/2-1;
}else{
index = k/2;
}
for(int i=index; i>=0;i--){
minHeapify(arr,i,k);
}
}

void display(int arr[], int size){
printf("first %d largest elements are ...\n", size);
for(int i=0; i<size;i++){
printf("%d\t", arr[i]);
}
printf("\n");
}

void getKLargest(int *arr, int k, int size){
int temp;
for(int i=k; i<size;i++){
if(arr[i]>arr[0]){
temp = arr[i];
arr[i] = arr[0];
arr[0] = temp;
minHeapify(arr,0,k);
}
}

display(arr,k);
}

int main(){
int arr[MAX], size,k,temp;
printf("Enter the array size\n");
scanf("%d",&size);
for(int i=0; i<size;i++){
printf("enter the %d th element\n", i);
scanf("%d",&arr[i]);
}

printf("Enter the value of k\n");
scanf("%d",&k);

makeMinHeapForK(arr,k,size);

getKLargest(arr,k,size);

return 0;
}

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

0 comments on commit 41a2e2d

Please sign in to comment.