Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CompetativeCoding-1 #1132

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
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
3 changes: 3 additions & 0 deletions .idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions .idea/Competitive-Coding-1.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

61 changes: 61 additions & 0 deletions MissingNumInSortedArray.java
Original file line number Diff line number Diff line change
@@ -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));
}
}
109 changes: 109 additions & 0 deletions MyMinHeap.java
Original file line number Diff line number Diff line change
@@ -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());
}

}
1 change: 0 additions & 1 deletion Problem1.cpp

This file was deleted.

1 change: 0 additions & 1 deletion Problem1.java

This file was deleted.

1 change: 0 additions & 1 deletion Problem2.cpp

This file was deleted.

1 change: 0 additions & 1 deletion Problem2.java

This file was deleted.