1251. Mirror Multiset Lengths

Medium3.5 points estimatedHash TableCountingArray3 hints

A conveyor belt carries the items listed in nums. For a length k, compare the first k items with the last k items: they match when both groups hold exactly the same values with the same multiplicities, in any order. The two groups may overlap when k is more than half the belt.

Return an array holding every k from 1 to n, where n is the length of nums, for which the two groups match, in ascending order. Since k = n compares the whole array with itself, the result is never empty. Track count differences in a hash map together with a counter of non-zero entries. The intended solution runs in O(n) time and O(n) space.

Example 1

Input:
nums = [1,2,3,2,1]
Output:
[1,2,3,4,5]
Explanation:

The array reads the same backwards, so the first k and last k items hold the same values for every k from 1 to 5.

Example 2

Input:
nums = [1,2,3]
Output:
[3]
Explanation:

For k = 1 and k = 2 the first and last groups differ; only the whole array matches itself.

Example 3

Input:
nums = [4,4,4,4]
Output:
[1,2,3,4]
Explanation:

All items are equal, so every k from 1 to 4 matches.

Constraints

  • 1 ≤ nums.length ≤ 1000

  • -109 ≤ nums[i] ≤ 109

How this problem is judged

Answers
Your answer must match exactly. Numbers compare by value, so 2 and 2.0 are equal.

Expected complexity

Time
O(n)
Space
O(n)

What the author was aiming for. Your own solution is not measured against it.

Asked in an interview

Were you asked this in an interview? Say where, anonymously.

Code
Loading the editor…