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

Competitive Coding-1 Comp #1071

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
8 changes: 8 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.

1 change: 0 additions & 1 deletion Problem1.cpp

This file was deleted.

22 changes: 22 additions & 0 deletions Problem1.java
Original file line number Diff line number Diff line change
@@ -1 +1,23 @@
class Problem1 {
public static int missingNumber(int[] nums) {
int n = nums.length;
int low = 0, high =n-1;

while (low < high) {
int mid = low + (high - low) / 2;
if (nums[mid] != mid+1) {
high = mid ;
} else {
low = mid+1;
}

}
return low+1;
}

public static void main(String[] args) {
int[] arr = {1, 2, 3, 4, 6, 7, 8};
int res = missingNumber(arr);
System.out.println(res);
}
}
1 change: 0 additions & 1 deletion Problem2.cpp

This file was deleted.

130 changes: 130 additions & 0 deletions Problem2.java
Original file line number Diff line number Diff line change
@@ -1 +1,131 @@


class Problem2 {

private int[] heap;
private int size;
private int maxSize;


private static final int FRONT=1;

public Problem2(int maxSize)
{
this.maxSize=maxSize;
this.size=0;

heap= new int[maxSize+1];
heap[0]= Integer.MIN_VALUE;
}
private int parent(int pos)
{
return pos/2;
}
private int leftChild(int pos)
{
return (2*pos);
}
private int rightChild(int pos)
{
return (2*pos+1);
}
private boolean isLeaf(int pos){
if(pos>(size/2))
{
return true;
}
return false;
}
private void swap(int fpos, int spos)
{
int temp=heap[fpos];;
heap[fpos]=heap[spos];
heap[spos]=temp;
}
private void minHeapify(int pos)
{
if(!isLeaf(pos))
{
int swapPos= pos;
if (rightChild(pos) <=size)
{
swapPos= heap[leftChild(pos)] < heap[rightChild(pos)] ? leftChild(pos) : rightChild(pos);

}
else {
swapPos= leftChild(pos);
}
if (heap[pos] > heap[leftChild(pos) ] || heap[pos] > heap[rightChild(pos)])
{
swap(swapPos, pos);
minHeapify(swapPos);
}
}
}
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);
}
}
public void print(){
for (int i=1;i<=size;i++)
{
System.out.print("Parent :" +heap[i]+" Left Child : "+ heap[2*i]
+ "Right child: "+ heap[2*i+1]);
System.out.println();
}
}
public int remove(){
int popped= heap[FRONT];
heap[FRONT]=heap[size--];
minHeapify(FRONT);

return popped;
}
public static void main(String[] arg)
{

// Display message
System.out.println("The Min Heap is ");

// Creating object of class in main() method
Problem2 minHeap = new Problem2(15);

// Inserting element to minHeap
// using insert() method

// Custom input entries
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);

// Print all elements of the heap
minHeap.print();

// Removing minimum value from above heap
// and printing it
System.out.println("The Min val is "
+ minHeap.remove());
}

// public static void main(String[] args) {
// int[] arr = {1, 2, 3, 4, 6, 7, 8};
// int res = missingNumber(arr);
// System.out.println(res);
// }
}