Reverse First K elements of Queue | Practice | GeeksforGeeks

Given an integer K and a queue of integers, we need to reverse the order of the first K elements of the queue, leaving the other elements in the same relative order.
Only following standard operations are allowed on queue.
enqueue(x) : Add an item x to rear of queue
dequeue() : Remove an item from front of queue
size() : Returns number of elements in queue.
front() : Finds front item.
Note: The above operations represent the general processings. In-built functions of the respective languages can be used to solve the problem.
Example 1:
Input:
5 3
1 2 3 4 5

Output: 
3 2 1 4 5

Explanation: 
After reversing the given
input from the 3rd position the resultant
output will be 3 2 1 4 5.


Example 2:
Input:
4 4
4 3 2 1

Output: 
1 2 3 4

Explanation: 
After reversing the given
input from the 4th position the resultant
output will be 1 2 3 4.
Your Task:
Complete the provided function modifyQueue that takes queue and k as parameters and returns a modified queue. The printing is done automatically by the driver code.
Expected Time Complexity : O(n)
Expected Auxiliary Space : O(n)
Constraints:
1 <= N <= 1000
1 <= K <= N
Note: The Input/Output format and the Example given are used for the system's internal purpose, and should be used by a user for Expected Output only. As it is a function problem, hence a user should not read any input from stdin/console. The task is to complete the function specified, and not to write the full code.
0
hrithikjain98893 days ago
 Total Time Taken :-0.02/1.62 
// just removing first k element from queue and push it to a stack 
then pop those element from stack (as element from stack are poped out from backside ,so they are already in reverse order) and push these element into a new queue and then push the other remaining element from queue.
 
queue<int> modifyQueue(queue<int> q, int k) {   // add code here.   stack<int> s;   queue<int> q1;   int p=k;   while(p--){       s.push(q.front());   q.pop();}while(k--){   q1.push(s.top());   s.pop();}while(q.size()!=0){   q1.push(q.front());   q.pop();}return q1;
}
0
tanvib20141 week ago
queue<int> modifyQueue(queue<int> q, int k) {
    stack<int> st;
    queue<int> res;
    while(k--){
        int x = q.front(); q.pop();
        st.push(x);
    }
    while(!st.empty()){
        int x = st.top();
        st.pop();
        res.push(x);
    }
    while(!q.empty()){
        int x = q.front();
        q.pop();
        res.push(x);
    }
    return res;
}
0
lovedeep79051 week ago
//Only using stack java code
 
 Stack<Integer> stack=new Stack<>();        int m=q.size()-k;       for(int i=0;i<k;i++){           stack.push(q.remove());       }        for(int i=0;i<k;i++){           q.add(stack.pop());       }             for(int i=0;i<m;i++){           int removed=q.remove();           q.add(removed);                  }      return q; 
0
shilsoumyadip1 week ago
queue<int> modifyQueue(queue<int> q, int k) {
stack<int>s;   int x = q.size();   while(!q.empty() and q.size() != x - k )   {       s.push(q.front());       q.pop();   }   queue<int>q1;   while(!q.empty())   {       q1.push(q.front());       q.pop();   }    while(!s.empty())    {        q.push(s.top());        s.pop();    }    while(!q1.empty())    {        q.push(q1.front());        q1.pop();    }    return q;}
0
harshscode1 week ago
 stack<int> s;     for(int i=0;i<k;i++)     {         s.push(q.front());         q.pop();     }          while(!s.empty())     {         q.push(s.top());         s.pop();     }          int size=q.size()-k;          while(size--)     {        int x=q.front();        q.pop();        q.push(x);     }     return q;     
0
ankitap99322 weeks ago
queue<int> modifyQueue(queue<int> q, int k) {   //adding element till k to stack   stack<int> s;   for(int i=0;i<k;i++){       int x=q.front();       q.pop();       s.push(x);   }      //push back stack element to queue...why? --> these are those reversed k elements   while(!s.empty()){       q.push(s.top());       s.pop();   }      //since element in pushed from back in queue so our queue at this stage will be 4 5 3 2 1    //so ratate from n-k    (n=q.size())   int n=q.size();   for(int i=0;i<n-k;i++){       int x=q.front();       q.pop();       q.push(x);   }   return q;}
0
sharmadikshant57053 weeks ago
queue<int> modifyQueue(queue<int> q, int k) {   // add code here.      stack <int> s;   //pop first k from queue and push into stack   for(int i=0;i<k ; i++){       int val = q.front();       q.pop();       s.push(val);   }   // s till not empty   while(!s.empty()){       int val = s.top();       s.pop();       q.push(val);   }    int t = q.size() - k;    while(t--){        int val = q.front();        q.pop();        q.push(val);    }    return q;
0
amarrajsmart1974 weeks ago
Easy C++ Solution.Time Taken=0.0/1.6 Sec.
 
queue<int> modifyQueue(queue<int> q, int k) {   // add code here.   stack <int> st;   int n=q.size();   queue<int> q1;   for(int i=0;i<k;i++)   {       st.push(q.front());       q.pop();   }       for(int i=0;i<k;i++)    {        q.push(st.top());        st.pop();    }    for(int i=0;i<n-k;i++)    {        q1.push(q.front());        q.pop();    }       for(int i=0;i<n-k;i++)       {           q.push(q1.front());           q1.pop();       }       return q;}
0
akbhobhiya4 weeks ago
queue<int> modifyQueue(queue<int> q, int k) {
    // add code here.
    int n = q.size();
    vector<int>ak,an;
    for(int i=0;i<k;i++){
        ak.push_back(q.front());
        q.pop();
    }
    for(int i=k;i<n;i++){
        an.push_back(q.front());
        q.pop();
    }
    reverse(ak.begin(),ak.end());
    for(int i=0;i<k;i++)q.push(ak[i]);
    for(int i=k;i<n;i++)q.push(an[i-k]);
    return q;
}
0
abhishekyadav952014 weeks ago
SIMPLE C++ SOL.
//
queue<int> modifyQueue(queue<int> q, int k) {stack<int>s;int n=q.size();for(int i=0;i<k;i++){   int a=q.front();   q.pop();   s.push(a);}while(!s.empty()){   q.push(s.top());    s.pop();}for(int i=0;i<n-k;i++){   int a =q.front();    q.pop();    q.push(a);}return q;}
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.