Interleave the first half of the queue with second half
Last Updated :
28 Nov, 2022
Given a queue of integers of even length, rearrange the elements by interleaving the first half of the queue with the second half of the queue.
Examples:
Input : 1 2 3 4
Output : 1 3 2 4
Input : 11 12 13 14 15 16 17 18 19 20
Output : 11 16 12 17 13 18 14 19 15 20
Using Stack
Following are the steps to solve the problem:
- Push the first half elements of the queue to stack.
- Enqueue back the stack elements.
- Dequeue the first half elements of the queue and enqueue them back.
- Again push the first half elements into the stack.
- Interleave the elements of queue and stack.
Implementation:
C++
#include <bits/stdc++.h>
using namespace std;
void interLeaveQueue(queue< int >& q)
{
if (q.size() % 2 != 0)
cout << "Input even number of integers." << endl;
stack< int > s;
int halfSize = q.size() / 2;
for ( int i = 0; i < halfSize; i++) {
s.push(q.front());
q.pop();
}
while (!s.empty()) {
q.push(s.top());
s.pop();
}
for ( int i = 0; i < halfSize; i++) {
q.push(q.front());
q.pop();
}
for ( int i = 0; i < halfSize; i++) {
s.push(q.front());
q.pop();
}
while (!s.empty()) {
q.push(s.top());
s.pop();
q.push(q.front());
q.pop();
}
}
int main()
{
queue< int > q;
q.push(11);
q.push(12);
q.push(13);
q.push(14);
q.push(15);
q.push(16);
q.push(17);
q.push(18);
q.push(19);
q.push(20);
interLeaveQueue(q);
int length = q.size();
for ( int i = 0; i < length; i++) {
cout << q.front() << " " ;
q.pop();
}
return 0;
}
|
Java
import java.util.*;
class GFG {
static void interLeaveQueue(Queue<Integer> q)
{
if (q.size() % 2 != 0 )
System.out.println(
"Input even number of integers." );
Stack<Integer> s = new Stack<>();
int halfSize = q.size() / 2 ;
for ( int i = 0 ; i < halfSize; i++) {
s.push(q.peek());
q.poll();
}
while (!s.empty()) {
q.add(s.peek());
s.pop();
}
for ( int i = 0 ; i < halfSize; i++) {
q.add(q.peek());
q.poll();
}
for ( int i = 0 ; i < halfSize; i++) {
s.push(q.peek());
q.poll();
}
while (!s.empty()) {
q.add(s.peek());
s.pop();
q.add(q.peek());
q.poll();
}
}
public static void main(String[] args)
{
Queue<Integer> q = new java.util.LinkedList<>();
q.add( 11 );
q.add( 12 );
q.add( 13 );
q.add( 14 );
q.add( 15 );
q.add( 16 );
q.add( 17 );
q.add( 18 );
q.add( 19 );
q.add( 20 );
interLeaveQueue(q);
int length = q.size();
for ( int i = 0 ; i < length; i++) {
System.out.print(q.peek() + " " );
q.poll();
}
}
}
|
Python3
from queue import Queue
def interLeaveQueue(q):
if (q.qsize() % 2 ! = 0 ):
print ( "Input even number of integers." )
s = []
halfSize = int (q.qsize() / 2 )
for i in range (halfSize):
s.append(q.queue[ 0 ])
q.get()
while len (s) ! = 0 :
q.put(s[ - 1 ])
s.pop()
for i in range (halfSize):
q.put(q.queue[ 0 ])
q.get()
for i in range (halfSize):
s.append(q.queue[ 0 ])
q.get()
while len (s) ! = 0 :
q.put(s[ - 1 ])
s.pop()
q.put(q.queue[ 0 ])
q.get()
if __name__ = = '__main__' :
q = Queue()
q.put( 11 )
q.put( 12 )
q.put( 13 )
q.put( 14 )
q.put( 15 )
q.put( 16 )
q.put( 17 )
q.put( 18 )
q.put( 19 )
q.put( 20 )
interLeaveQueue(q)
length = q.qsize()
for i in range (length):
print (q.queue[ 0 ], end = " " )
q.get()
|
C#
using System;
using System.Collections.Generic;
class GFG {
static void interLeaveQueue(Queue< int > q)
{
if (q.Count % 2 != 0)
Console.WriteLine(
"Input even number of integers." );
Stack< int > s = new Stack< int >();
int halfSize = q.Count / 2;
for ( int i = 0; i < halfSize; i++) {
s.Push(q.Peek());
q.Dequeue();
}
while (s.Count != 0) {
q.Enqueue(s.Peek());
s.Pop();
}
for ( int i = 0; i < halfSize; i++) {
q.Enqueue(q.Peek());
q.Dequeue();
}
for ( int i = 0; i < halfSize; i++) {
s.Push(q.Peek());
q.Dequeue();
}
while (s.Count != 0) {
q.Enqueue(s.Peek());
s.Pop();
q.Enqueue(q.Peek());
q.Dequeue();
}
}
public static void Main(String[] args)
{
Queue< int > q = new Queue< int >();
q.Enqueue(11);
q.Enqueue(12);
q.Enqueue(13);
q.Enqueue(14);
q.Enqueue(15);
q.Enqueue(16);
q.Enqueue(17);
q.Enqueue(18);
q.Enqueue(19);
q.Enqueue(20);
interLeaveQueue(q);
int length = q.Count;
for ( int i = 0; i < length; i++) {
Console.Write(q.Peek() + " " );
q.Dequeue();
}
}
}
|
Javascript
<script>
function interLeaveQueue(q){
if (q.length % 2 != 0)
document.write( "Inpush even number of integers." )
let s = []
let halfSize = Math.floor(q.length / 2)
for (let i = 0; i < halfSize; i++)
s.push(q.shift())
while (s.length != 0){
q.push(s[s.length-1])
s.pop()
}
for (let i = 0; i < halfSize; i++)
q.push(q.shift())
for (let i = 0; i < halfSize; i++)
s.push(q.shift())
while (s.length != 0){
q.push(s[s.length-1])
s.pop()
q.push(q.shift())
}
}
let q =[]
q.push(11)
q.push(12)
q.push(13)
q.push(14)
q.push(15)
q.push(16)
q.push(17)
q.push(18)
q.push(19)
q.push(20)
interLeaveQueue(q)
let length = q.length
for (let i=0;i<length;i++)
document.write(q.shift(), " " )
</script>
|
Output
11 16 12 17 13 18 14 19 15 20
Time complexity: O(n). Auxiliary Space: O(n).
Using Queue
We can also solve the given problem by using a queue instead of a stack. The idea is to move the first half to another queue, and then push values from the temporary queue and original queue into the original queue. The original queue will get converted to the interleaved queue after the operations.
Steps to solve :
- Make a temporary queue and push the first half of the original queue into the temp queue.
- Till the temp queue is empty
- Pop the front of the temp queue and push it to the original queue
- Pop the front of the original queue and push it to the original queue
- The original queue is converted to the interleaved queue.
Move the first half to the temporary queue
Interleaving the two halves
C++
#include <bits/stdc++.h>
using namespace std;
void interLeaveQueue(queue< int >& q)
{
if (q.size() % 2 != 0)
cout << "Input even number of integers." << endl;
queue< int > temp;
int halfSize = q.size() / 2;
for ( int i = 0; i < halfSize; i++) {
temp.push(q.front());
q.pop();
}
while (!temp.empty()) {
q.push(temp.front());
q.push(q.front());
q.pop();
temp.pop();
}
}
int main()
{
queue< int > q;
q.push(11);
q.push(12);
q.push(13);
q.push(14);
q.push(15);
q.push(16);
q.push(17);
q.push(18);
q.push(19);
q.push(20);
interLeaveQueue(q);
int length = q.size();
for ( int i = 0; i < length; i++) {
cout << q.front() << " " ;
q.pop();
}
return 0;
}
|
Java
Python3
C#
Javascript
Output
11 16 12 17 13 18 14 19 15 20
This approach is contributed by Rudra Tiwari.
Time complexity: O(n)
Auxiliary Space: O(n).
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...