You are given two 0-indexed arrays nums1 and nums2 of length n, both of which are permutations of [0, 1, ..., n - 1].

A good triplet is a set of 3 distinct values which are present in increasing order by position both in nums1 and nums2. In other words, if we consider pos1[v] as the index of value v in nums1 and pos2[v] as the index of value v in nums2, then a good triplet will be a set (x, y, z) where 0 <= x, y, z <= n - 1, such that:

  • pos1[x] < pos1[y] < pos1[z] AND
  • pos2[x] < pos2[y] < pos2[z]

Return the total number of good triplets.

Example: If nums1 = [2, 0, 1, 3] and nums2 = [0, 1, 2, 3], then value 0 appears at position 1 in nums1 and position 0 in nums2. The triplet (0, 1, 3) is good because positions in nums1 are [1, 2, 3] (increasing) and positions in nums2 are [0, 1, 3] (also increasing).

Input & Output

example_1.py โ€” Basic Case
$ Input: nums1 = [2,0,1,3], nums2 = [0,1,2,3]
โ€บ Output: 1
๐Ÿ’ก Note: The only good triplet is (0,1,3). In nums1, positions are [1,2,3] (increasing). In nums2, positions are [0,1,3] (also increasing).
example_2.py โ€” Multiple Triplets
$ Input: nums1 = [4,0,1,3,2], nums2 = [4,1,0,2,3]
โ€บ Output: 4
๐Ÿ’ก Note: Good triplets are: (4,0,3), (4,0,2), (4,1,3), (4,1,2). All maintain increasing order in both arrays.
example_3.py โ€” Edge Case - Small Array
$ Input: nums1 = [0,1,2], nums2 = [2,1,0]
โ€บ Output: 0
๐Ÿ’ก Note: No valid triplets exist because the arrays have completely reversed order.

Constraints

  • n == nums1.length == nums2.length
  • 3 โ‰ค n โ‰ค 105
  • 0 โ‰ค nums1[i], nums2[i] โ‰ค n - 1
  • nums1 and nums2 are permutations of [0, 1, ..., n - 1]
  • All values in each array are distinct

Visualization

Tap to expand
๐ŸŽญ Concert Venue AnalysisFinding VIP guest triplets with consistent seating orderVenue 1: [2,0,1,3] โ†’ positions: Guest 2@0, Guest 0@1, Guest 1@2, Guest 3@3Venue 2: [0,1,2,3] โ†’ positions: Guest 0@0, Guest 1@1, Guest 2@2, Guest 3@3๐ŸŸ๏ธ Venue 1 Seating2013Pos 0Pos 1Pos 2Pos 3๐ŸŸ๏ธ Venue 2 Seating0123Pos 0Pos 1Pos 2Pos 3๐ŸŽฏ Valid Triplet Found: (0, 1, 3)Venue 1 order: Guest 0 (pos 1) โ†’ Guest 1 (pos 2) โ†’ Guest 3 (pos 3) โœ“Venue 2 order: Guest 0 (pos 0) โ†’ Guest 1 (pos 1) โ†’ Guest 3 (pos 3) โœ“Both venues maintain increasing position order!
Understanding the Visualization
1
Map Guest Positions
Create a mapping of where each VIP guest sits in both venues
2
Transform Coordinates
Convert venue 2 positions into venue 1's coordinate system
3
Count Valid Pairs
For each potential middle guest, count valid left and right partners
4
Calculate Combinations
Multiply left and right counts for each middle position
Key Takeaway
๐ŸŽฏ Key Insight: Transform the coordinate system and use Binary Indexed Trees to efficiently count valid left and right elements for each potential middle position, achieving O(n log n) complexity.
Asked in
Google 45 Meta 32 Amazon 28 Microsoft 18
52.3K Views
Medium-High Frequency
~35 min Avg. Time
1.8K Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen