Ques - Alex - - - - - - - - - - - - - - - - - - - - - - - ?
#include <iostream>
using namespace std;
struct Bid {
int bid_amount;
};
void swap(Bid& a, Bid& b) {
Bid temp = a;
a = b;
b = temp;
}
void insertBid(Bid heap[], int& heapSize, Bid newBid) {
if (heapSize == 0) {
heap[0] = newBid;
} else {
int current = heapSize;
heap[heapSize] = newBid;
while (current > 0 && heap[current].bid_amount > heap[(current - 1) / 2].bid_amount) {
swap(heap[current], heap[(current - 1) / 2]);
current = (current - 1) / 2;
}
}
heapSize++;
}
int main() {
int maxSize = 100;
Bid binaryHeap[maxSize];
int heapSize = 0;
while (true) {
Bid newBid;
if (!(cin >> newBid.bid_amount)) {
break;
}
if (heapSize < maxSize) {
insertBid(binaryHeap, heapSize, newBid);
} else {
cout << "Heap is full. Cannot insert more bids." << endl;
}
}
// Print the final state of the heap
for (int i = 0; i < heapSize; i++) {
cout << binaryHeap[i].bid_amount;
if (i < heapSize - 1) {
cout << ' ';
}
}
cout << endl;
return 0;
}
Ques - David- - - - - - - - - - - - - - - - - - - - - - - ?
#include <iostream>
using namespace std;
struct Location {
int distance;
};
void swap(Location& a, Location& b) {
Location temp = a;
a = b;
b = temp;
}
void insertLocation(Location heap[], int& heapSize, Location newLocation) {
if (heapSize == 0) {
heap[0] = newLocation;
} else {
int current = heapSize;
heap[heapSize] = newLocation;
while (current > 0 && heap[current].distance < heap[(current - 1) / 2].distance) {
swap(heap[current], heap[(current - 1) / 2]);
current = (current - 1) / 2;
}
}
heapSize++;
}
int main() {
Location minHeap[100];
int heapSize = 0;
while (true) {
Location newLocation;
if (!(cin >> newLocation.distance)) {
break;
}
if (heapSize < 100) {
insertLocation(minHeap, heapSize, newLocation);
} else {
cout << "Heap is full. Cannot insert more locations." << endl;
}
}
// Print the distances of delivery locations stored in the min-heap
for (int i = 0; i < heapSize; i++) {
cout << minHeap[i].distance;
if (i < heapSize - 1) {
cout << ' ';
}
}
cout << endl;
return 0;
}
Ques - Developer_in_IT- - - - - - - - - - - - - - - - - - - - - - - ?
#include <iostream>
using namespace std;
void heapify(int arr[], int n, int i) {
int largest = i;
int left = 2 * i + 1;
int right = 2 * i + 2;
if (left < n && arr[left] > arr[largest])
largest = left;
if (right < n && arr[right] > arr[largest])
largest = right;
if (largest != i) {
swap(arr[i], arr[largest]);
heapify(arr, n, largest);
}
}
void buildMaxHeap(int arr[], int n) {
for (int i = n / 2 - 1; i >= 0; i--)
heapify(arr, n, i);
}
int main() {
int n;
cin >> n;
int arr[n];
for (int i = 0; i < n; i++) {
cin >> arr[i];
}
buildMaxHeap(arr, n);
for (int i = 0; i < n; i++) {
cout << arr[i] << " ";
}
int sum = arr[0] + arr[n - 1];
cout << "\n" << sum << endl;
return 0;
}
Ques - Ethan- - - - - - - - - - - - - - - - - - - - - - - ?
#include <iostream>
#include <vector>
using namespace std;
const int maxHeapSize = 100;
void maxHeapify(vector<int> &maxHeap, int i) {
int largest = i;
int left = 2 * i + 1;
int right = 2 * i + 2;
if (left < maxHeap.size() && maxHeap[left] > maxHeap[largest])
largest = left;
if (right < maxHeap.size() && maxHeap[right] > maxHeap[largest])
largest = right;
if (largest != i) {
swap(maxHeap[i], maxHeap[largest]);
maxHeapify(maxHeap, largest);
}
}
void insertIntoMaxHeap(vector<int> &maxHeap, int task) {
maxHeap.push_back(task);
int currentIdx = maxHeap.size() - 1;
while (currentIdx > 0 && maxHeap[(currentIdx - 1) / 2] < maxHeap[currentIdx]) {
swap(maxHeap[currentIdx], maxHeap[(currentIdx - 1) / 2]);
currentIdx = (currentIdx - 1) / 2;
}
}
int main() {
vector<int> maxHeap;
int task;
while (cin >> task) {
if (maxHeap.size() < maxHeapSize) {
insertIntoMaxHeap(maxHeap, task);
}
}
for (int i = 0; i < maxHeap.size(); i++) {
cout << maxHeap[i] << " ";
}
return 0;
}
Ques - Jacob- - - - - - - - - - - - - - - - - - - - - - - ?
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
const int maxHeapSize = 100;
void minHeapify(vector<int>& minHeap, int i) {
int smallest = i;
int left = 2 * i + 1;
int right = 2 * i + 2;
if (left < minHeap.size() && minHeap[left] < minHeap[smallest])
smallest = left;
if (right < minHeap.size() && minHeap[right] < minHeap[smallest])
smallest = right;
if (smallest != i) {
swap(minHeap[i], minHeap[smallest]);
minHeapify(minHeap, smallest);
}
}
void insertIntoMinHeap(vector<int>& minHeap, int requestTime) {
minHeap.push_back(requestTime);
int currentIdx = minHeap.size() - 1;
while (currentIdx > 0 && minHeap[(currentIdx - 1) / 2] > minHeap[currentIdx]) {
swap(minHeap[currentIdx], minHeap[(currentIdx - 1) / 2]);
currentIdx = (currentIdx - 1) / 2;
}
}
int main() {
vector<int> minHeap;
int requestTime;
while (cin >> requestTime) {
if (minHeap.size() < maxHeapSize) {
insertIntoMinHeap(minHeap, requestTime);
}
}
for (int i = 0; i < minHeap.size(); i++) {
cout << minHeap[i] << " ";
}
return 0;
}
Ques - John- - - - - - - - - - - - - - - - - - - - - - - ?
#include <iostream>
using namespace std;
struct Book {
int popularity;
};
void swap(Book& a, Book& b) {
Book temp = a;
a = b;
b = temp;
}
// Function to insert a new book into the max heap
void insertBook(Book heap[], int& heapSize, Book newBook) {
heap[heapSize] = newBook;
int i = heapSize;
while (i > 0 && heap[(i - 1) / 2].popularity < heap[i].popularity) {
swap(heap[i], heap[(i - 1) / 2]);
i = (i - 1) / 2;
}
heapSize++;
}
void printHeap(Book heap[], int heapSize) { // Removed the 'const' keyword
for (int i = 0; i < heapSize; i++) {
cout << heap[i].popularity << ' ';
}
cout << endl;
}
int main() {
Book maxHeap[100];
int heapSize = 0;
while (true) {
Book newBook;
if (!(cin >> newBook.popularity)) {
break;
}
if (heapSize < 100) {
insertBook(maxHeap, heapSize, newBook);
} else {
cout << "Heap is full. Cannot insert more books." << endl;
}
}
printHeap(maxHeap, heapSize);
return 0;
}
Ques - Michael- - - - - - - - - - - - - - - - - - - - - - - ?
#include <iostream>
using namespace std;
struct Location {
int distance;
};
void swap(Location& a, Location& b) {
Location temp = a;
a = b;
b = temp;
}
void insertLocation(Location heap[], int& heapSize, Location newLocation) {
if (heapSize == 0) {
heap[0] = newLocation;
} else {
int current = heapSize;
heap[heapSize] = newLocation;
while (current > 0 && heap[current].distance < heap[(current - 1) / 2].distance) {
swap(heap[current], heap[(current - 1) / 2]);
current = (current - 1) / 2;
}
}
heapSize++;
}
int main() {
int maxSize = 100;
Location binaryHeap[maxSize];
int heapSize = 0;
while (true) {
Location newLocation;
if (!(cin >> newLocation.distance)) {
break;
}
if (heapSize < maxSize) {
insertLocation(binaryHeap, heapSize, newLocation);
} else {
cout << "Heap is full. Cannot insert more locations." << endl;
}
}
// Print the final state of the binary min-heap
for (int i = 0; i < heapSize; i++) {
cout << binaryHeap[i].distance;
if (i < heapSize - 1) {
cout << ' ';
}
}
cout << endl;
return 0;
}
Ques - Online_gaming_platform- - - - - - - - - - - - - - - - - - - - - - - ?
Ques - Sarah- - - - - - - - - - - - - - - - - - - - - - - ?
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
const int maxHeapSize = 100;
void minHeapify(vector<int>& minHeap, int i) {
int smallest = i;
int left = 2 * i + 1;
int right = 2 * i + 2;
if (left < minHeap.size() && minHeap[left] < minHeap[smallest])
smallest = left;
if (right < minHeap.size() && minHeap[right] < minHeap[smallest])
smallest = right;
if (smallest != i) {
swap(minHeap[i], minHeap[smallest]);
minHeapify(minHeap, smallest);
}
}
void insertIntoMinHeap(vector<int>& minHeap, int requestTime) {
minHeap.push_back(requestTime);
int currentIdx = minHeap.size() - 1;
while (currentIdx > 0 && minHeap[(currentIdx - 1) / 2] > minHeap[currentIdx]) {
swap(minHeap[currentIdx], minHeap[(currentIdx - 1) / 2]);
currentIdx = (currentIdx - 1) / 2;
}
}
int main() {
vector<int> minHeap;
int requestTime;
while (cin >> requestTime) {
if (minHeap.size() < maxHeapSize) {
insertIntoMinHeap(minHeap, requestTime);
}
}
for (int i = 0; i < minHeap.size(); i++) {
cout << minHeap[i] << " ";
}
return 0;
}
Ques - T20_cricket- - - - - - - - - - - - - - - - - - - - - - - ?
#include <iostream>
using namespace std;
void max_heap(int *a, int m, int n){
int j, t;
t= a[m];
j = 2 * m;
while (j <= n) {
if (j < n && a[j+1] > a[j])
j = j + 1;
if (t > a[j])
break;
else if (t <= a[j]) {
a[j/2] = a[j];
j = 2 * j;
}
}
a[j/2] = t;
return;
}
void build_maxheap(int *a, int n) {
int k;
for(k = n/2; k >= 1; k--) {
max_heap(a,k,n);
}
}
int main() {
int n, i;
cin>>n;
int a[n];
for (i = 1; i <= n; i++) {
cin>>a[i];
}
build_maxheap(a, n);
for (i = 1; i <= n; i++) {
cout<<a[i]<<" ";
}
}
Ques - The_treasure_hunt- - - - - - - - - - - - - - - - - - - - - - - ?
#include <iostream>
#include <vector>
#include <algorithm>
int main() {
int n;
std::cin >> n;
std::vector<int> treasures(n);
for (int i = 0; i < n; i++) {
std::cin >> treasures[i];
}
// Convert the vector into a min-heap
std::make_heap(treasures.begin(), treasures.end(), std::greater<int>());
// Print the min-heap
for (int i = 0; i < n; i++) {
std::cout << treasures[i];
if (i < n - 1) {
std::cout << ' ';
}
}
return 0;
}
Ques - Archeologist- - - - - - - - - - - - - - - - - - - - - - - ?
#include <iostream>
using namespace std;
// to heapify a subtree with root at given index
void MaxHeapify(int arr[], int i, int N)
{
int l = 2 * i + 1;
int r = 2 * i + 2;
int largest = i;
if (l < N && arr[l] > arr[i])
largest = l;
if (r < N && arr[r] > arr[largest])
largest = r;
if (largest != i) {
swap(arr[i], arr[largest]);
MaxHeapify(arr, largest, N);
}
}
// This function basically builds max heap
void convertMaxHeap(int arr[], int N)
{
// Start from bottommost and rightmost internal node and heapify all internal nodes in bottom up way
for (int i = (N - 2) / 2; i >= 0; --i)
MaxHeapify(arr, i, N);
}
void printArray(int* arr, int size)
{
for (int i = 0; i < size; ++i)
cout << arr[i] << " ";
cout<<endl;
}
int main()
{
int arrayCount;
cin>>arrayCount;
int arr[arrayCount];
for (int i=0;i<arrayCount;i++){
cin>>arr[i];
}
convertMaxHeap(arr, arrayCount);
printArray(arr, arrayCount);
return 0;
}
Ques - Ava- - - - - - - - - - - - - - - - - - - - - - - ?
#include <iostream>
using namespace std;
struct Job {
int processing_time;
};
void swap(Job& a, Job& b) {
Job temp = a;
a = b;
b = temp;
}
// Function to insert a new job into the binary heap based on processing time
void insertJob(Job heap[], int& heapSize, Job newJob) {
heapSize++;
int i = heapSize - 1;
heap[i] = newJob;
while (i > 0 && heap[(i - 1) / 2].processing_time < heap[i].processing_time) {
swap(heap[i], heap[(i - 1) / 2]);
i = (i - 1) / 2;
}
}
void printHeap(Job heap[], int heapSize) {
for (int i = 0; i < heapSize; i++) {
cout << heap[i].processing_time << " ";
}
cout << endl;
}
int main() {
int maxSize = 100;
Job jobHeap[maxSize];
int heapSize = 0;
while (true) {
Job newJob;
if (!(cin >> newJob.processing_time)) {
break;
}
insertJob(jobHeap, heapSize, newJob);
}
printHeap(jobHeap, heapSize);
return 0;
}
Ques - Basketball- - - - - - - - - - - - - - - - - - - - - - - ?
#include <iostream>
using namespace std;
int min_heap(int a[],int n)
{
for(int i=0;i<(n-1)/2;i++)
{
if((a[i]>a[(i*2)+1]) || a[i]>a[(i*2)+2])
{
return 0;
}
}
return 1;
}
int main() {
int n;
cin>>n;
int keys[n];
for (int i = 0; i < n; i++) {
cin>>keys[i];
}
if(min_heap(keys, n)==1)
{
cout<<"The players are arranged in min heap order";
}
else
{
cout<<"The players are not arranged in min heap order";
}
}
Ques - Caleb- - - - - - - - - - - - - - - - - - - - - - - ?
#include <iostream>
using namespace std;
struct Process {
int executionTime;
};
void swap(Process& a, Process& b) {
Process temp = a;
a = b;
b = temp;
}
// Function to insert a new process into the min heap
void insertProcess(Process heap[], int& heapSize, Process newProcess) {
heapSize++;
int i = heapSize - 1;
heap[i] = newProcess;
while (i > 0 && heap[(i - 1) / 2].executionTime > heap[i].executionTime) {
swap(heap[i], heap[(i - 1) / 2]);
i = (i - 1) / 2;
}
}
void printHeap(Process heap[], int heapSize) {
for (int i = 0; i < heapSize; i++) {
cout << heap[i].executionTime << " ";
}
cout << endl;
}
int main() {
Process minHeap[100];
int heapSize = 0;
while (true) {
Process newProcess;
if (!(cin >> newProcess.executionTime)) {
break;
}
insertProcess(minHeap, heapSize, newProcess);
}
printHeap(minHeap, heapSize);
return 0;
}
Ques - Emily- - - - - - - - - - - - - - - - - - - - - - - ?
#include <iostream>
using namespace std;
struct Message {
char content;
};
void swap(Message& a, Message& b) {
Message temp = a;
a = b;
b = temp;
}
// Function to insert a new message into the max heap
void insertMessage(Message heap[], int& heapSize, Message newMessage) {
heapSize++;
int i = heapSize - 1;
heap[i] = newMessage;
while (i > 0 && heap[(i - 1) / 2].content < heap[i].content) {
swap(heap[i], heap[(i - 1) / 2]);
i = (i - 1) / 2;
}
}
void printHeap(Message heap[], int heapSize) {
for (int i = 0; i < heapSize; i++) {
cout << heap[i].content << " ";
}
cout << endl;
}
int main() {
Message maxHeap[100]; // Assuming a maximum heap size of 100
int heapSize = 0;
while (true) {
Message newMessage;
if (!(cin >> newMessage.content)) {
break;
}
insertMessage(maxHeap, heapSize, newMessage);
}
printHeap(maxHeap, heapSize);
return 0;
}
Ques - Gaming_tournament- - - - - - - - - - - - - - - - - - - - - - - ?
#include <iostream>
using namespace std;
struct Player {
int score;
};
void swap(Player& a, Player& b) {
Player temp = a;
a = b;
b = temp;
}
// Function to insert a new player's score into the max heap
void insertScore(Player heap[], int& heapSize, Player newPlayer) {
heapSize++;
int i = heapSize - 1;
heap[i] = newPlayer;
while (i > 0 && heap[(i - 1) / 2].score < heap[i].score) {
swap(heap[i], heap[(i - 1) / 2]);
i = (i - 1) / 2;
}
}
void printHeap(Player heap[], int heapSize) { // Removed the 'const' keyword
for (int i = 0; i < heapSize; i++) {
cout << heap[i].score << " ";
}
cout << endl;
}
int main() {
Player maxHeap[100];
int heapSize = 0;
while (true) {
Player newPlayer;
if (!(cin >> newPlayer.score)) {
break;
}
insertScore(maxHeap, heapSize, newPlayer);
}
printHeap(maxHeap, heapSize);
return 0;
}
Ques - Isabella- - - - - - - - - - - - - - - - - - - - - - - ?
#include <iostream>
using namespace std;
struct Job {
int priority;
};
void swap(Job& a, Job& b) {
Job temp = a;
a = b;
b = temp;
}
// Function to insert a new job into the min heap based on priority
void insertJob(Job heap[], int& heapSize, int newJobPriority) {
heapSize++;
int i = heapSize - 1;
heap[i].priority = newJobPriority;
while (i > 0 && heap[(i - 1) / 2].priority > heap[i].priority) {
swap(heap[i], heap[(i - 1) / 2]);
i = (i - 1) / 2;
}
}
void printHeap(Job heap[], int heapSize) {
for (int i = 0; i < heapSize; i++) {
cout << heap[i].priority << " ";
}
cout << endl;
}
int main() {
Job minHeap[100];
int heapSize = 0;
while (true) {
int newJobPriority;
if (!(cin >> newJobPriority)) {
break;
}
insertJob(minHeap, heapSize, newJobPriority);
}
printHeap(minHeap, heapSize);
return 0;
}
Ques - Magician- - - - - - - - - - - - - - - - - - - - - - - ?
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main() {
int n;
cin >> n;
vector<int> arr(n);
for (int i = 0; i < n; i++) {
cin >> arr[i];
}
int k;
cin >> k;
make_heap(arr.begin(), arr.end(), greater<int>());
if (k > n) {
cout << "Invalid entry" << endl;
} else {
cout << "Min heap is: ";
for (int i = 0; i < arr.size(); i++) {
cout << arr[i] << " ";
}
cout << endl;
cout << "Kth element in min heap is " << arr[k-1] << endl;
}
return 0;
}
Ques - Network_simulator - - - - - - - - - - - - - - - - - - - - - - ?
#include <iostream>
using namespace std;
// to heapify a subtree with root at given index
void MaxHeapify(int arr[], int i, int N)
{
int l = 2 * i + 1;
int r = 2 * i + 2;
int largest = i;
if (l < N && arr[l] > arr[i])
largest = l;
if (r < N && arr[r] > arr[largest])
largest = r;
if (largest != i) {
swap(arr[i], arr[largest]);
MaxHeapify(arr, largest, N);
}
}
// This function basically builds max heap
void convertMaxHeap(int arr[], int N)
{
// Start from bottommost and rightmost internal node and heapify all internal nodes in bottom up way
for (int i = (N - 2) / 2; i >= 0; --i)
MaxHeapify(arr, i, N);
}
void printArray(int* arr, int size)
{
for (int i = 0; i < size; ++i)
cout << arr[i] << " ";
cout<<endl;
}
int main()
{
int arrayCount;
cin>>arrayCount;
int arr[arrayCount];
for (int i=0;i<arrayCount;i++){
cin>>arr[i];
}
convertMaxHeap(arr, arrayCount);
printArray(arr, arrayCount);
cout<<"The largest element is "<<arr[0]<<endl;
return 0;
}
Ques - Video_game- - - - - - - - - - - - - - - - - - - - - - ?
#include <iostream>
using namespace std;
void min_heap(int *a, int m, int n){
int j, t;
t= a[m];
j = 2 * m;
while (j <= n) {
if (j < n && a[j+1] < a[j])
j = j + 1;
if (t < a[j])
break;
else if (t >= a[j]) {
a[j/2] = a[j];
j = 2 * j;
}
}
a[j/2] = t;
return;
}
void build_minheap(int *a, int n) {
int k;
for(k = n/2; k >= 1; k--) {
min_heap(a,k,n);
}
}
int main() {
int n, i;
cin>>n;
int a[n];
for (i = 1; i <= n; i++) {
cin>>a[i];
}
build_minheap(a, n);
for (i = 1; i <= n; i++) {
cout<<a[i]<<" ";
}
}
Lecture 33:
Ques - Amritha- - - - - - - - - - - - - - - - - - - - - - - ?
#include <iostream>
using namespace std;
void heapify(int arr[], int n, int i) {
int smallest = i;
int left = 2 * i + 1;
int right = 2 * i + 2;
if (left < n && arr[left] < arr[smallest])
smallest = left;
if (right < n && arr[right] < arr[smallest])
smallest = right;
if (smallest != i) {
swap(arr[i], arr[smallest]);
heapify(arr, n, smallest);
}
}
void buildMinHeap(int arr[], int n) {
for (int i = n / 2 - 1; i >= 0; i--) {
heapify(arr, n, i);
}
}
void deleteMin(int arr[], int& n) {
arr[0] = arr[n - 1];
n--;
heapify(arr, n, 0);
}
void displayHeap(int arr[], int n) {
for (int i = 0; i < n; i++) {
cout << arr[i] << " ";
}
}
int main() {
int n;
cin >> n;
int arr[n];
for (int i = 0; i < n; i++) {
cin >> arr[i];
}
buildMinHeap(arr, n);
deleteMin(arr, n);
displayHeap(arr, n);
return 0;
}
Ques - Bheem- - - - - - - - - - - - - - - - - - - - - - - ?
#include <iostream>
#include <vector>
using namespace std;
void minHeapify(vector<int>& minHeap, int n, int i) {
int smallest = i;
int left = 2 * i + 1;
int right = 2 * i + 2;
if (left < n && minHeap[left] < minHeap[smallest])
smallest = left;
if (right < n && minHeap[right] < minHeap[smallest])
smallest = right;
if (smallest != i) {
swap(minHeap[i], minHeap[smallest]);
minHeapify(minHeap, n, smallest);
}
}
void buildMinHeap(vector<int>& minHeap) {
int n = minHeap.size();
for (int i = n / 2 - 1; i >= 0; i--) {
minHeapify(minHeap, n, i);
}
}
void deleteElement(vector<int>& minHeap, int key) {
int n = minHeap.size();
int i;
for (i = 0; i < n; i++) {
if (minHeap[i] == key)
break;
}
swap(minHeap[i], minHeap[n - 1]);
minHeap.pop_back();
minHeapify(minHeap, n - 1, i);
}
int main() {
int n;
cin >> n;
vector<int> minHeap(n);
for (int i = 0; i < n; i++) {
cin >> minHeap[i];
}
buildMinHeap(minHeap);
int key;
cin >> key;
deleteElement(minHeap, key);
for (int i = 0; i < minHeap.size(); i++) {
cout << minHeap[i];
if (i < minHeap.size() - 1) {
cout << " ";
}
}
cout << endl;
return 0;
}
Ques - Chitra- - - - - - - - - - - - - - - - - - - - - - - ?
#include <iostream>
#include <algorithm>
using namespace std;
int main(){
int n;
cin>>n;
int arr[n];
for(int i=0;i<n;i++){
cin>>arr[i];
}
sort(arr,arr+n);
for(int i:arr){
cout<<i<<" ";
}
return 0;
}
Ques - Group_of_children- - - - - - - - - - - - - - - - - - - - - - - ?
#include <iostream>
using namespace std;
void maxHeapify(int arr[], int n, int i) {
int largest = i;
int left = 2 * i + 1;
int right = 2 * i + 2;
if (left < n && arr[left] > arr[largest])
largest = left;
if (right < n && arr[right] > arr[largest])
largest = right;
if (largest != i) {
swap(arr[i], arr[largest]);
maxHeapify(arr, n, largest);
}
}
int serveIceCream(int arr[], int& n) {
if (n <= 0)
return -1;
int mostExcited = arr[0];
arr[0] = arr[n - 1];
n--;
maxHeapify(arr, n, 0);
return mostExcited;
}
int main() {
int* children = nullptr;
int n;
cin >> n;
children = new int[n];
for (int i = 0; i < n; i++) {
cin >> children[i];
}
for (int i = n / 2 - 1; i >= 0; i--) {
maxHeapify(children, n, i);
}
int mostExcited = serveIceCream(children, n);
if (mostExcited != -1) {
cout << mostExcited << endl;
}
while (n > 0) {
int mostExcited = serveIceCream(children, n);
if (mostExcited != -1) {
cout << mostExcited << " ";
}
}
delete[] children;
return 0;
}
Ques -Kamal- - - - - - - - - - - - - - - - - - - - - - - ?
#include <iostream>
using namespace std;
void heapify(int arr[], int n, int i) {
int smallest = i;
int left = 2 * i + 1;
int right = 2 * i + 2;
if (left < n && arr[left] < arr[smallest])
smallest = left;
if (right < n && arr[right] < arr[smallest])
smallest = right;
if (smallest != i) {
swap(arr[i], arr[smallest]);
heapify(arr, n, smallest);
}
}
void buildMinHeap(int arr[], int n) {
for (int i = n / 2 - 1; i >= 0; i--) {
heapify(arr, n, i);
}
}
void deleteInRange(int arr[], int& n, int start, int end) {
int i;
for (i = 0; i < n; i++) {
if (arr[i] >= start)
break;
}
int j = i;
for (; i < n; i++) {
if (arr[i] > end)
break;
}
int deleteCount = i - j;
if (deleteCount > 0) {
for (int k = j + deleteCount; k < n; k++) {
arr[k - deleteCount] = arr[k];
}
n -= deleteCount;
}
buildMinHeap(arr, n);
}
void displayHeap(int arr[], int n) {
for (int i = 0; i < n; i++) {
cout << arr[i] << " ";
}
}
int main() {
int n;
cin >> n;
int arr[n];
for (int i = 0; i < n; i++) {
cin >> arr[i];
}
int start, end;
cin >> start >> end;
buildMinHeap(arr, n);
deleteInRange(arr, n, start, end);
displayHeap(arr, n);
return 0;
}
Ques -Lithika- - - - - - - - - - - - - - - - - - - - - - - ?
#include <iostream>
using namespace std;
#define MAX_SIZE 100;
void heapify(int arr[], int n, int i) {
int largest = i;
int l = 2 * i + 1;
int r = 2 * i + 2;
if (l < n && arr[l] > arr[largest])
largest = l;
if (r < n && arr[r] > arr[largest])
largest = r;
if (largest != i) {
swap(arr[i], arr[largest]);
heapify(arr, n, largest);
}
}
void buildMaxHeap(int arr[], int n) {
for (int i = n / 2 - 1; i >= 0; i--)
heapify(arr, n, i);
}
int extractMax(int arr[], int& n) {
int maxElement = arr[0];
arr[0] = arr[n - 1];
n--;
heapify(arr, n, 0);
return maxElement;
}
void printArray(int arr[], int n) {
for (int i = 0; i < n; ++i)
cout << arr[i] << " ";
}
int main() {
int n;
cin >> n;
int arr[MAX_SIZE];
for (int i = 0; i < n; ++i)
cin >> arr[i];
buildMaxHeap(arr, n);
int maxElement = extractMax(arr, n);
if (maxElement != -1) {
cout << maxElement << endl;
}
printArray(arr, n);
return 0;
}
Ques -Mithu- - - - - - - - - - - - - - - - - - - - - - - ?
#include <iostream>
#include <vector>
using namespace std;
void maxHeapify(vector<int>& maxHeap, int n, int i) {
int largest = i;
int left = 2 * i + 1;
int right = 2 * i + 2;
if (left < n && maxHeap[left] > maxHeap[largest])
largest = left;
if (right < n && maxHeap[right] > maxHeap[largest])
largest = right;
if (largest != i) {
swap(maxHeap[i], maxHeap[largest]);
maxHeapify(maxHeap, n, largest);
}
}
void buildMaxHeap(vector<int>& maxHeap) {
int n = maxHeap.size();
for (int i = n / 2 - 1; i >= 0; i--) {
maxHeapify(maxHeap, n, i);
}
}
void removeElementsGreaterThanThreshold(vector<int>& maxHeap, int threshold) {
vector<int> newHeap;
for (int num : maxHeap) {
if (num <= threshold) {
newHeap.push_back(num);
}
}
maxHeap = newHeap;
}
int main() {
int n;
cin >> n;
vector<int> maxHeap(n);
for (int i = 0; i < n; i++) {
cin >> maxHeap[i];
}
buildMaxHeap(maxHeap);
int threshold;
cin >> threshold;
removeElementsGreaterThanThreshold(maxHeap, threshold);
for (int i = 0; i < maxHeap.size(); i++) {
cout << maxHeap[i] << " ";
}
cout << endl;
return 0;
}
Ques -Mythili- - - - - - - - - - - - - - - - - - - - - - - ?
#include <iostream>
#include <algorithm>
using namespace std;
int main(){
int n,m;
cin>>n;
int arr[n];
for(int i=0;i<n;i++){
cin>>arr[i];
}
sort(arr,arr+n);
cin>>m;
if(m==1){
for(int i:arr){
cout<<i<<" ";
}
}
else if(m==2){
reverse(arr,arr+n);
for(int i:arr){
cout<<i<<" ";
}
}
else{
cout<<"Invalid choice";
}
return 0;
}
Ques -Ram- - - - - - - - - - - - - - - - - - - - - - - ?
#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
using namespace std;
void maxHeapify(vector<string> &arr, int n, int i) {
int largest = i;
int left = 2 * i + 1;
int right = 2 * i + 2;
if (left < n && arr[left] > arr[largest])
largest = left;
if (right < n && arr[right] > arr[largest])
largest = right;
if (largest != i) {
swap(arr[i], arr[largest]);
maxHeapify(arr, n, largest);
}
}
void heapSort(vector<string> &arr) {
int n = arr.size();
for (int i = n / 2 - 1; i >= 0; i--) {
maxHeapify(arr, n, i);
}
for (int i = n - 1; i > 0; i--) {
swap(arr[0], arr[i]);
maxHeapify(arr, i, 0);
}
}
int main() {
int n;
cin >> n;
vector<string> names(n);
for (int i = 0; i < n; i++) {
cin >> names[i];
}
heapSort(names);
for (const string &name : names) {
cout << name << " ";
}
cout << endl;
return 0;
}
Ques -Rita- - - - - - - - - - - - - - - - - - - - - - - ?
#include <iostream>
#include <algorithm>
using namespace std;
int main(){
int n;
cin>>n;
int arr[n];
for(int i=0;i<n;i++){
cin>>arr[i];
}
sort(arr,arr+n);
for(int i:arr){
cout<<i<<" ";
}
return 0;
}
Ques -Student_with_varying_score- - - - - - - - - - - - - - - - - - - - - - - ?
#include <iostream>
#include <algorithm>
using namespace std;
int main(){
int n;
cin>>n;
int arr[n];
for(int i=0;i<n;i++){
cin>>arr[i];
}
sort(arr,arr+n);
cout<<arr[n-1];
return 0;
}
Ques -Digital_music_library- - - - - - - - - - - - - - - - - - - - - - - ?
#include <iostream>
using namespace std;
void swap(int &x, int &y) {
int temp = x;
x = y;
y = temp;
}
void heapifyDown(int *heap, int n, int i) {
int smallest = i;
int left = 2 * i + 1;
int right = 2 * i + 2;
if (left < n && heap[left] < heap[smallest])
smallest = left;
if (right < n && heap[right] < heap[smallest])
smallest = right;
if (smallest != i) {
swap(heap[i], heap[smallest]);
heapifyDown(heap, n, smallest);
}
}
void deleteShortestSong(int *heap, int &n) {
swap(heap[0], heap[n - 1]);
n--;
heapifyDown(heap, n, 0);
}
int main() {
int *heap;
int n;
cin >> n;
heap = new int[n];
for (int i = 0; i < n; i++) {
cin >> heap[i];
}
for (int i = n / 2 - 1; i >= 0; i--) {
heapifyDown(heap, n, i);
}
deleteShortestSong(heap, n);
for (int i = 0; i < n; i++) {
cout << heap[i] << " ";
}
cout << endl;
delete[] heap;
return 0;
}
Ques -Software_application- - - - - - - - - - - - - - - - - - - - - - - ?
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
void maxHeapify(vector<int>& maxHeap, int n, int i) {
int largest = i;
int left = 2 * i + 1;
int right = 2 * i + 2;
if (left < n && maxHeap[left] > maxHeap[largest])
largest = left;
if (right < n && maxHeap[right] > maxHeap[largest])
largest = right;
if (largest != i) {
swap(maxHeap[i], maxHeap[largest]);
maxHeapify(maxHeap, n, largest);
}
}
void buildMaxHeap(vector<int>& maxHeap) {
int n = maxHeap.size();
for (int i = n / 2 - 1; i >= 0; i--) {
maxHeapify(maxHeap, n, i);
}
}
void removeMaxScore(vector<int>& maxHeap) {
if (!maxHeap.empty()) {
swap(maxHeap[0], maxHeap.back());
maxHeap.pop_back();
maxHeapify(maxHeap, maxHeap.size(), 0);
}
}
int main() {
int n;
cin >> n;
vector<int> maxHeap;
for (int i = 0; i < n; i++) {
int score;
cin >> score;
maxHeap.push_back(score);
}
buildMaxHeap(maxHeap);
// Remove the maximum score
removeMaxScore(maxHeap);
for (int i = 0; i < maxHeap.size(); i++) {
cout << maxHeap[i];
if (i < maxHeap.size() - 1) {
cout << " ";
}
}
cout << endl;
return 0;
}