BackmediumArraysOracleAtlassian

Array Rotation Alignment Solution

Problem Statement

Given two arrays of integers sequenceA and sequenceB, determine if sequenceB can be transformed into sequenceA by applying a series of rotations. If possible, find the minimum number of rotations required.

Example 1
Input
[1, 2, 3, 4, 5, 6, 7] and [7, 1, 2, 3, 4, 5, 6]
Output
{"identical": false, "rotations": 6}

Explanation: Step-by-step: Rotate sequenceB to the left by 6 positions to get sequenceA. This is the minimum number of rotations required.

Example 2
Input
[1, 2, 3, 4, 5, 6, 7] and [3, 4, 5, 6, 7, 1, 2]
Output
{"identical": false, "rotations": -1}

Explanation: Step-by-step: No valid rotation exists to transform sequenceB into sequenceA.

Constraints

  • The sequences will have a length between 2 and 100 elements.
  • All elements in the sequences are integers between 1 and 1000.
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

Array Rotation Alignment — Problem Statement & Solution Guide

ArraysMediumpattern recognition and rotation
TimeO(n)
|
SpaceO(1)

Problem Description

Given two arrays of integers sequenceA and sequenceB, determine if sequenceB can be transformed into sequenceA by applying a series of rotations. If possible, find the minimum number of rotations required.

Examples

Example 1

Input

[1, 2, 3, 4, 5, 6, 7] and [7, 1, 2, 3, 4, 5, 6]

Output

{"identical": false, "rotations": 6}

Explanation: Step-by-step: Rotate sequenceB to the left by 6 positions to get sequenceA. This is the minimum number of rotations required.

Example 2

Input

[1, 2, 3, 4, 5, 6, 7] and [3, 4, 5, 6, 7, 1, 2]

Output

{"identical": false, "rotations": -1}

Explanation: Step-by-step: No valid rotation exists to transform sequenceB into sequenceA.

Constraints

  • The sequences will have a length between 2 and 100 elements.
  • All elements in the sequences are integers between 1 and 1000.

Optimal Approach & Strategy

The optimal approach involves using the fact that if two sequences can be made identical by rotation, then one must be a rotation of the other. We can check this by concatenating one sequence with itself and checking if the other sequence is a substring of the concatenated sequence.

Brute Force Approach

The brute-force approach involves trying all possible rotations of one sequence and checking if it matches the other sequence. This can be done using nested loops to generate all rotations and compare them. However, this approach has a time complexity of O(n²).

Verified Code Solutions

JavaScript Solution
Time: O(n)
function arrayRotationAlignment(sequenceA, sequenceB) {
  if (sequenceA.length !== sequenceB.length) return { identical: false, rotations: -1 };
  let rotations = 0;
  for (let i = 0; i < sequenceA.length; i++) {
    if (sequenceA[i] !== sequenceB[(i - rotations + sequenceA.length) % sequenceA.length]) {
      rotations = -1;
      break;
    }
  }
  return { identical: true, rotations: rotations === -1 ? -1 : rotations % sequenceA.length === 0 ? 0 : 1 };
}

Asked in Top Tech Interviews

OracleAtlassian

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.