Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion Problem1.java

This file was deleted.

1 change: 0 additions & 1 deletion Problem2.java

This file was deleted.

100 changes: 100 additions & 0 deletions src/MinHeap.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
/*
problem statement - Design MinHeap
Time Complexity -
insert - O(log n) inserting a new key takes O(Log n) time. We add a new key at the end of the tree. If a new key is larger than its parent, then we don’t need to do anything. Otherwise, we need to traverse up to fix the violated heap property.
remove - O(log n) if removing the root element violates the property of heap we have to perform heapify starting from the root to retore heap properties
Space Complexity - O(n)
*/

public class MinHeap {
private int [] minHeap;
private int size;
private int maxSize;
private static final int FRONT = 1;

public MinHeap(int maxSize) {
this.maxSize = maxSize;
this.size = 0;
minHeap = new int[this.maxSize+1];
minHeap[0] = Integer.MIN_VALUE;
}

// Method to return the position the parent node
private int findParent(int index) {
return index/2;
}
//method to return the position the left node
private int findLeft(int index) {
return (index*2);
}
//method to return the position of right node

private int findRight(int index) {
return (index*2)+1;
}
//method to find the leaf node
private boolean isLeafNode(int index) {
if(index > (size/2)){
return true;
}
return false;
}
private void minHeapify(int index){
while (!isLeafNode(index)){
int left = findLeft(index);
int right = findRight(index);
int smallest = left;
if(right <= size && minHeap[right] < minHeap[left]){
smallest = right;
}
if(minHeap[index] <= minHeap[smallest]){
break;
}
swap(index,smallest);
index = smallest;
}
}

private void swap(int index1, int index2) {
int temp = minHeap[index1];
minHeap[index1] = minHeap[index2];
minHeap[index2] = temp;
}
public void insert(int value) {
if(size >= maxSize){
return;
}
minHeap[++size] = value;
int current = size;
//swap the current value if the current value is less than its parent
while(minHeap[current] < minHeap[findParent(current)]){
swap(current, findParent(current));
current = findParent(current);
}
}
public void print() {
for (int i = 1; i <= size / 2; i++) {
System.out.print(" PARENT : " + minHeap[i]);

int left = findLeft(i);
int right = findRight(i);

if (left <= size) {
System.out.print(" LEFT CHILD : " + minHeap[left]);
}

if (right <= size) {
System.out.print(" RIGHT CHILD : " + minHeap[right]);
}

System.out.println();
}
}
public int remove(){
int value = minHeap[FRONT];
minHeap[FRONT] = minHeap[size--];
minHeapify(FRONT);
return value;
}

}
22 changes: 22 additions & 0 deletions src/MissingElement.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/*
problem statement - Given a sorted array arr[] of n-1 integers, these integers are in the range of 1 to n. There are no duplicates in the array. One of the integers is missing in the array. Find the missing integer.
Approach: Using binary search, calculate the mid and check if the mid is in the right place if yes the missing number would be in the right side of mid update low = mid+1 position if not missing would be the left half
Time Complexity - O(log n)
Space Complexity - O(1)
*/

public class MissingElement {
public static int missingNumber(int[] arr) {
int low = 0;
int high = arr.length - 1;
while (low <= high) {
int mid = low + (high - low) / 2;
if (arr[mid] == mid + 1) {
low = mid + 1;
} else {
high = mid - 1;
}
}
return low + 1;
}
}
19 changes: 19 additions & 0 deletions src/TestCompetitiveCoding1.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
public class TestCompetitiveCoding1 {
public static void main(String[] args) {
MinHeap minHeap = new MinHeap(10);
minHeap.insert(5);
minHeap.insert(3);
minHeap.insert(2);
minHeap.insert(1);
minHeap.insert(4);
minHeap.insert(6);
minHeap.insert(7);
minHeap.print();
System.out.println("The Min val is "
+ minHeap.remove());
minHeap.print();
// Test for the missing element
int arr[] = {1,2,3,4,6,7,8};
System.out.println("Missing element in an array is "+MissingElement.missingNumber(arr));
}
}