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
32 changes: 32 additions & 0 deletions Problem1.java
Original file line number Diff line number Diff line change
@@ -1 +1,33 @@
//Find Missing Number in a sorted array
//We are taking the approach as the sorted array value would be arr[i]=i+1
//Time complexity would be O(logn) and space would be O(1)

public class Problem1 {
//Search for 3 here
public static int binarySearch(int arr[]) {
int n= arr.length;
int low=0;
int high=n-1;
while(low<=high) { //initiated binary search

int mid= low +(high-low)/2;
//right sorting
if(arr[mid]==mid+1) {
low=mid+1;
}
//left sorting
else {
high=mid-1;
}
}
//When the loop ends, low points to the index where the mismatch starts.
return low+1;
}

public static void main(String[] args) {
// TODO Auto-generated method stub
int arr[] = {1,2,4,5,6};
int result = binarySearch(arr);
System.out.print(result);
}
}
117 changes: 117 additions & 0 deletions Problem2.java
Original file line number Diff line number Diff line change
@@ -1 +1,118 @@
class MyMinHeap {
private int[] heap;
private int size;
private int maxSize;

// Constructor to initialize the heap with a given capacity
public MyMinHeap(int capacity) {
this.maxSize = capacity;
this.size = 0;
heap = new int[capacity]; // Allocate space for the heap
}

// Returns the index of the parent of the node at index i
private int parent(int i) {
return (i - 1) / 2;
}

// Returns the index of the left child of the node at index i
private int leftChild(int i) {
return 2 * i + 1;
}

// Returns the index of the right child of the node at index i
private int rightChild(int i) {
return 2 * i + 2;
}

// Checks if the node at index i is a leaf node
private boolean isLeaf(int i) {
return i >= size / 2 && i < size;
}

// Swaps two elements in the heap array
private void swap(int i, int j) {
int temp = heap[i];
heap[i] = heap[j];
heap[j] = temp;
}

// Maintains the min-heap property by pushing down the element at index i
private void heapify(int i) {
if (isLeaf(i)) return; // No need to heapify leaf nodes

int left = leftChild(i);
int right = rightChild(i);
int smallest = i;

// Find the smallest among current, left, and right
if (left < size && heap[left] < heap[smallest]) {
smallest = left;
}
if (right < size && heap[right] < heap[smallest]) {
smallest = right;
}

// If the smallest is not the current node, swap and continue heapifying
if (smallest != i) {
swap(i, smallest);
heapify(smallest);
}
}

public void insert(int val) {
if (size >= maxSize) return; // Heap is full

heap[size] = val; // Place the new value at the end
int current = size;
size++;

// Bubble up the new value to maintain heap property
while (current > 0 && heap[current] < heap[parent(current)]) {
swap(current, parent(current));
current = parent(current);
}
}

public int removeMin() {
if (size == 0) return -1; // Heap is empty

int min = heap[0];
heap[0] = heap[size - 1]; // Replace root with the last element
size--; // Reduce heap size
heapify(0); // Restore heap property from the root
return min;
}

public void printHeap() {
for (int i = 0; i <= (size - 2) / 2; i++) {
System.out.print("PARENT: " + heap[i]);
if (leftChild(i) < size)
System.out.print(" LEFT: " + heap[leftChild(i)]);
if (rightChild(i) < size)
System.out.print(" RIGHT: " + heap[rightChild(i)]);
System.out.println();
}
}

public static void main(String[] args) {
MyMinHeap heap = new MyMinHeap(15);

heap.insert(5);
heap.insert(3);
heap.insert(17);
heap.insert(10);
heap.insert(84);
heap.insert(19);
heap.insert(6);
heap.insert(22);
heap.insert(9);

// Display the heap structure
System.out.println("Min Heap:");
heap.printHeap();

// Remove and print the minimum element
System.out.println("Removed Min: " + heap.removeMin());
}
}