3 849
Підписники
Немає даних24 години
-297 днів
-12730 день
Архів дописів
3 849
// You are using GCC
#include <iostream>
#include <deque>
#include <algorithm>
using namespace std;
int main() {
int N;
// cout << "Enter the number of elements: ";
cin >> N;
deque<int> elements;
// cout << "Enter the elements: ";
for (int i = 0; i < N; i++) {
int element;
cin >> element;
elements.push_back(element);
}
sort(elements.begin(), elements.end(), greater<int>());
// cout << "Elements sorted in descending order: ";
for (auto it = elements.begin(); it != elements.end(); ++it) {
cout << *it << " ";
}
cout << endl;
//awasthi
return 0;
}
3 849
John is a software engineer working on an application that requires sorting elements of an array in a dequeue format.
Write a program to help John implement the functionality to sort the elements in an array in descending order.
Input format :
The first line of input consists of an integer N, representing the number of elements.
The second line consists of N space-separated elements.
3 849
// You are using GCC
#include <iostream>
#include <deque>
using namespace std;
int main() {
deque<int> stockPrices;
int price;
// cout << "Enter the stock prices (enter -1 to stop): ";
while (cin >> price && price != -1) {
stockPrices.push_back(price);
}
int minPrice = stockPrices.front();
for (auto it = stockPrices.begin(); it != stockPrices.end(); ++it) {
if (*it < minPrice) {
minPrice = *it;
}
}
//awasthi
cout << minPrice << endl;
return 0;
}
3 849
Michael is developing a stock market analysis tool that processes historical stock prices. As part of his task, he needs to find and display the minimum stock price using a double-ended queue (deque).
Michael has a deque that stores a collection of stock prices. The deque is initially empty. Write a program that Michael can use to find and print the minimum stock price from the deque.
Input format :
The input consists of the elements of the dequeue.
The input is terminated by entering -1.
3 849
// You are using GCC
#include <iostream>
#include <deque>
using namespace std;
int main() {
deque<int> myDeque;
int front1, front2, back1, back2, front3, back3;
// cout << "Enter the first integer to insert at the front: ";
cin >> front1;
myDeque.push_front(front1);
// cout << "Enter the second integer to insert at the front: ";
cin >> front2;
myDeque.push_front(front2);
//cout << "Enter the first integer to insert at the back: ";
cin >> back1;
myDeque.push_back(back1);
//cout << "Enter the second integer to insert at the back: ";
cin >> back2;
myDeque.push_back(back2);
// cout << "Enter the third integer to insert at the front: ";
cin >> front3;
myDeque.push_front(front3);
// cout << "Enter the third integer to insert at the back: ";
cin >> back3;
myDeque.push_back(back3);
//awasthi
// cout << "Deque elements: ";
for (auto it = myDeque.begin(); it != myDeque.end(); ++it) {
cout << *it << " ";
}
cout << endl;
return 0;
}
3 849
You are tasked with implementing a double-ended queue, a data structure that allows elements to be added or removed from both ends.
You need to create a program that provides the following functionality:
Initialize an empty deque.
Insert elements at the front of the deque.
Insert elements at the back of the deque.
Print the elements of the deque.
Input format :
The first line of input consists of the first integer to insert at the front.
The second line consists of the second integer to insert at the front.
The third line consists of the first integer to insert at the back.
The fourth line consists of the second integer to insert at the back.
The fifth line consists of the third integer to insert at the front.
The sixth line consists of the third integer to insert at the back.
3 849
#include <iostream>
#define MAX 25
int queue[MAX];
int rear = -1;
int front = -1;
void Enqueue(int data) {
if (rear == MAX - 1)
std::cout << "Overflow" << std::endl;
else {
if (front == -1)
front = 0;
rear = rear + 1;
queue[rear] = data;
}
}
void Dequeue() {
if (front == -1 || front > rear) {
std::cout << "Underflow" << std::endl;
return;
} else {
front = front + 1;
}
}
void display() {
if (front == -1)
std::cout << "Queue is empty" << std::endl;
else {
for (int i = front; i <= rear; i++)
std::cout << queue[i] << " ";
std::cout << std::endl;
}
}
int main() {
int n, i, e;
std::cin >> n;
for (i = 0; i < n; i++) {
std::cin >> e;
Enqueue(e);
}
Dequeue();
display();
//awasthi
return 0;
}
3 849
You are given a Queue of N integers. Write a program to implement the dequeue operation using the linked list.
Input format :
The first line of input consists of an integer N, denoting the size of the queue.
The second line consists of N space-separated integers, denoting the elements of the queue.
Output format :
The output prints the queue after performing the dequeue operation.
If an underflow occurs, print "Underflow", and print "Queue is empty".
3 849
// You are using GCC
#include <iostream>
#include <deque>
#include <unordered_set>
int main() {
std::deque<int> deque;
std::unordered_set<int> uniqueElements;
int element;
while (std::cin >> element && element != -1) {
if (uniqueElements.find(element) == uniqueElements.end()) {
deque.push_back(element);
uniqueElements.insert(element);
}
}
for (int element : deque) {
std::cout << element << " ";
}
std::cout << std::endl;
//awasthi
return 0;
}
3 849
Olivia is working on a data processing project that involves analyzing a list of user IDs. As part of her task, she needs to display the user IDs without duplicates using a double-ended queue.
Olivia has a deque that stores a collection of user IDs. The deque is initially empty.
Write a program that Olivia can use to print the user IDs without duplicates from the deque.
Input format :
The input consists of the elements of the dequeue.
The input is terminated by entering -1.
3 849
// You are using GCC
#include <iostream>
#include <queue>
struct Patient {
int data;
int priority;
// Custom comparator to prioritize lower values of priority
bool operator>(const Patient& other) const {
return priority > other.priority;
}
};
int main() {
int N;
std::cin >> N;
std::priority_queue<Patient, std::vector<Patient>, std::greater<Patient>> pq;
for (int i = 0; i < N; ++i) {
int data, priority;
std::cin >> data >> priority;
pq.push({data, priority});
}
std::cout << "Priority queue elements: ";
if (pq.empty()) {
std::cout << "Priority queue is empty";
} else {
while (!pq.empty()) {
Patient p = pq.top();
pq.pop();
std::cout << p.data << "";
}
}
//awasthi
std::cout << std::endl;
return 0;
}
3 849
You are tasked with implementing a priority queue for a hospital's emergency room. The emergency room receives patients with different levels of severity, and it's crucial to treat patients based on their priority.
The program should allow the hospital staff to do the following:
Add patients to the priority queue: The staff should be able to enter the patient's age and severity level. The severity level is an integer value that indicates the urgency of the patient's condition, where a lower value represents a higher priority.
View the current queue: The staff should be able to view the list of patients in the queue, ordered by their priority.
Input format :
The first line of input consists of an integer N, representing the number of elements in the priority queue.
The following N lines consist of two space-separated integers: data and priority values.
Output format :
The output prints the elements of the priority queue in order of their priority.
If the priority queue is empty, it prints the message "Priority queue is empty"
3 849
// You are using GC
#include <iostream>
#include <queue>
int main() {
int n;
std::cin >> n;
std::queue<int> queue;
int sum = 0;
for (int i = 0; i < n; i++) {
int value;
std::cin >> value;
queue.push(value);
sum += value;
}
//awasthi
if (queue.empty()) {
std::cout << "Queue is empty." << std::endl;
} else {
double average = static_cast<double>(sum) / n;
std::cout << average << std::endl;
}
return 0;
}
3 849
You are tasked with implementing a program to calculate the average of elements in a queue. The queue represents a series of values, and you need to find the average of all the elements in the queue. However, there are a few scenarios to consider:
If the queue is empty, you should display the message "Queue is empty." and the average value should be 0.
If the queue is not empty, you need to calculate the average of its elements and display the result.
Note: This is a sample question asked in a TCS interview.
Input format :
The first line consists of the integer n, representing the number of elements in the queue.
The second line consists of the n space-separated integers representing the elements to be enqueued.
Output format :
The output displays, if the queue is empty, "Queue is empty." on a separate line.
If the queue is not empty, output the average of its elements as a double value on a separate line.
3 849
#include <iostream>
#include <queue>
#include <string>
struct Patient {
int priority;
std::string name;
};
//awasthi
struct ComparePatients {
bool operator()(const Patient& p1, const Patient& p2) {
return p1.priority > p2.priority;
}
};
int main() {
std::priority_queue<Patient, std::vector<Patient>, ComparePatients> patientQueue;
int choice;
while (true) {
// std::cout << "Enter choice (1: Add patient, 2: Treat patient, 0: Exit): ";
std::cin >> choice;
if (choice == 0) {
break;
} else if (choice == 1) {
Patient newPatient;
// std::cout << "Enter priority: ";
std::cin >> newPatient.priority;
// std::cout << "Enter name: ";
std::cin >> newPatient.name;
patientQueue.push(newPatient);
} else if (choice == 2) {
if (patientQueue.empty()) {
std::cout << "Error: Queue is empty." << std::endl;
} else {
Patient treatedPatient = patientQueue.top();
patientQueue.pop();
std::cout << "Patient with priority " << treatedPatient.priority << " and name " << treatedPatient.name << " has been treated." << std::endl;
}
} else {
std::cout << "Invalid choice." << std::endl;
}
}
return 0;
}
3 849
Develop a software solution for the emergency room to prioritize patients based on the severity of their conditions using a priority queue mechanism. The priority queue should prioritize patients with severe symptoms such as chest pain, loss of consciousness, and severe bleeding.
The software should be designed to ensure that patients with life-threatening conditions are seen by medical staff in a timely and efficient manner, reducing the risk of harm or loss of life. The solution should also be easily integrated with the existing systems in the emergency room.
Enter 1 to add a patient, 2 to treat a patient or 0 to exit
Note: There is a new line space after the last line of the output
Note: This is a sample question asked in a Wipro interview.
Input format :
The first line of input will contain an integer n, representing the number of patients to be added to the priority queue.
The next n lines will contain two values for each patient:
An integer priority, represents the severity of the patient's condition.
A string name, represents the name of the patient.
3 849
#include <iostream>
#include <string>
#include <queue>
struct Package {
std::string destination;
int priority;
Package(const std::string& dest, int prio) : destination(dest), priority(prio) {}
bool operator<(const Package& other) const {
return priority > other.priority;
}
};
int main() {
std::priority_queue<Package> deliveryQueue;
int choice;
do {
std::cin >> choice;
switch (choice) {
case 1: {
std::string destination;
int priority;
std::cin.ignore();
std::getline(std::cin, destination);
std::cin >> priority;
if (priority < 1) priority = 1;
if (priority > 5) priority = 5;
deliveryQueue.push(Package(destination, priority));
std::cout << "Package added to the delivery queue.\n";
break;
}
case 2:
if (!deliveryQueue.empty()) {
std::cout << "Delivered package to: " << deliveryQueue.top().destination << "\n";
deliveryQueue.pop();
} else {
std::cout << "No packages in the delivery queue.\n";
}
break;
case 3:
if (!deliveryQueue.empty()) {
std::cout << "Next package for delivery: " << deliveryQueue.top().destination << "\n";
} else {
std::cout << "No packages in the delivery queue.\n";
}
break;
case 4:
std::cout << "Exiting the application.\n";
break;
default:
std::cout << "Invalid choice.\n";
break;
}
} while (choice != 4);
return 0;
}
3 849
You are assigned the task of developing a package delivery system for a logistics company. The system should enable efficient management of packages for delivery, including prioritizing deliveries based on certain criteria.
The program utilizes a priority queue to manage packages for delivery. Each package is represented by its destination and delivery priority. The delivery priority is a value between 1 and 5, with higher values indicating higher priority for delivery.
The system offers the following options Add package, Deliver package, View next package for delivery, and Exit.
Input format :
The input consists of an integer representing the choice from the menu.
Choice 1: Add package
Choice 2: Deliver the package
Choice 3: View next package for delivery
choice 4: Exit
For choice 1, the program expects the following inputs on separate lines:
Package destination (string with spaces allowed)
Delivery priority (integer between 1 and 5, inclusive)
For choice 2, choice 3, and for choice 4, no additional input is required.
3 849
// You are using GCC
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
class Queue {
private:
vector<float> elements;
int maxSize;
public:
Queue(int size) : maxSize(size) {}
bool isEmpty() {
return elements.empty();
}
bool isFull() {
return elements.size() == maxSize;
}
void enqueue(float element) {
if (!isFull()) {
elements.push_back(element);
}
}
void dequeue() {
if (!isEmpty()) {
elements.erase(elements.begin());
}
}
float front() {
if (!isEmpty()) {
return elements[0];
}
return 0.0; // Default value for an empty queue
}
void deleteSmallest() {
if (!isEmpty()) {
float smallest = *min_element(elements.begin(), elements.end());
elements.erase(find(elements.begin(), elements.end(), smallest));
}
}
void display() {
for (float element : elements) {
cout << element << " ";
}
cout << endl;
}
};
int main() {
int n;
cin >> n;
Queue queue(100);
if (n == 0) {
cout << "queue is empty" << endl;
} else {
for (int i = 0; i < n; i++) {
float element;
cin >> element;
queue.enqueue(element);
}
queue.deleteSmallest();
queue.display();
}
return 0;
}
3 849
Usha wants to implement a queue data structure with the following operations: enqueue, dequeue, and deleteSmallest. She has written code for the queue implementation but needs help with the problem statement.
Implement a queue class called "Queue" with the following specifications:
The queue should support the following operations:
isEmpty(): Returns true if the queue is empty, false otherwise.
isFull(): Returns true if the queue is full, false otherwise.
enqueue(float element): Inserts the given element at the rear of the queue if the queue is not full.
dequeue(): Removes the element at the front of the queue if the queue is not empty.
front(): Returns the element at the front of the queue without removing it if the queue is not empty.
deleteSmallest(): Removes the smallest element from the queue. If there are multiple occurrences of the smallest element, remove the one that appears first (closest to the front of the queue).
Note: The queue should have a fixed maximum size of 100 elements.
Note: This is a sample question asked in a Wipro interview.
