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

done CompetitiveCoding1 #1151

Open
wants to merge 1 commit 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
43 changes: 43 additions & 0 deletions Problem1.java
Original file line number Diff line number Diff line change
@@ -1 +1,44 @@
// Time Complexity : O(logn) everytime we are reducing the search space by 1/2.
// Space Complexity : O(1) constant space complexity.
// Did this code successfully run on Leetcode : yes.
// Any problem you faced while coding this : no


// Your code here along with comments explaining your approach in three sentences only
import java.io.*;

public class FindMissingNumber {

public static int search(int[] nums) {
if (nums == null || nums.length == 0) {
return -1;
}

int n = nums.length;
int low = 0;
int high = n - 1;

// Use binary search
while (low < high) {
int mid = low + (high - low) / 2; // we find mid of array
int midIdx = nums[mid] - 1;
int lowIdx = nums[low] - 1;
int highIdx = nums[high] - 1;

if (midIdx != lowIdx) { // we chech whether mid value and low value are equal or not
high = mid-1;
} else if (midIdx != highIdx) { // we chech whether mid value and high value are equal or not
low = mid + 1;
}
}

return (nums[low]+nums[high])/2 // at last we get should add low value and high value divide by 2
}

public static void main(String[] args) {
int[] arr = new int[]{1, 2, 3, 4, 6, 7, 8};
System.out.println("Missing number: " + search(arr));
}
}


118 changes: 118 additions & 0 deletions Problem2.java
Original file line number Diff line number Diff line change
@@ -1 +1,119 @@
//Time Complexity: O(1)
//Space Complexity: O(n)
// Did this code successfully run on Leetcode : yes.
// Any problem you faced while coding this : no

public class MinHeap {

private int[] Heap;
private int size;
private int maxsize;


private static final int FRONT = 1;


public MinHeap(int maxsize) {
this.maxsize = maxsize;
this.size = 0;
Heap = new int[this.maxsize + 1];
Heap[0] = Integer.MIN_VALUE;
}

// Return the index of the parent of the node at index pos
private int parent(int pos) {
return pos / 2;
}

// Return the index of the left child of the node at index pos
private int leftChild(int pos) {
return (2 * pos);
}


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

private boolean isLeaf(int pos) {
return pos > (size / 2) && pos <= size;
}

private void swap(int fpos, int spos) {
int tmp = Heap[fpos];
Heap[fpos] = Heap[spos];
Heap[spos] = tmp;
}


private void minHeapify(int pos) {

while (!isLeaf(pos)) {
int left = leftChild(pos);
int right = rightChild(pos);
int smallest = pos;
if (left <= size && Heap[left] < Heap[smallest]) {
smallest = left;
}
if (right <= size && Heap[right] < Heap[smallest]) {
smallest = right;
}
if (smallest != pos) {
swap(pos, smallest);
pos = smallest;
} else {
break;
}
}
}

// Insert a new element into the heap
public void insert(int element) {
if (size >= maxsize) {
return;
}
Heap[++size] = element;
int current = size;

while (Heap[current] < Heap[parent(current)]) {
swap(current, parent(current));
current = parent(current);
}
}

// Print the heap
public void print() {
for (int i = 1; i <= size / 2; i++) {
System.out.println(" PARENT : " + Heap[i] + " LEFT CHILD : " +
Heap[2 * i] + " RIGHT CHILD :" +
Heap[2 * i + 1]);
}
}


public int remove() {
int popped = Heap[FRONT];
Heap[FRONT] = Heap[size];
Heap[size--] = 0;
minHeapify(FRONT);
return popped;
}


public static void main(String[] arg) {
System.out.println("The Min Heap is ");
MinHeap minHeap = new MinHeap(15);
minHeap.insert(5);
minHeap.insert(3);
minHeap.insert(17);
minHeap.insert(10);
minHeap.insert(84);
minHeap.insert(19);
minHeap.insert(6);
minHeap.insert(22);
minHeap.insert(9);
minHeap.print();
System.out.println("The Min val is " + minHeap.remove());
minHeap.print();
}
}