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;
   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++;
   }
   
   // getting the size of the first window of the given size 
   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 or given subarray with size " + size + " after " + rotations + " number of rotations is " + ans);
}

// defining array 
var arr= [5, 7, 1, 4, 3, 8, 2]

// defining quries 
var queries = [[3,3], [2,4]]

// traversing over the array 
for(var i =0; i<queries.length; i++){
   subSum(arr, queries[i][0], queries[i][1]);
}

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

This problem can be solved in an efficient way by using the sliding window approach. Let us directly move to the code of this problem and an get overview through it −

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;
   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++;
   }
   
   // getting the size of the first window of the given size 
   var ans = -1000000000
   var cur = 0;
   for(var i = 0;i<size;i++){
      cur += temp[i];
   }
   ans = cur;
   for(var i = size; i<n;i++){
      cur -= temp[i-size];
      cur += temp[i];
      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 quries 
var queries = [[3,3], [2,4]]

// traversing over the array 
for(var i =0; i<queries.length; i++){
   subSum(arr, queries[i][0], queries[i][1]);
}

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.

Conclusion

In this tutorial, we have implemented a JavaScript program for queries to find the maximum sum of contiguous subarrays of a given length in a rotating array. We have implemented a naive approach with O(N*Q*D) time complexity and then improved it by using the sliding window’s concept to O(N*Q) time complexity, but space complexity of both the codes is same O(N).

Updated on: 14-Apr-2023

96 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements