File: make-two-arrays-equal-by-reversing-subarrays/solution.py

Date: 2026-06-06

Time: 17:31

Purpose

This file solves LeetCode 1460: Make Two Arrays Equal by Reversing Subarrays. It determines whether arr can be transformed into target using any number of subarray reversals.

The key insight is that reversing subarrays is equivalent to arbitrary reordering — you can sort any array using a sequence of subarray reversals (this is essentially selection sort). So the problem reduces to: do the two arrays contain the same elements with the same frequencies?

Key Components

Solution.makeTwoArraysEqualByReversingSubarrays — Takes two integer lists, returns a bool. The entire logic is a single-line multiset equality check via sorting.

Patterns

Dependencies

Imports: typing.List — used only for type annotations in the LeetCode function signature.

Imported by: The massive importedby list is misleading — it reflects the test runner infrastructure, not actual code coupling. Only make-two-arrays-equal-by-reversing-subarrays/testsolution.py actually tests this solution. The other test files import the same Solution pattern from their own directories.

Flow

1. Both target and arr are sorted independently (producing new lists, not mutating the originals).

2. The sorted lists are compared element-by-element via ==.

3. The boolean result is returned directly.

No iteration, no branching, no mutation.

Invariants

Error Handling

None. The function trusts the caller to provide valid inputs per LeetCode's contract. Passing arrays of different lengths returns False naturally (sorted lists of different lengths aren't equal).

Topics to Explore

Beliefs