diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 00000000..26d33521 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,3 @@ +# Default ignored files +/shelf/ +/workspace.xml diff --git a/.idea/Competitive-Coding-1.iml b/.idea/Competitive-Coding-1.iml new file mode 100644 index 00000000..b107a2dd --- /dev/null +++ b/.idea/Competitive-Coding-1.iml @@ -0,0 +1,11 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 00000000..72991755 --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 00000000..4db1dff7 --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 00000000..35eb1ddf --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/MissingNumInSortedArray.java b/MissingNumInSortedArray.java new file mode 100644 index 00000000..ad8d3a0b --- /dev/null +++ b/MissingNumInSortedArray.java @@ -0,0 +1,61 @@ +/** + * + * Find the missing number in a sorted array. + * Given a list of n-1 integers, these integers are in the range + * of 1 to n. + * There are no duplicates in the list. + * One of the integers is missing in the list. Write efficient code to find the missing integer. + * + */ +public class MissingNumInSortedArray { + public int missingNumber(int[] nums) { + + if (nums == null || nums.length == 0) { + return -1; + } + + int low = 0; + int high = nums.length - 1; + + if (nums[low] != 1) { + return 1; + } + + if (nums[high] != nums.length + 1) { + return nums.length + 1; + } + + while (low < high) { + int mid = low + (high - low) / 2; + if (nums[mid] - mid != nums[low] - low) { + high = mid; + } else { + low = mid + 1; + } + } + + return nums[low] - 1; + } + + public static void main(String[] args) { + int[] nums1 = {2, 3, 4, 5};//Missing smallest number + int[] nums2 = {1, 2, 3, 4};//Missing largest number + int[] nums3 = {1, 2, 4, 5};//Missing a number in the middle + int[] nums4 = {2};//Single-element array with missing 1 + int[] nums5 = {1};//Single-element array with missing nums.length + 1: + int[] nums6 = {1, 2, 3, 5, 6, 7, 8};//Larger array with a missing number in the middle + int[] nums7 = {};//Empty array + int[] nums8 = null;//Null input + int[] nums9 = {1, 2, 3, 4, 5};//no missing number + MissingNumInSortedArray missingNumber = new MissingNumInSortedArray(); + System.out.println("Missing smallest number: " + missingNumber.missingNumber(nums1)); + System.out.println("Missing largest number: " + missingNumber.missingNumber(nums2)); + System.out.println("Missing a number in the middle: " + missingNumber.missingNumber(nums3)); + System.out.println("Single-element array with missing 1: " + missingNumber.missingNumber(nums4)); + System.out.println("Single-element array with missing nums.length + 1: " + missingNumber.missingNumber(nums5)); + System.out.println("Larger array with a missing number in the middle: " + missingNumber.missingNumber(nums6)); + System.out.println("Empty array: " + missingNumber.missingNumber(nums7)); + System.out.println("Null input: " + missingNumber.missingNumber(nums8)); + System.out.println("no missing number: " + missingNumber.missingNumber(nums9)); + } +} \ No newline at end of file diff --git a/MyMinHeap.java b/MyMinHeap.java new file mode 100644 index 00000000..22fc1811 --- /dev/null +++ b/MyMinHeap.java @@ -0,0 +1,109 @@ +import java.util.Arrays; + +/** + * if a node is at ith index then in an array starting at index 0 + * left-child is at (2*i)+1 + * right-child is at (2*i)+2 + * Parent would be at |(i-1)/2| + * if a node is at ith index then in an array starting at index 0 + * left-child is at (2*i) + * right-child is at (2*i)+1 + * Parent would be at |i/2| + * Max-Heap Min-Heap + */ +public class MyMinHeap { + private int[] heap; + private int size; + + public MyMinHeap(int capacity) { + heap = new int[capacity]; + size = 0; + } + public int getMin(){ + if (size == 0) { + throw new IllegalStateException("Heap is empty"); + } + return heap[0]; + } + + //remove min + public int extractMin(){ + if (size == 0) { + throw new IllegalStateException("Heap is empty"); + } + + int min = heap[0]; + heap[0] = heap[size - 1]; + size--; + extractHeapify(0); + return min; + } + + private void extractHeapify(int index) { + int leftChildIndex = 2 * index + 1; + int rightChildIndex = 2 * index + 2; + int smallestIndex = index; + + if (leftChildIndex < size && heap[leftChildIndex] < heap[smallestIndex]) { + smallestIndex = leftChildIndex; + } + + if (rightChildIndex < size && heap[rightChildIndex] < heap[smallestIndex]) { + smallestIndex = rightChildIndex; + } + + if (smallestIndex != index) { + swap(index, smallestIndex); + extractHeapify(smallestIndex); + } + } + + public void insert(int val){ + if (size == heap.length) { + heap = Arrays.copyOf(heap, heap.length * 2); + } + heap[size] = val; + insertHeapify(size); + size++; + } + + public int size() { + return size; + } + + private void insertHeapify(int index) { + int parentIndex = (index - 1) / 2; + + while (index > 0 && heap[index] < heap[parentIndex]) { + swap(index, parentIndex); + index = parentIndex; + parentIndex = (index - 1) / 2; + } + } + + public void swap(int i, int j){ + int temp = heap[i]; + heap[i] = heap[j]; + heap[j] = temp; + } + + + public static void main(String[] args){ + MyMinHeap minHeap=new MyMinHeap(3); + System.out.println(minHeap.size()); + minHeap.insert(4); + minHeap.insert(6); + minHeap.insert(2); + minHeap.insert(3); + minHeap.insert(1); + System.out.println(minHeap.size()); + System.out.println(minHeap.getMin()); + System.out.println(minHeap.getMin()); + System.out.println(minHeap.extractMin()); + System.out.println(minHeap.size()); + System.out.println(minHeap.getMin()); + minHeap.insert(-6); + System.out.println(minHeap.getMin()); + } + +} diff --git a/Problem1.cpp b/Problem1.cpp deleted file mode 100644 index 8b137891..00000000 --- a/Problem1.cpp +++ /dev/null @@ -1 +0,0 @@ - diff --git a/Problem1.java b/Problem1.java deleted file mode 100644 index 8b137891..00000000 --- a/Problem1.java +++ /dev/null @@ -1 +0,0 @@ - diff --git a/Problem2.cpp b/Problem2.cpp deleted file mode 100644 index 8b137891..00000000 --- a/Problem2.cpp +++ /dev/null @@ -1 +0,0 @@ - diff --git a/Problem2.java b/Problem2.java deleted file mode 100644 index 8b137891..00000000 --- a/Problem2.java +++ /dev/null @@ -1 +0,0 @@ -