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
30 changes: 30 additions & 0 deletions BinarySearch.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// Find missing number in a Sorted Array of Natural Numbers

// Approach:
// Check the value at middle if it equals index + 1, it indicates all the numbers on the left are present
// and the missing number is on the right. Shift the search to right side. If the value at mid is greater
// than index + 1, it indicates there is a value less on the left side, so move the search to left size of array.
class BinarySearch{
static int missingNumber(int[] arr) {
int len = arr.length;

int low = 0;
int high = len-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;
}

public static void main(String[] args) {
int[] arr = {1, 2, 3, 4, 6, 7, 8};
System.out.println(missingNumber(arr));
}
}
96 changes: 96 additions & 0 deletions MinHeap.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
// Time complexity : Insert (O(log n)), Peek (O(1)), ExtractMin (O(log n))
// Space complexity : O(1)
public class MinHeap {
private static Integer MAX_SIZE = 1000;
private final Integer[] heap = new Integer[MAX_SIZE];
private Integer size = 0;

MinHeap() {}

// Add the element to the end and bubble it up to the correct position
public void insert(Integer element) {
if (size.equals(MAX_SIZE)) return;

heap[size] = element;
heapifyUp(size-1);
size++;
}

private void heapifyUp(int index) {
while(index > 0){
Integer parentIndex = getParentIndex(index);
if (parentIndex == null) break;

if(heap[parentIndex] > heap[index]){
swap(parentIndex, index);
index = parentIndex;
}else{
break;
}
}
}

// Remove the element to the first and copy the last element to the root.
// Shift down the root value by comparing with its child.
public Integer extractMin() {
if (size == 0) return null;

int minValue = heap[0];
// Check if heap has elements
if(size > 0) {
heap[0] = heap[size - 1];
heapifyDown(0);
}
size--;
return minValue;
}

private void heapifyDown(int index) {
int smallest = index;
while (true) {
Integer leftChildIndex = getLeftChild(index);
if (leftChildIndex < size && heap[leftChildIndex] < heap[smallest]) {
smallest = leftChildIndex;
}

Integer rightChildIndex = getRightChild(index);
if (rightChildIndex < size && heap[rightChildIndex] < heap[smallest]) {
smallest = rightChildIndex;
}
// If there is no child with lower value, stop
if (smallest == index) {
break;
}

// Otherwise, swap and update 'index' to continue the loop from the new position
swap(index, smallest);
index = smallest;
}
}

public Integer getMin() {
if (size == 0) return null;
return heap[0];
}

private Integer getParentIndex(Integer childIndex) {
// No parent for the root
if (childIndex == 0) return null;

return (childIndex % 2 == 0) ? (childIndex - 1) / 2 : childIndex / 2;
}

private void swap(int i, int j){
int temp = heap[i];
heap[i] = heap[j];
heap[j] = temp;
}

private Integer getLeftChild(Integer parentIndex) {
return 2 * parentIndex + 1;
}

private Integer getRightChild(Integer parentIndex) {
return 2 * parentIndex + 1;
}
}
1 change: 0 additions & 1 deletion Problem1.java

This file was deleted.

1 change: 0 additions & 1 deletion Problem2.java

This file was deleted.