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
1 change: 0 additions & 1 deletion Problem1.cpp

This file was deleted.

34 changes: 34 additions & 0 deletions Problem1.java
Original file line number Diff line number Diff line change
@@ -1 +1,35 @@
// Time Complexity :O(logn)
// Space Complexity :O(1)
// Did this code successfully run on Leetcode :No
// Any problem you faced while coding this :No


// Your code here along with comments explaining your approach
//saw the diff between the index and val is constant or not. if not then the number is missing

import java.io.*;

class GFG {

static int search(int[] ar, int size) {
int low =0;
int high = size-1;

while (high-low >= 2){
int mid = low + (high-low)/2;
int midDiff = ar[mid]- mid;
int lowDiff = ar[low]-low;
int highDiff = ar[high]-high;

if (lowDiff == highDiff){
return -1;
}
if (midDiff != lowDiff){
high = mid;
}else if (midDiff != highDiff){
low = mid;
}
}
return (ar[low] + ar[high])/2;

}}
20 changes: 20 additions & 0 deletions Problem1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Time Complexity :O(logn)
# Space Complexity :O(1)
# Did this code successfully run on Leetcode :No
# Any problem you faced while coding this :No


# Your code here along with comments explaining your approach
#saw the diff between the index and val is constant or not. if not then the number is missing

def missingElement(nums):
low = 0
high = len(nums)-1
while high-low >= 2:
mid = (low + high)//2
if (mid - nums[mid] != low - nums[low]):
high = mid
else:
low = mid
return (nums[low] + nums[high])/2
print(missingElement([1,2,3,4,5,7,8]))
1 change: 0 additions & 1 deletion Problem2.cpp

This file was deleted.

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


// Your code here along with comments explaining your approach
//for remove min..i removed the first val, and then replaced the last with first and then heapify
//for insert..i inserted first then do at last and then check with parent and iterate
// for heapify..select an index and check with right and left child, then swap

class MyMinHeap {
private int[] heap;
private int size;
private int maxSize;

public MyMinHeap(int capacity) {
this.size = 0;
this.maxSize = capacity;
this.heap = new int[capacity];
}

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

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

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

public void swap(int i, int j){
int temp = heap[i];
heap[i] = heap[j];
heap[j] = temp;
}
public boolean isLeaf(int i){
return i >= size/2 && i < size;
}

public void insert(int val){
if (size >= maxSize){
return;
}
heap[size] = val;
int curr = size;
size += 1;
while (curr > 0){
if (heap[curr] < heap[parent(curr)]){
swap(curr,parent(curr));
curr = parent(curr);
}
}
}

public void heapify(int i){
if (size >= maxSize){
return;
}
if (isLeaf(i)){
return;
}
int smallest = i;
if (rightChild(i) < size && heap[smallest]> heap[rightChild(i)]){
smallest = rightChild(i);
}
if (leftChild(i) < size && heap[smallest]> heap[leftChild(i)]){
smallest = leftChild(i);
}
if (smallest != i){
swap(smallest,i);
heapify(smallest);
}

}

public int removeMin(){
if (size ==0){
return -1;
}
int min_val = heap[0];
heap[0] = heap[size-1];
size--;
heapify(0);
return min_val;
}




}
69 changes: 69 additions & 0 deletions Problem2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
# Time Complexity :insert - O(n), heapify-O(n), removemin-O(n)
# Space Complexity :O(n)
# Did this code successfully run on Leetcode :No
# Any problem you faced while coding this :No


# Your code here along with comments explaining your approach
#for remove min..i removed the first val, and then replaced the last with first and then heapify
#for insert..i inserted first then do at last and then check with parent and iterate
# for heapify..select an index and check with right and left child, then swap

class MyMinHeap:
def __init__(self, capacity):
self.maxSize = 0
self.capacity = capacity
self.heap = [] * capacity
def parent(self,i):
return (i-1/2)
def leftChild(self,i):
return (2*i + 1)
def rightChild(self,i):
return (2*i+2)
def swap(self,i,j):
self.heap[i], self.heap[j] = self.heap[j],self.heap[i]
def isleaf(self,i):
return i >= self.maxSize/2 and i < self.maxSize

def insert(self,val):
if self.maxSize >= self.capacity:
return
self.heap[self.maxSize] = val
self.maxSize += 1
curr = self.maxSize
heap_length = len(self.heap)
while heap_length >0:
if self.heap[curr] < self.parent(curr):
self.swap(val, self.parent(curr))
curr = self.parent(curr)
def heapify(self,i):
if self.maxSize >= self.capacity:
return
if self.isleaf(i):
return
curr = i
right = self.heap[self.rightChild(curr)]
left = self.heap[self.leftChild(curr)]
if self.heap[curr] > right:
curr = right
if self.heap[curr] > left:
curr = left
if i != curr:
self.swap(i,curr)
self.heapify(curr)
def removeMin(self):
if self.maxSize == 0:
return
min_val = self.heap[0]
self.heap[0] = self.heap[-1]
self.size -= 1
self.heapify(0)
return min_val