Heap
Heap
Constraints:
1 <= N <= 106
1 <= x <= 106
Example:
Input:
4
5
15
1
3
Output:
5
10
5
4
CODE:
int main()
int n;
cin>>n;
int arr[n];
for(int i=0;i<n;i++)
cin>>arr[i];
priority_queue<int> l;
priority_queue<int,vector<int>,greater<int>> r;
l.push(arr[0]);
int med=arr[0];
cout<<med<<endl;
for(int i=1;i<n;i++)
int x=arr[i];
if(l.size()>r.size())
if(x<med)
r.push(l.top());
l.pop();
l.push(x);
}
else
r.push(x);
med=(r.top()+l.top())/2;
else if(l.size()<r.size())
if(x>med)
l.push(r.top());
r.pop();
r.push(x);
else
l.push(x);
med=(r.top()+l.top())/2;
else
if(x<med)
l.push(x);
med=l.top();
}
else
r.push(x);
med=r.top();
cout<<med<<endl;
return 0;
2. Heap Sort
Given an array of size N. The task is to sort the array elements by completing
functions heapify() and buildHeap() which are used to implement Heap Sort.
Example 1:
Input:
N = 5
arr[] = {4,1,3,9,7}
Output:1 3 4 7 9
Explanation:After sorting elements
using heap sort, elements will be
in order as 1,3,4,7,9.
Example 2:
Input:
N = 10
arr[] = {10,9,8,7,6,5,4,3,2,1}
Output:1 2 3 4 5 6 7 8 9 10
Explanation:After sorting elements
using heap sort, elements will be
in order as 1, 2,3,4,5,6,7,8,9,10.
Your Task :
Complete the functions heapify() and buildheap().
Expected Time Complexity: O(N * Log(N)).
Expected Auxiliary Space: O(1).
Constraints:
1 <= N <= 106
1 <= arr[i] <= 106
CODE:
#include <bits/stdc++.h>
buildHeap(arr, n);
swap(arr[0], arr[i]);
heapify(arr, i, 0);
int i;
printf("\n");
int main()
int arr[1000000],n,T,i;
scanf("%d",&T);
while(T--){
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%d",&arr[i]);
heapSort(arr, n);
printArray(arr, n);
return 0;
and heapify()
buildHeap(arr, n);
swap(arr[0], arr[i]);
heapify(arr, i, 0);
} */
int largest=i;
int l=2*i+1;
int r=2*i+2;
largest=l;
largest=r;
if(largest!=i)
swap(arr[i],arr[largest]);
heapify(arr,n,largest);
for(int i=n/2-1;i>=0;i--)
heapify(arr,n,i);
// Your Code Here
Input:
Q = 7
Queries:
insertKey(4)
insertKey(2)
extractMin()
insertKey(6)
deleteKey(0)
extractMin()
extractMin()
Output: 2 6 - 1
Explanation: In the first test case for
query
insertKey(4) the heap will have {4}
insertKey(2) the heap will be {2 4}
extractMin() removes min element from
heap ie 2 and prints it
now heap is {4}
insertKey(6) inserts 6 to heap now heap
is {4 6}
deleteKey(0) delete element at position 0
of the heap,now heap is {6}
extractMin() remove min element from heap
ie 6 and prints it now the
heap is empty
extractMin() since the heap is empty thus
no min element exist so -1
is printed.
Example 2:
Input:
Q = 5
Queries:
insertKey(8)
insertKey(9)
deleteKey(1)
extractMin()
extractMin()
Output: 8 -1
Your Task:
You are required to complete the 3 methods insertKey() which take one argument the value to
be inserted, deleteKey() which takes one argument the position from where the element is to
be deleted and extractMin() which returns the minimum element in the heap(-1 if the heap is
empty)
Expected Time Complexity: O(Q*Log(size of Heap) ).
Expected Auxiliary Space: O(1).
Constraints:
1 <= Q <= 104
1 <= x <= 104
CODE:
#include <bits/stdc++.h>
struct MinHeap {
int *harr;
int capacity;
int heap_size;
MinHeap(int c) {
heap_size = 0;
capacity = c;
harr = new int[c];
int extractMin();
};
// Driver code
int main() {
int t;
cin >> t;
while (t--) {
ll a;
cin >> a;
MinHeap h(a);
int c;
int n;
cin >> c;
if (c == 1) {
cin >> n;
h.insertKey(n);
if (c == 2) {
cin >> n;
h.deleteKey(n);
if (c == 3) {
// delete h.harr;
h.harr = NULL;
}
return 0;
struct MinHeap
int *harr;
int extractMin();
};*/
{ if(heap_size == 0)
return -1;
int a = harr[0];
swap(harr[0],harr[heap_size-1]);
heap_size--;
MinHeapify(0);
return a;
{ if(i<0 || i>=heap_size)
return;
decreaseKey(i,INT_MIN);
extractMin();
if(heap_size == capacity)
return;
harr[heap_size] = INT_MAX;
heap_size++;
decreaseKey(heap_size-1,k);
}
// Decrease Key operation, helps in deleting key from heap
harr[i] = new_val;
swap(harr[i], harr[parent(i)]);
i = parent(i);
void MinHeap::MinHeapify(int i) {
int l = left(i);
int r = right(i);
int smallest = i;
if (smallest != i) {
swap(harr[i], harr[smallest]);
MinHeapify(smallest);
}
4. Rearrange characters
Given a string S with repeated characters (only lowercase). The task is to rearrange characters
in a string such that no two adjacent characters are same.
Note : It may be assumed that the string has only lowercase English alphabets.
Input:
The first line of input contains an integer T denoting the number of test cases. Then T test
cases follow. Each test case contains a single line containing a string of lowercase english
alphabets.
Output:
For each test case in a new line print "1" (without quotes) if the generated string doesn't
contains any same adjacent characters, else if no such string is possible to be made print "0"
(without quotes).
Constraints:
1 <= T <= 100
1 <= length of string <= 104
Example:
Input:
3
geeksforgeeks
bbbabaaacd
bbbbb
Output:
1
1
0
CODE:
int main()
{
int t;
cin>>t;
while(t--)
string s;
cin>>s;
map<char,int> mp;
for(int i=0;s[i]!='\0';i++)
mp[s[i]]++;
vector<int> v;
int sum=0;
while(it!=mp.end())
v.push_back(it->second);
sum+=it->second;
it++;
}
sort(v.begin(),v.end());
//debug
// for(int i=0;i<v.size();i++)
// {
// cout<<v[i]<<" ";
// }
// cout<<endl;
if(v.size()<=1)
cout<<0<<endl;
continue;
else
sum=sum-2*v[v.size()-1];
if(sum>=-1)
cout<<1<<endl;
else
cout<<0<<endl;
}
return 0;
Given an input stream of n integers, find the kth largest element for each element in the stream.
Input:
The first line of input contains an integer T denoting the number of test cases. Then T test
cases follow. Each test case contains two lines. The first line of each test case contains two
space separated integers k and n . Then in the next line are n space separated values of the
array.
Output:
For each test case, in a new line, print the space separated values denoting the kth largest
element at each insertion, if the kth largest element at a particular insertion in the stream
doesn't exist print -1.
Constraints:
1 <= T <= 100
1 <= K <= n
1 <= n <= 106
1 <= stream[] <= 105
Example:
Input:
2
46
123456
12
34
Output:
-1 -1 -1 1 2 3
34
CODE:
int t;
cin>>t;
while(t--)
int k,n;
cin>>k>>n;
if(k==1)
int maxn=-1;
for(int i=0;i<n;i++)
int x;
cin>>x;
maxn=max(maxn,x);
cout<<maxn<<" ";
cout<<endl;
}
else
int arr[n];
for(int i=0;i<n;i++)
cin>>arr[i];
priority_queue<int> l;
priority_queue<int,vector<int>,greater<int>> r;
for(int i=0;i<n;i++)
if(i+1<k)
cout<<"-1"<<" ";
r.push(arr[i]);
else
int x=arr[i];
if(x>r.top())
l.push(r.top());
r.pop();
r.push(x);
else
l.push(x);
cout<<l.top()<<" ";
cout<<endl;
return 0;
Input:
K = 4
value = {{1,2,3},{4 5},{5 6},{7,8}}
Output: 1 2 3 4 5 5 6 7 8
Explanation:
The test case has 4 sorted linked
list of size 3, 2, 2, 2
1st list 1 -> 2-> 3
2nd list 4->5
3rd list 5->6
4th list 7->8
The merged list will be
1->2->3->4->5->5->6->7->8.
Example 2:
Input:
K = 3
value = {{1,3},{4,5,6},{8}}
Output: 1 3 4 5 6 8
Explanation:
The test case has 3 sorted linked
list of size 2, 3, 1.
1st list 1 -> 3
2nd list 4 -> 5 -> 6
3rd list 8
The merged list will be
1->3->4->5->6->8.
Your Task:
The task is to complete the function mergeKList() which merges the K given lists into a sorted
one. The printing is done automatically by the driver code.
Expected Time Complexity: O(nk Logk)
Expected Auxiliary Space: O(k)
Note: n is the maximum size of all the k link list
Constraints
1 <= K <= 103
CODE:
// C++ program to merge k sorted arrays of size n each
#include <bits/stdc++.h>
struct Node
int data;
Node* next;
Node(int x){
data = x;
next = NULL;
};
node = node->next;
cout<<endl;
int main()
int t;
cin>>t;
while(t--)
int N;
cin>>N;
for(int j=0;j<N;j++)
int n;
cin>>n;
int x;
cin>>x;
arr[j]=new Node(x);
for(int i=0;i<n;i++)
cin>>x;
curr->next =temp;
curr=temp;
printList(res);
return 0;
struct Node
int data;
Node* next;
Node(int x){
data = x;
next = NULL;
};
*/
struct comparator
if(a->data>b->data)
return true;
return false;
};
priority_queue<Node*,vector<Node*>,comparator> pq;
for(int i=0;i<n;i++)
pq.push(arr[i]);
Node* head=NULL;
Node* last=NULL;
while(!pq.empty())
Node* top=pq.top();
pq.pop();
if(top->next!=NULL)
pq.push(top->next);
if(head==NULL)
head=top;
last=top;
else
last->next=top;
last=top;
}
return head;