- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Rearrange and Update Array Elements as Specified by the given Queries
In this problem, we will perform the given queries on the array elements. The queries contain the circular left rotation, right rotation, and updation of the array element.
The logical part of solving the problem is array rotation. The naïve approach to rotating the array in the left direction is to replace each element with the next element and the last element with the first element.
We can use the deque data structure to rotate the array efficiently.
Problem statement − We have given an arr[] array containing the integer values. Also, we have given a queries[] array containing K queries. We need to perform each query given in the queries[] on arr[] array elements according to the below rules.
{0} − Make a circular left rotation of the array.
{1) − Make a circular right rotation of the array.
{2, p, q} − Update the element at the pth index with the q.
{3, p} − Print element from the pth index.
Sample examples
Input
arr[] = {8, 9, 13, 44, 76, 67, 21, 51}; queries = {{1}, {0}, {2, 4, 50}, {3, 2}, {2, 2, 223}, {3, 2}};
Output
13,223
Explanation − Let’s perform each query.
{1} −> After rotating the array in the right direction, the array becomes {51, 8, 9, 13, 44, 76, 67, 21}
{0} −> After rotating the updated array in the left direction, the array becomes equal to the {8, 9, 13, 44, 76, 67, 21, 51}.
{2, 4, 50} −> After updating the element at index 4th with 50, the array becomes {8, 9, 13, 44, 50, 67, 21, 51}
{3, 2} −> It prints the element from the 2nd index.
{2, 2, 223}−> It updates the element at the 2nd index with 223, and the array becomes {8, 9, 223, 44, 50, 67, 21, 51}.
{3, 2} −> It prints the element from the 2nd index.
Input
arr[] = {3, 2, 1}, {{3, 2}, {3, 0}}
Output
1,3
Explanation − It prints the array from the 2nd and 0th index.
Input
arr[] = {76,20,51,78}, queries={{1},{1},{3, 1}}
Output
78
Explanation − After rotating the array in the right direction by 2 times, the array becomes [51, 78, 76, 20]. The element at the first index is 78.
Approach 1
In this approach, we will traverse each query and perform the operations according to the given query. We will replace each element of the array with the next element to rotate it in the left direction and replace each element with the previous element to rotate in the right direction.
Algorithm
Step 1− Start traversing through each query.
Step 2− If queries[p][0] equals 0, follow the steps below.
Step 2.1− Initialize the ‘temp’ variable with the first element of the array.
Step 2.2− Start traversing the array, and replace each element with the next element.
Step 2.3− Replace the last element with the ‘temp’ value.
Step 3− If queries[p][0] equals 1, follow the steps below.
Step 3.1− Store the last element of the array in the ‘temp’ variable.
Step 3.2− Start traversing the array, and replace each element with the previous element.
Step 3.3− Update the first element with the ‘temp’ value.
Step 4− If queries[p][0] is 2, update the array element at the given index with the given value.
Step 5− If queries[p][0] is 3, print the array value from the given index.
Example
#include <bits/stdc++.h> using namespace std; void performQueries(int arr[], int N, vector<vector<int>> &queries) { int len = queries.size(); for (int p = 0; p < len; p++) { // For left shift if (queries[p][0] == 0) { // left shift array int temp = arr[0]; for (int p = 0; p < N - 1; p++){ arr[p] = arr[p + 1]; } arr[N - 1] = temp; } // For the right shift else if (queries[p][0] == 1) { // Right shift array int temp = arr[N - 1]; for (int p = N - 1; p > 0; p--){ arr[p] = arr[p - 1]; } arr[0] = temp; } // For updating the value else if (queries[p][0] == 2) { arr[queries[p][1]] = queries[p][2]; } // For printing the value else { cout << arr[queries[p][1]] << " "; } } } int main() { int arr[] = {8, 9, 13, 44, 76, 67, 21, 51}; int N = sizeof(arr) / sizeof(arr[0]); vector<vector<int>> queries; queries = {{1}, {0}, {2, 4, 50}, {3, 2}, {2, 2, 223}, {3, 2}}; performQueries(arr, N, queries); return 0; }
Output
13 223
Time complexity − O(N*K), to traverse the queries and rotate the array.
Space complexity − O(1), as we use the constant space.
Approach 2
In this approach, we will use the deque to store the array element. After that, to rotate the array by left, we can pop the front element from the queue and push it at the end of the queue. Similarly, we can rotate the array in the right direction.
Algorithm
Step 1− Define deque and push all array elements into the queue.
Step 2− Travese through each queries using the for loop.
Step 3− To rotate the array to the left, remove the first element from the start of the queue, and push it to the end of the queue.
Step 4− To rotate the array in the right direction, remove an element from the end of the queue, and push the element at the start.
Step 5− Update the element or print the element value according to the given query.
Example
#include <bits/stdc++.h> using namespace std; void performQueries(int arr[], int N, vector<vector<int>> &queries) { // Queue to insert array elements deque<int> que; // Add elements to queue for (int p = 0; p < N; p++) { que.push_back(arr[p]); } // total queries int len = queries.size(); for (int p = 0; p < len; p++) { // For left shift if (queries[p][0] == 0) { // Get the first element int temp = que[0]; // Remove the first element que.pop_front(); // Push element at the last que.push_back(temp); } // For the right shift else if (queries[p][0] == 1) { // Get the last element int temp = que[N - 1]; // remove the last element que.pop_back(); // Insert element at the start que.push_front(temp); } // For updating the value else if (queries[p][0] == 2) { que[queries[p][1]] = queries[p][2]; } // For printing the value else { cout << que[queries[p][1]] << " "; } } } int main() { int arr[] = {8, 9, 13, 44, 76, 67, 21, 51}; int N = sizeof(arr) / sizeof(arr[0]); vector<vector<int>> queries; queries = {{1}, {0}, {2, 4, 50}, {3, 2}, {2, 2, 223}, {3, 2}}; performQueries(arr, N, queries); return 0; }
Output
13 223
Time complexity − O(N+K) to insert array elements into the queue.
Space complexity − O(N) to store elements into the deque.
The deque data structure allows us to perform the right and left rotation operations in the O(1) time. So, it improves the efficiency of the code for performing the given queries.