Rearrange and update array elements as specified by the given queries
Last Updated :
24 Feb, 2023
Given an array arr[] of size N and queries Q[][], the task is to perform the following types of queries on the given array. 0: Left shift the array by one position.
- 1: Right shift the array by one position.
- 2 X Y: Update the value of arr[X] = Y.
- 3 X: Print arr[X].
Example:
Input: arr[]={1, 2, 3, 4, 5}, Q[][]={{0}, {1}, {3, 1}, {2, 2, 54}, {3, 2}}.
Output:4 54
Explanation:
Query1: The array arr[] modifies to {2, 3, 4, 5, 1}
Query2: The array arr[] modifies to {1, 2, 3, 4, 5}
Query3: Print the value of arr[1] i.e. 2
Query4: The array arr[] modifies to {1, 54, 3, 4, 5}
Query5: Print the value of arr[2], i.e. 54.
Input: arr[]={1}, Q[][]={{0}, {1}, {2, 0, 54}, {3, 0}}
Output: 54
Approach: The problem can be solved using Deque(Double Ended queue) to perform the insert and delete operation at the front and back of the queue in O(1). Follow the below steps to solve the problem.
- Create a double-ended Queue, dq.
- Push all elements of the array arr[] to dq.
- For the query of type 0(Left Shift), pop an element from the front of dq and push the element to the back of dq.
- For the query of type 1(Right Shift), pop an element from the back of dq and push the element to the front of dq.
- For the query of type 2, update dq[X] = Y.
- For the query of type 3, print dq[X].
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void Queries( int arr[], int N,
vector<vector< int > >& Q)
{
deque< int > dq;
for ( int i = 0; i < N; i++) {
dq.push_back(arr[i]);
}
int sz = Q.size();
for ( int i = 0; i < sz; i++) {
if (Q[i][0] == 0) {
int front = dq[0];
dq.pop_front();
dq.push_back(front);
}
else if (Q[i][0] == 1) {
int back = dq[N - 1];
dq.pop_back();
dq.push_front(back);
}
else if (Q[i][0] == 2) {
dq[Q[i][1]] = Q[i][2];
}
else {
cout << dq[Q[i][1]] << " " ;
}
}
}
int main()
{
int arr[] = { 1, 2, 3, 4, 5 };
int N = sizeof (arr) / sizeof (arr[0]);
vector<vector< int > > Q;
Q = { { 0 }, { 1 }, { 3, 1 },
{ 2, 2, 54 }, { 3, 2 } };
Queries(arr, N, Q);
return 0;
}
|
Java
import java.util.*;
class GFG{
static void Queries( int arr[], int N,
int [][]Q)
{
Vector<Integer> dq = new Vector<>();
for ( int i = 0 ; i < N; i++)
{
dq.add(arr[i]);
}
int sz = Q.length;
for ( int i = 0 ; i < sz; i++)
{
if (Q[i][ 0 ] == 0 )
{
int front = dq.get( 0 );
dq.remove( 0 );
dq.add(front);
}
else if (Q[i][ 0 ] == 1 )
{
int back = dq.elementAt(dq.size() - 1 );
dq.remove(dq.size() - 1 );
dq.add( 0 , back);
}
else if (Q[i][ 0 ] == 2 )
{
dq.set(Q[i][ 1 ], Q[i][ 2 ]);
}
else
{
System.out.print(dq.get(Q[i][ 1 ]) + " " );
}
}
}
public static void main(String[] args)
{
int arr[] = { 1 , 2 , 3 , 4 , 5 };
int N = arr.length;
int [][]Q = {{ 0 }, { 1 }, { 3 , 1 },
{ 2 , 2 , 54 }, { 3 , 2 }};
Queries(arr, N, Q);
}
}
|
Python3
from collections import deque
def Queries(arr, N, Q):
dq = deque()
for i in range (N):
dq.append(arr[i])
sz = len (Q)
for i in range (sz):
if (Q[i][ 0 ] = = 0 ):
front = dq[ 0 ]
dq.popleft()
dq.appendleft(front)
elif (Q[i][ 0 ] = = 1 ):
back = dq[N - 1 ]
dq.popleft()
dq.appendleft(back)
elif (Q[i][ 0 ] = = 2 ):
dq[Q[i][ 1 ]] = Q[i][ 2 ]
else :
print (dq[Q[i][ 1 ]], end = " " )
if __name__ = = '__main__' :
arr = [ 1 , 2 , 3 , 4 , 5 ]
N = len (arr)
Q = [ [ 0 ], [ 1 ], [ 3 , 1 ],
[ 2 , 2 , 54 ], [ 3 , 2 ] ]
Queries(arr, N, Q)
|
Javascript
<script>
function Queries(arr,N,Q)
{
let dq = [];
for (let i = 0; i < N; i++)
{
dq.push(arr[i]);
}
let sz = Q.length;
for (let i = 0; i < sz; i++)
{
if (Q[i][0] == 0)
{
let front = dq[0];
dq.shift();
dq.push(front);
}
else if (Q[i][0] == 1)
{
let back = dq[dq.length - 1];
dq.pop();
dq.unshift( back);
}
else if (Q[i][0] == 2)
{
dq[Q[i][1]] = Q[i][2];
}
else
{
document.write(dq[Q[i][1]] + " " );
}
}
}
let arr=[1, 2, 3, 4, 5];
let N = arr.length;
let Q = [[0], [1], [3, 1],
[2, 2, 54], [3, 2]];
Queries(arr, N, Q);
</script>
|
C#
using System;
public class Program
{
public static void Queries( int [] arr, int N, int [][] Q)
{
int [] dq = new int [N];
for ( int i = 0; i < N; i++)
{
dq[i] = arr[i];
}
int sz = Q.Length;
for ( int i = 0; i < sz; i++)
{
if (Q[i][0] == 0)
{
int front = dq[0];
for ( int j = 1; j < N; j++)
{
dq[j - 1] = dq[j];
}
dq[N - 1] = front;
}
else if (Q[i][0] == 1)
{
int back = dq[N - 1];
for ( int j = N - 1; j > 0; j--)
{
dq[j] = dq[j - 1];
}
dq[0] = back;
}
else if (Q[i][0] == 2)
{
dq[Q[i][1]] = Q[i][2];
}
else
{
Console.Write(dq[Q[i][1]] + " " );
}
}
}
public static void Main()
{
int [] arr = { 1, 2, 3, 4, 5 };
int N = arr.Length;
int [][] Q = { new int [] { 0 }, new int [] { 1 }, new int [] { 3, 1 },
new int [] { 2, 2, 54 }, new int [] { 3, 2 } };
Queries(arr, N, Q);
}
}
|
Time Complexity: O(N+|Q|)
Auxiliary Space: O(N)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...