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 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.
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.
Example 2
Input Given array: [1, 2, 3, 4, 5, 6] Query: [8, 0, 3] Output: 18
Explanation
The number of rotations is 8. Since 8 % 6 = 2, this is equivalent to 2 rotations (after the length of the array number of rotations, the same array reappears). 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 physically rotate the array by the given number of positions and then calculate the sum of elements in the specified range.
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
The sum of the elements in the range 1 to 4 after 3 rotations is 14
The sum of the elements in the range 0 to 3 after 8 rotations is 18
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 space complexity is O(N), as we are creating a new array of size N.
Optimized Prefix Sum Approach
In the prefix sum approach, we create a prefix sum array where each index contains the sum of all elements up to that index. This allows us to calculate range sums efficiently without physically rotating the array.
Example
// function to answer the queries using prefix sum
function getSumOptimized(preSum, rotations, L, R, originalL, originalR) {
var len = preSum.length;
var rot = rotations % len;
// updating L and R based on rotation
var newL = (L + rot) % len;
var newR = (R + rot) % len;
var sum = 0;
if (newL
The sum of the elements in the range 1 to 4 after 3 rotations is 14
The sum of the elements in the range 0 to 3 after 8 rotations is 18
Time and Space Complexity
The time complexity of the optimized approach is O(Q) where Q is the number of queries, as each query is answered in constant time. The space complexity is O(N) for storing the prefix sum array.
Comparison
| Approach | Time Complexity | Space Complexity | Advantages |
|---|---|---|---|
| Naive | O(Q*N) | O(N) | Simple to understand |
| Prefix Sum | O(Q) | O(N) | Efficient for multiple queries |
Conclusion
We have implemented two approaches for range sum queries on anticlockwise rotated arrays. The prefix sum approach is significantly more efficient with O(Q) time complexity compared to the naive O(Q*N) approach, making it ideal for handling multiple queries efficiently.
