Maximum Number of Matching Indices After Right Shifts - Problem
You are given two integer arrays, nums1 and nums2, of the same length.
An index i is considered matching if nums1[i] == nums2[i].
Return the maximum number of matching indices after performing any number of right shifts on nums1.
A right shift is defined as shifting the element at index i to index (i + 1) % n, for all indices.
Input & Output
Example 1 — Perfect Match After One Shift
$
Input:
nums1 = [1,2,3], nums2 = [3,1,2]
›
Output:
3
💡 Note:
After 1 right shift, nums1 becomes [3,1,2] which perfectly matches nums2, giving us 3 matching indices.
Example 2 — Partial Matches
$
Input:
nums1 = [1,2,3,4], nums2 = [4,3,2,1]
›
Output:
2
💡 Note:
After 3 right shifts, nums1 becomes [2,3,4,1]. This matches nums2 at indices 1 and 2, giving us 2 matches.
Example 3 — No Improvement
$
Input:
nums1 = [1,1,1], nums2 = [2,2,2]
›
Output:
0
💡 Note:
No elements match regardless of rotation since all elements in nums1 are 1 and all in nums2 are 2.
Constraints
- 1 ≤ nums1.length, nums2.length ≤ 100
- nums1.length == nums2.length
- 1 ≤ nums1[i], nums2[i] ≤ 100
Visualization
Tap to expand
Understanding the Visualization
1
Input Arrays
nums1=[1,2,3], nums2=[3,1,2] - initially 0 matches
2
Apply Right Shift
1 right shift moves each element one position right (with wraparound)
3
Count Matches
After shift: [3,1,2] matches [3,1,2] perfectly = 3 matches
Key Takeaway
🎯 Key Insight: Try all n possible rotations to find the alignment that maximizes matching indices
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code