Print the Array of size N containing values in range [0, M) after Q query updates
Given array arr[] of size N containing cyclic variables having states from 0 to (M – 1) (i.e when incremented from M-1 it goes to 0). The task is to fulfill the Q queries of which are of any of the following two types:
- 1st Type: 1 L R K – increment all the values in range of indices [L, R], K times cyclically.
- 2nd Type: 2 L R – print the updated values in range of indices [L, R].
Examples:
Input: arr[] = {2, 2, 7, 2, 5}, Q = 5, M = 8
queries[][] = {{1, 0, 3, 4},
{1, 4, 4, 2},
{1, 0, 0, 7},
{2, 1, 3},
{2, 3, 3}}
Output: {6, 3, 6}, {6}
Explanation: The states after performing each operation are:
After 1st: {6, 6, 3, 6, 5}
After 2nd: {6, 6, 3, 6, 7}
After 3rd: {5, 6, 3, 6, 7}
So in 4th query elements from index 1 to 3 and in 5th query element in index 3 are printed.Input: arr[] = [2, 3, 4, 5], Q = 3, M = 6
queries[][] = {{1, 0, 0, 3},
{1, 1, 2, 2},
{1, 3, 3, 1},
{2, 0, 3}}
Output: {5, 5, 1, 1}
Approach: The problem can be solved using greedy approach. Follow the steps mentioned below to implement the approach:
- When a query is of first type update all the values in range [L, R], K times.
- When a query is of second type then print the value in range [L, R].
Below is the implementation of the above approach.
C++14
// C++ code to implement above approach #include <bits/stdc++.h> using namespace std; // Function to implement the queries void update( int arr[], int N, int M, int Q, vector<vector< int > >& queries) { // Loop to implement the queries for ( int i = 0; i < Q; i++) { if (queries[i][0] == 1) { for ( int j = queries[i][1]; j <= queries[i][2]; j++) arr[j] = (arr[j] + queries[i][3]) % M; } else if (queries[i][0] == 2) { for ( int j = queries[i][1]; j <= queries[i][2]; j++) cout << arr[j] << " " ; cout << endl; } } } // Driver's code int main() { int N = 5, M = 8, Q = 5; int arr[] = { 2, 2, 7, 2, 5 }; vector<vector< int > > queries(Q); queries[0] = { 1, 0, 3, 4 }; queries[1] = { 1, 4, 4, 2 }; queries[2] = { 1, 0, 0, 7 }; queries[3] = { 2, 1, 3 }; queries[4] = { 2, 3, 3 }; update(arr, N, M, Q, queries); return 0; } |
Java
// Java code to implement the above approach import java.io.*; class GFG { // Function to implement the queries public static void update( int [] arr, int N, int M, int Q, int [][] queries) { // Loop to implement the queries for ( int i = 0 ; i < Q; i++) { if (queries[i][ 0 ] == 1 ) { for ( int j = queries[i][ 1 ]; j <= queries[i][ 2 ]; j++) arr[j] = (arr[j] + queries[i][ 3 ]) % M; } else if (queries[i][ 0 ] == 2 ) { for ( int j = queries[i][ 1 ]; j <= queries[i][ 2 ]; j++) System.out.print(arr[j]+ " " ); System.out.println(); } } } // Driver's code public static void main (String[] args) { int N = 5 , M = 8 , Q = 5 ; int [] arr = { 2 , 2 , 7 , 2 , 5 }; int [][] queries = new int [][] { new int [] { 1 , 0 , 3 , 4 }, new int [] { 1 , 4 , 4 , 2 }, new int [] { 1 , 0 , 0 , 7 }, new int [] { 2 , 1 , 3 }, new int [] { 2 , 3 , 3 } }; update(arr, N, M, Q, queries); } } // This code is contributed by Shubham Singh |
Python3
# Python3 code to implement above approach # Function to implement the queries def update(arr, N, M, Q, queries): # Loop to implement the queries for i in range (Q): if (queries[i][ 0 ] = = 1 ): for j in range (queries[i][ 1 ], queries[i][ 2 ] + 1 ): arr[j] = (arr[j] + queries[i][ 3 ]) % M elif (queries[i][ 0 ] = = 2 ): for j in range (queries[i][ 1 ], queries[i][ 2 ] + 1 ): print (arr[j], end = " " ) print () # Driver code if __name__ = = "__main__" : N = 5 M = 8 Q = 5 arr = [ 2 , 2 , 7 , 2 , 5 ] queries = [] queries.append([ 1 , 0 , 3 , 4 ]) queries.append([ 1 , 4 , 4 , 2 ]) queries.append([ 1 , 0 , 0 , 7 ]) queries.append([ 2 , 1 , 3 ]) queries.append([ 2 , 3 , 3 ]) update(arr, N, M, Q, queries) # This code is contributed by ukasp |
C#
// C# code to implement the above approach using System; public class GFG{ // Function to implement the queries public static void update( int [] arr, int N, int M, int Q, int [][] queries) { // Loop to implement the queries for ( int i = 0; i < Q; i++) { if (queries[i][0] == 1) { for ( int j = queries[i][1]; j <= queries[i][2]; j++) arr[j] = (arr[j] + queries[i][3]) % M; } else if (queries[i][0] == 2) { for ( int j = queries[i][1]; j <= queries[i][2]; j++) Console.Write(arr[j]+ " " ); Console.WriteLine(); } } } // Driver's code public static void Main() { int N = 5, M = 8, Q = 5; int [] arr = { 2, 2, 7, 2, 5 }; int [][] queries = new int [][] { new int [] { 1, 0, 3, 4 }, new int [] { 1, 4, 4, 2 }, new int [] { 1, 0, 0, 7 }, new int [] { 2, 1, 3 }, new int [] { 2, 3, 3 } }; update(arr, N, M, Q, queries); } } // This code is contributed by Shubham Singh |
Javascript
<script> // JavaScript code for the above approach // Function to implement the queries function update(arr, N, M, Q, queries) { // Loop to implement the queries for (let i = 0; i < Q; i++) { if (queries[i][0] == 1) { for (let j = queries[i][1]; j <= queries[i][2]; j++) arr[j] = (arr[j] + queries[i][3]) % M; } else if (queries[i][0] == 2) { for (let j = queries[i][1]; j <= queries[i][2]; j++) document.write(arr[j] + " " ); document.write( '<br>' ) } } } // Driver's code let N = 5, M = 8, Q = 5; let arr = [2, 2, 7, 2, 5]; let queries = new Array(Q); queries[0] = [1, 0, 3, 4]; queries[1] = [1, 4, 4, 2]; queries[2] = [1, 0, 0, 7]; queries[3] = [2, 1, 3]; queries[4] = [2, 3, 3]; update(arr, N, M, Q, queries); // This code is contributed by Potta Lokesh </script> |
6 3 6 6
Time Complexity: O(Q*N)
Auxiliary Space: O(1)