Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
JavaScript Program for Queries to find the maximum sum of contiguous subarrays of a given length in a rotating array
Rotating array means we will be given a number and we have to move the elements of the array in cyclic order in either the right or left direction. Here we are not specified so we will use the right rotation as the standard and after the given number of rotations, we will return the subarrays with the maximum sum. We will see the code with the proper explanation in the article.
Introduction to Problem
In this problem, we are given an array that contains the integers and another array that contains the pairs of queries. Each index of the queries array contains two integers first indicates the number of times the current array rotates and the second integer indicates the length of the required subarray. For example ?
If the given array is [ 5, 7, 1, 4, 3, 8, 2] and the queries are as follows ?
Queries: 3 rotations and size 3 After the three rotations, the array looks like: 3, 8, 2, 5, 7, 1, 4 From the above array, the result is 15 by subarray: 8, 2, and 5. Queries: 2 rotations and size 4 After the two rotations, the array looks like: 8, 2, 5, 7, 1, 4, 3 From the above array, the result is 22 by subarrays 8, 2, 5, and 7
Let us move to the approach to solving this problem
Naive Approach
The naive approach is straight in which we are going to implement the given problem by using two for loops. First, we will move over the array and rotate it in a clockwise manner a given number of times. Then we find the subarray with the given size and the subarray which have the largest sum. Let's see its code ?
Example
// function to rotate the array and find the subarray sum
function subSum(arr, rotations, size){
var n = arr.length
var temp = new Array(n)
var j = 0;
// Rotate array to the right by 'rotations' positions
for(var i = n-rotations; i<n; i++){
temp[j] = arr[i];
j++;
}
for(var i = 0; i < n-rotations; i++){
temp[j] = arr[i];
j++;
}
// Find maximum sum subarray of given size using brute force
var ans = -1000000000;
for(var i = 0; i<=n-size; i++) {
var cur = 0;
for(var j = i; j < i+size; j++) {
cur += temp[j];
}
if(ans < cur) {
ans = cur;
}
}
console.log("The maximum sum of given subarray with size " + size + " after " + rotations + " number of rotations is " + ans);
}
// defining array
var arr = [5, 7, 1, 4, 3, 8, 2]
// defining queries
var queries = [[3,3], [2,4]]
// traversing over the queries
for(var i = 0; i < queries.length; i++){
subSum(arr, queries[i][0], queries[i][1]);
}
The maximum sum of given subarray with size 3 after 3 number of rotations is 15 The maximum sum of given subarray with size 4 after 2 number of rotations is 22
Time and Space Complexity
The time complexity of the above code is O(Q*D*N), where Q is the number of queries, D is the size of each required subarray and N is the length of the array.
The space complexity of the above code is O(N), as we are using an extra array to store the rotated array.
Efficient Approach (Sliding Window)
This problem can be solved efficiently by using the sliding window approach. Instead of calculating the sum for each window from scratch, we can slide the window and adjust the sum by removing the leftmost element and adding the new rightmost element.
Example
// function to rotate the array and find the subarray sum using sliding window
function subSum(arr, rotations, size){
var n = arr.length
var temp = new Array(n)
var j = 0;
// Rotate array to the right by 'rotations' positions
for(var i = n-rotations; i<n; i++){
temp[j] = arr[i];
j++;
}
for(var i = 0; i < n-rotations; i++){
temp[j] = arr[i];
j++;
}
// Calculate sum of first window
var ans = -1000000000
var cur = 0;
for(var i = 0; i<size; i++){
cur += temp[i];
}
ans = cur;
// Slide the window and update maximum sum
for(var i = size; i<n; i++){
cur -= temp[i-size]; // Remove leftmost element
cur += temp[i]; // Add new rightmost element
if(ans < cur) {
ans = cur;
}
}
console.log("The maximum sum of given subarray with size " + size + " after " + rotations + " number of rotations is " + ans);
}
// defining array
var arr = [5, 7, 1, 4, 3, 8, 2]
// defining queries
var queries = [[3,3], [2,4]]
// traversing over the queries
for(var i = 0; i < queries.length; i++){
subSum(arr, queries[i][0], queries[i][1]);
}
The maximum sum of given subarray with size 3 after 3 number of rotations is 15 The maximum sum of given subarray with size 4 after 2 number of rotations is 22
Time and Space Complexity
The time complexity of the above code is O(Q*N), where Q is the number of queries and N is the length of the array.
The space complexity of the above code is O(N), as we are using an extra array to store the rotated array.
Comparison
| Method | Time Complexity | Space Complexity | Approach |
|---|---|---|---|
| Naive Approach | O(Q*D*N) | O(N) | Brute force for each window |
| Sliding Window | O(Q*N) | O(N) | Efficient window sliding |
Conclusion
In this tutorial, we implemented a JavaScript program for queries to find the maximum sum of contiguous subarrays of a given length in a rotating array. The sliding window approach significantly improves time complexity from O(N*Q*D) to O(N*Q) while maintaining the same space complexity.
