Get free ebooK with 50 must do coding Question for Product Based Companies solved
Fill the details & get ebook over email
Thank You!
We have sent the Ebook on 50 Must Do Coding Questions for Product Based Companies Solved over your email. All the best!

Quiz Test

Last Updated on March 30, 2022 by Ria Pathak

Concepts Used

Queues.

Difficulty Level

Hard.

Problem Statement :

Ram has recently appeared for his Physics, Maths, and Chemistry quiz where each subject has multiple quizzes. Ram’s class teacher asked him to solve a challenge which would allow him some extra marks at his pending assignment work. The arrays [P1,P2,P3…,Pn], [M1,M2,M3…,Mn] and [C1,C2,C3…,Cn] contains the marks obtained by Ram in ith quiz of Physics, Maths and Chemistry respectively. His class teacher asks Ram to calculate the total marks where the value of Ram’s ith quiz marks would be the sum of his marks in the ith Physics quiz, ith Maths quiz and ith Chemistry quiz. Ram is quite convinced by the task given by his teacher, but his teacher adds some twist to the task, and now she wants Ram to delete the qth smallest marks once it is answered by Ram. Seeing much complications Ram is confused and needs your help, help Ram to answer all the questions asked by his teacher.

See original problem statement here

EXAMPLE:

Input
3
1 4 6
1 2 3
4 8 9
2
1
4

Output
6
-1

Sample test case explanation
For the first query q=1,
Smallest marks are [1,1,4] for Physics, Maths and Chemistry quiz respectively. 
Therefore the output is 1+1+4=6.
For the second query q>n, therefore the output is −1.

EXPLANATION:

Push the sum of marks in each subject to the priority queue.

For each query pop out the qth element of the priority queue.

Push the rest back to the queue.Follow the code.

SOLUTION:

#include
#include
using namespace std;
int main(void)
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int n;
    cin>>n;
    long int P[n];
    long int M[n];
    long int C[n];
    for(int i=0;i>P[i];
    for(int i=0;i>M[i];
    for(int i = 0; i < n; i++)
        cin>>C[i];
    priority_queue,greater>pq;
    queuequ;
    for(int i = 0; i < n; i++)
        pq.push(P[i] + M[i] + C[i]);
    int Q;
    cin>>Q;
    for(int i = 0; i < Q; i++)
    {
        int q;
        cin>>q;
        if(q > pq.size())
        cout<<"-1"<



						 
import java.util.*;
  import java.io.*;
  public class Main {

    public static void main(String args[]) throws IOException {
      Scanner sc=new Scanner(System.in);
        int n=sc.nextInt();
        Long[] P= new Long[n];
        Long[] M= new Long[n];
        Long[] C= new Long[n];

         for(int i=0;i PQ = new PriorityQueue(); 
        Queue qu = new LinkedList(); 
        for(int i=0;iPQ.size())
       {
           System.out.println(-1);
       }
       else{
           for(int j=1;j
						 
from queue import PriorityQueue
n = int(input())
p = list(map(int,input().split()))
m = list(map(int,input().split()))
c = list(map(int,input().split()))
pq = PriorityQueue()
qu = []

for i in range(n):
	pq.put(p[i] + m[i] + c[i])

Q = int(input())

for i in range(Q):
	q = int(input())
	
	if q > pq.qsize():
		print(-1)
	else:
		
		for j in range(1, q):
			top = pq.get()
			pq.put(top)
			qu.push(top)
			pq.get()
		
		top = pq.get()
		pq.put(top)
		print(top, end = " ")
		pq.get()

		while qu:
			pq.put(qu[0])
			qu.pop(0)
			

[forminator_quiz id="2331"]

This article tried to discuss the concept of queues. Hope this blog helps you understand and solve the problem. To practice more problems on queues you can check out MYCODE | Competitive Programming.

Leave a Reply

Your email address will not be published. Required fields are marked *