0% found this document useful (0 votes)
59 views8 pages

HEAP

The document contains code snippets for implementing a heap data structure and related algorithms like heap sort, finding the k largest/smallest elements, checking if an array represents a max heap, and finding the index of the kth largest element. The code uses concepts like priority queues, heap operations like down-heapify, and algorithms like heap sort.

Uploaded by

Anubhav
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
59 views8 pages

HEAP

The document contains code snippets for implementing a heap data structure and related algorithms like heap sort, finding the k largest/smallest elements, checking if an array represents a max heap, and finding the index of the kth largest element. The code uses concepts like priority queues, heap operations like down-heapify, and algorithms like heap sort.

Uploaded by

Anubhav
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

HEAP

public class Solution {

public static void downHeapify(int arr[],int i, int n){

int parentIndex=i;

int leftChildIndex=2*parentIndex+1;

int rightChildIndex=2*parentIndex+2;

while(leftChildIndex < n){

int minIndex=parentIndex;

if(arr[leftChildIndex] < arr[minIndex]){

minIndex=leftChildIndex;

if(rightChildIndex<n && arr[rightChildIndex] < arr[minIndex]){

minIndex=rightChildIndex;

if(minIndex==parentIndex){

return;

int temp=arr[parentIndex];

arr[parentIndex]=arr[minIndex];

arr[minIndex]=temp;

parentIndex=minIndex;

leftChildIndex=2*parentIndex+1;

rightChildIndex=2*parentIndex+2;
}

public static void inplaceHeapSort(int arr[]) {

/* Your class should be named Solution

* Don't write main().

* Don't read input, it is passed as function argument.

* Change in the given input itself.

* Taking input and printing output is handled automatically.

*/

int n=arr.length;

for(int i=(n/2)-1;i>=0;i--){

downHeapify(arr,i,n);

for(int i=n-1;i>=0;i--){

int temp=arr[i];

arr[i]=arr[0];

arr[0]=temp;

downHeapify(arr,0,i);

K- LARGEST

import java.util.PriorityQueue;

import java.util.ArrayList;

public class Solution {


public static ArrayList<Integer> kLargest(int input[], int k) {

ArrayList<Integer> ans=new ArrayList<>();

int n=input.length;

PriorityQueue<Integer> pq=new PriorityQueue<>();

for(int i=0;i<k;i++){

pq.add(input[i]);

for(int i=k;i<n;i++){

if(input[i] > pq.peek()){

pq.poll();

pq.add(input[i]);

while(!pq.isEmpty()){

ans.add(pq.peek());

pq.poll();

return ans;

/* Your class should be named Solution

* Don't write main().

* Don't read input, it is passed as function argument.

* Return output and don't print it.


* Taking input and printing output is handled automatically.

*/

K-SMALLEST

import java.util.ArrayList;

import java.util.PriorityQueue;

import java.util.*;

public class Solution {

public static ArrayList<Integer> kSmallest(int n, int[] input, int k) {

PriorityQueue<Integer> maxPQ = new PriorityQueue<>(Collections.reverseOrder());

ArrayList<Integer> ans=new ArrayList<>();

for(int i=0;i<k;i++){

maxPQ.add(input[i]);

for(int i=k;i<n;i++){

if(input[i] < maxPQ.peek()){

maxPQ.poll();

maxPQ.add(input[i]);

}
while(!maxPQ.isEmpty()){

ans.add(maxPQ.peek());

maxPQ.poll();

return ans;

// Write your code here

CHECK

import java.util.*;

public class Solution {

public static boolean checkMaxHeap(int arr[]) {

boolean ans=true;

PriorityQueue<Integer> maxPQ=new PriorityQueue<>(Collections.reverseOrder());

for(int i=0;i<arr.length;i++){

// int parentIndex=i;

int leftChild = 2 * i + 1;

int rightChild = 2 * i + 2;

if(leftChild < arr.length && arr[i] < arr[leftChild]){

ans=false;

}
if(rightChild < arr.length && arr[i] < arr[rightChild]){

ans=false;

return ans;

/*

* Your class should be named Solution Don't write main(). Don't read input, it

* is passed as function argument. Return output and don't print it. Taking

* input and printing output is handled automatically.

*/

K-th Largest

import java.util.*;

import java.util.PriorityQueue;

public class Solution {

public static int kthLargest(int n, int[] input, int k) {

int ans=0;

PriorityQueue<Integer> maxPQ = new PriorityQueue<>(Collections.reverseOrder());

for(int i=0;i<n;i++){

maxPQ.add(input[i]);

for(int i=1;i<=k;i++){
ans=maxPQ.poll();

return ans;

// Write your code here

BUY

import java.util.*;

public class Solution {

public static int buyTicket(int input[], int k) {

/* Your class should be named Solution

* Don't write main().

* Don't read input, it is passed as function argument.

* Return output and don't print it.

* Taking input and printing output is handled automatically.

*/

int count = 0;

Queue<Integer> queue = new LinkedList<>();

PriorityQueue<Integer> pq = new PriorityQueue<>(Collections.reverseOrder());

for (int i = 0 ; i < input.length ; i++){

queue.add(i);

pq.add(input[i]);

while (!queue.isEmpty()){
if (input[queue.peek()] < pq.peek()){

queue.add(queue.poll());

}else{

int temp = queue.poll();

pq.remove();

count++;

if (temp == k){

return count;

return count;

You might also like