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
33 changes: 33 additions & 0 deletions Problem1.java
Original file line number Diff line number Diff line change
@@ -1 +1,34 @@
// Time Complexity : O(log n)
// Space Complexity : O(1)
// Did this code successfully run on Leetcode : yes
// Any problem you faced while coding this : no

/*
* Approach
* We are using Binary search because we have been give a sorted array,
* number that the array can have are from 1 to n and only one number will be missing
* first we will handle the edge cases where first or the last element might be missing
* when int the while loop we are elemenating the half that that every elemnt and selecting the other half to fid the missing number
*
*/
class Solution {
public int findmissing(int[] nums) {
int low = 0;
int high = nums.length - 1;
if (nums[0] != 1) {
return 1;
}
if (nums[high] != high + 2) {
return high + 2;
}
while (low <= high) {
int mid = low + (high - low) / 2;
if ((nums[low] - low) != nums[mid] - mid) {
high = mid;
} else {
low = mid;
}
}
return nums[low] + 1;
}
}
90 changes: 90 additions & 0 deletions Problem2.java
Original file line number Diff line number Diff line change
@@ -1 +1,91 @@
// Time Complexity :
// Space Complexity :
// Did this code successfully run on Leetcode :
// Any problem you faced while coding this : i dont fully understand this, i have never implement heaps before so most of the code is taken from some where

class minHeap {
int[] heap;
int size;
int index;

public minHeap(int size) {
this.size = size;
this.index = 0;
this.heap = new int[size];
}

private int parent(int i) {
return (i - 1) / 2;
}

private int leftChild(int i) {
return (i * 2) + 1;
}

private int rightChild(int i) {
return (i * 2) + 2;
}

private boolean isLeaf(int i) {
if (rightChild(i) >= size || leftChild(i) >= size) {
return true;
}
return false;
}

private void swap(int x, int y) {
int temp = heap[x];
heap[x] = heap[y];
heap[y] = temp;

}

private void minHeapify(int i) {
if (!isLeaf(i)) {
if (heap[i] > heap[leftChild(i)] || heap[i] > heap[rightChild(i)]) {
swap(i, leftChild(i));
minHeapify(leftChild(i));
} else {
swap(i, rightChild(i));
minHeapify(rightChild(i));
}
}
}

public void minHeap() {
for (int i = (index - 1 / 2); i >= 1; i--) {
minHeapify(i);
}
}

public void insert(int num) {
if (index >= size) {
return;
}
heap[index] = num;
int current = index;
while (heap[current] < heap[parent(current)]) {
swap(current, parent(current));
current = parent(current);
}
index++;
}

public void printHeap() {
for (int i = 0; i < (index / 2); i++) {
System.out.print("Parent : " + heap[i]);
if (leftChild(i) < index)
System.out.print(" Left : " + heap[leftChild(i)]);
if (rightChild(i) < index)
System.out.print(" Right :" + heap[rightChild(i)]);
System.out.println();
}
}

public int remove() {
int popped = heap[0];
heap[0] = heap[--index];
minHeapify(0);
return popped;
}
}