JavaScript Program for Range sum queries for anticlockwise rotations of Array by K indices

Anticlockwise rotation of an array means rotating all the elements of the given array to their left side by the given number of indexes. In this article, we will implement a JavaScript program for range sum queries for anticlockwise rotations of the array by k indices.

Introduction to Problem

In this problem, we are given an array that will contain some integers and another array that will contain the values in the pairwise form. Each pair will be the number of rotations we need for the current query and after a given number of rotations, we will be given a range and have to answer the sum of elements present in that given range. For example,

For Example 1

Input
Given array: [1, 2, 3, 4, 5, 6]
Query: [3, 1, 4]
Output 14


Explanation

The number of rotations is 3 so the array after 3 rotations is 4 5 6 1 2 3.

In the range 1 to 4 elements are 5, 6, 1, and 2. So, the sum is 14.

For Example 2

Input
Given array: [1, 2, 3, 4, 5, 6]
Query: [8, 0, 3]
Output 18


Explanation

The number of rotations is 8 so the array after 8 rotations are equal to the 8 %(length of the array) rotations because, after the length of the array number of rotations, the same array again appears means 8 rotations is equivalent to 2 rotations.

So, the array after 8 rotations is 3 4 5 6 1 2.

In the range, 0 to 3 elements are 3, 4, 5, and 6. So, the sum is 18.

Naive Approach

In the naive approach, we will just simply do all the steps which are said in the query array. Like, it is given to rotating the array, then we will rotate the array elements by the given number of times and then check the sum of the elements in the range. Let us see its code −

Example

// function to answer the queries
function getSum(arr, rotations, L, R){
var len = arr.length
var rot = rotations % len;
var temp = new Array(len);

// rotating the given array
for(var i =0;  i< len - rot; i++ ){
temp[i] = arr[i + rot];
}

// getting the last elements
for(var i = 0; i < rot; i++)    {
temp[len-rot+i] = arr[i];
}

// getting the required sum
var sum = 0;
for(var i = L; i<=R; i++){
sum += temp[i];
}
console.log("The sum of the elements in the range " + L + " to " + R + " after " + rotations + " number of rotations is " + sum);
}

// defining the array
var arr = [ 1, 2, 3, 4, 5, 6]

// defining the queries array
var queries = [ [ 3, 1, 4], [ 8, 0, 3]]

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


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 size of the array.

The time complexity of the above code is O(N), as we are creating a new array of size N.

Prefix Sum Approach

In the prefix sum approach, we are going to create a prefix sum array and each index of the prefix sum array contains the sum of all the elements up to current index. Let us see its code −

Example

// function to answer the queries
function getSum(preSum, rotations, L, R){
var len = preSum.length
var rot = rotations % len;

// updating L and R
L = (L + rot) %len
R = (R + rot) %len
var sum = 0;
if(L <= R) {
if(L == 0) {
sum = preSum[R];
}
else{
sum = preSum[R]-preSum[L-1];
}
}
else{
sum += preSum[R];
sum += preSum[len-1]-preSum[L-1];
}
console.log("The sum of the elements in the range " + L + " to " + R + " after " + rotations + " number of rotations is " + sum);
}

// defining the array
var arr = [ 1, 2, 3, 4, 5, 6]
var preSum = new Array(arr.length)
preSum[0] = arr[0]
for(var i = 1; i<arr.length; i++){
preSum[i] = preSum[i-1] + arr[i]
}

// defining the quries array
var queries = [ [ 3, 1, 4], [ 8, 0, 3]]

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


Time and Space Complexity

The time complexity of the above code is O(Q) where Q is the number of queries.

The time complexity of the above code is O(N), as we are creating a new array to store the prefix sum of the array elements.

Conclusion

In this tutorial, we have implemented a JavaScript program for range sum queries for anticlockwise rotations of the array by k indices. Anticlockwise rotation of an array means rotating all the elements of the given array to their left side by the given number of indexes. We have implemented two approaches first, was the naive approach with O(Q*N) time complexity and another was the prefix sum approach with O(Q) time complexity.

Updated on: 14-Apr-2023

73 Views