Java Program for Queries to find maximum sum contiguous subarrays of given length in a rotating array
Given an array arr[] of N integers and Q queries of the form {X, Y} of the following two types:
- If X = 1, rotate the given array to the left by Y positions.
- If X = 2, print the maximum sum subarray of length Y in the current state of the array.
Examples:Â
Input: N = 5, arr[] = {1, 2, 3, 4, 5}, Q = 2, Query[][] = {{1, 2}, {2, 3}}
Output:
Query 1: 3 4 5 1 2
Query 2: 12
Explanation:
Query 1: Shift array to the left 2 times: {1, 2, 3, 4, 5} -> {2, 3, 4, 5, 1} -> {3, 4, 5, 1, 2}
Query 2: Maximum sum subarray of length 3 is {3, 4, 5} and the sum is 12
Input: N = 5, arr[] = {3, 4, 5, 1, 2}, Q = 3, Query[][] = {{1, 3}, {1, 1}, {2, 4}}
Output:Â
Query 1: 1 2 3 4 5
Query 2: 2 3 4 5 1
Query 3: 14
Explanation:
Query 1: Shift array to the left 3 times: {3, 4, 5, 1, 2} -> {4, 5, 1, 2, 3} -> {5, 1, 2, 3, 4} -> {1, 2, 3, 4, 5}
Query 2: Shift array to the left 1 time: {1, 2, 3, 4, 5} -> {2, 3, 4, 5, 1}
Query 3: Maximum sum subarray of length 4 is {2, 3, 4, 5} and sum is 14
Naive Approach: The simplest approach is to rotate the array by shifting elements one by one up to distance Y for queries is of type 1 and generating the sum of all the subarrays of length Y and print the maximum sum if the query is of type 2.Â
Time Complexity: O(Q*N*Y)
Auxiliary Space: O(N)
Efficient Approach: To optimize the above approach, the idea is to use the Juggling Algorithm for array rotation and for finding the maximum sum subarray of length Y, use the Sliding Window Technique. Follow the steps below to solve the problem:
- If X = 1, rotate the array by Y, using the Juggling Algorithm.
- Otherwise, if X = 2, find the maximum sum subarray of length Y using the Sliding Window Technique.
- Print the array if query X is 1.
- Otherwise, print the maximum sum subarray of size Y.
Below is the implementation of the above approach:
Java
class GFG{
static int MaxSum( int []arr,
int n, int k)
{
int i, max_sum = 0 , sum = 0 ;
for (i = 0 ; i < k; i++)
{
sum += arr[i];
}
max_sum = sum;
while (i < n)
{
sum = sum - arr[i - k] +
arr[i];
if (max_sum < sum)
{
max_sum = sum;
}
i++;
}
return max_sum;
}
static int gcd( int n1, int n2)
{
if (n2 == 0 )
{
return n1;
}
else
{
return gcd(n2, n1 % n2);
}
}
static int []RotateArr( int []arr,
int n, int d)
{
int i = 0 , j = 0 ;
d = d % n;
int no_of_sets = gcd(d, n);
for (i = 0 ; i < no_of_sets; i++)
{
int temp = arr[i];
j = i;
while ( true )
{
int k = j + d;
if (k >= n)
k = k - n;
if (k == i)
break ;
arr[j] = arr[k];
j = k;
}
arr[j] = temp;
}
return arr;
}
static void performQuery( int []arr,
int Q[][], int q)
{
int N = arr.length;
for ( int i = 0 ; i < q; i++)
{
if (Q[i][ 0 ] == 1 )
{
arr = RotateArr(arr, N,
Q[i][ 1 ]);
for ( int t : arr)
{
System.out.print(t + " " );
}
System.out.print("
");
}
else
{
System.out.print(MaxSum(arr, N,
Q[i][ 1 ]) + "
");
}
}
}
public static void main(String[] args)
{
int []arr = { 1 , 2 , 3 , 4 , 5 };
int q = 5 ;
int Q[][] = {{ 1 , 2 }, { 2 , 3 },
{ 1 , 3 }, { 1 , 1 },
{ 2 , 4 }};
performQuery(arr, Q, q);
}
}
|
Output:Â
3 4 5 1 2
12
1 2 3 4 5
2 3 4 5 1
14
Time Complexity: O(Q*N), where Q is the number of queries, and N is the size of the given array.
Auxiliary Space: O(N)
Please refer complete article on Queries to find maximum sum contiguous subarrays of given length in a rotating array for more details!
Last Updated :
27 Jan, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...