BackmediumArraysMicrosoft

Subarray Rotation Checker Solution

Problem Statement

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.

Example 1
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.

Example 2
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
Live Compiler1 Free Run Available
Loading Editor...
Test Cases & Output
Click "Run" to test your 1 free compile trial!

🚀 Practice this problem

Run code, get AI hints & track streak

Sign Up Free

Subarray Rotation Checker — Problem Statement & Solution Guide

ArraysMediumFundamentals
TimeO(n)
|
SpaceO(1)

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

Example 1

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.

Example 2

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

JavaScript Solution
Time: O(n)
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;
   }

Asked in Top Tech Interviews

Microsoft

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.