Subarray Rotation Checker — Problem Statement & Solution Guide
Problem Description
Given an array of integers and two pointers, left and right, representing a subarray, determine if there exists a rotation of the subarray such that the sum of the absolute differences between consecutive elements is minimized.
Examples
Input
[1, 2, 3, 4, 5], 1, 3
Output
true
Explanation: Step-by-step: Given the subarray [1, 2, 3, 4, 5] and the rotation [2, 3, 4, 5, 1], we calculate the sum of absolute differences between consecutive elements: |2-3| + |3-4| + |4-5| + |5-1| + |1-2| = 10. This is the minimum possible sum, so the function returns true.
Input
[1, 2, 3, 4, 5], 2, 4
Output
false
Explanation: Step-by-step: Given the subarray [1, 2, 3, 4, 5] and the rotation [3, 4, 5, 1, 2], we calculate the sum of absolute differences between consecutive elements: |3-4| + |4-5| + |5-1| + |1-2| + |2-3| = 12. This is not the minimum possible sum, so the function returns false.
Constraints
- 1 <= array length <= 10^5
- 0 <= left < right < array length
- All elements in the array are integers between -10^5 and 10^5
- The input array is not empty
Optimal Approach & Strategy
An optimized approach involves utilizing the properties of the sum of absolute differences and rotational symmetry to minimize the number of calculations required. By considering how the sum changes with each rotation, we can find an efficient algorithm to determine the existence of a rotation that minimizes the sum of absolute differences.
Brute Force Approach
A brute-force approach involves checking all possible rotations of the subarray and calculating the sum of absolute differences for each rotation. However, this approach has a high time complexity due to the nested loops required to generate and evaluate all rotations. It is not efficient for large inputs.
Verified Code Solutions
function solution(nums, left, right) {
const n = right - left + 1;
let minSum = Infinity;
for (let i = 0; i < n; i++) {
let sum = 0;
for (let j = 0; j < n; j++) {
const index = (left + i + j) % n;
sum += Math.abs(nums[index] - nums[(left + i + j + 1) % n]);
}
minSum = Math.min(minSum, sum);
}
return minSum === 0;
}class Solution {
public:
bool solution(vector<int>& nums, int left, int right) {
int n = right - left + 1;
int minSum = INT_MAX;
for (int i = 0; i < n; i++) {
int sum = 0;
for (int j = 0; j < n; j++) {
int index = (left + i + j) % n;
sum += abs(nums[index] - nums[(left + i + j + 1) % n]);
}
minSum = min(minSum, sum);
}
return minSum == 0;
}
};class Solution {
public boolean solution(int[] nums, int left, int right) {
int n = right - left + 1;
int minSum = Integer.MAX_VALUE;
for (int i = 0; i < n; i++) {
int sum = 0;
for (int j = 0; j < n; j++) {
int index = (left + i + j) % n;
sum += Math.abs(nums[index] - nums[(left + i + j + 1) % n]);
}
minSum = Math.min(minSum, sum);
}
return minSum == 0;
}
}def solution(nums, left, right):
n = right - left + 1
min_sum = float('inf')
for i in range(n):
sum_ = 0
for j in range(n):
index = (left + i + j) % n
sum_ += abs(nums[index] - nums[(left + i + j + 1) % n])
min_sum = min(min_sum, sum_)
return min_sum == 0function solution(nums, left, right) {
const n = right - left + 1;
let minSum = Infinity;
for (let i = 0; i < n; i++) {
let sum = 0;
for (let j = 0; j < n; j++) {
const index = (left + i + j) % n;
sum += Math.abs(nums[index] - nums[(left + i + j + 1) % n]);
}
minSum = Math.min(minSum, sum);
}
return minSum === 0;
}Asked in Top Tech Interviews
Solve in Interative Editor
Ready to test your code? Open our built-in compiler, run custom test suites, and see detailed complexity analysis reports instantly.