Skip to content

Practice Count Good Triplets in an Array

Read the task, work through the examples, and check your approach against the constraints. Use the original LeetCode task for coding and submissions.

Problem Statement

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 pos1v as the index of the value v in nums1 and pos2v as the index of the value v in nums2, then a good triplet will be a set (x, y, z) where 0 <= x, y, z <= n - 1, such that pos1x < pos1y < pos1z and pos2x < pos2y < pos2z. Return the total number of good triplets.

Source: Count Good Triplets in an Array on LeetCode. Use the original task for its coding environment and submissions.

Examples

# Example 1

Input
nums1 = [2,0,1,3], nums2 = [0,1,2,3]
Output
1

# Example 2

Input
nums1 = [4,0,1,3,2], nums2 = [4,1,0,2,3]
Output
4

Constraints

n == nums1.length == nums2.length
3 <= n <= 10^5
0 <= nums1[i], nums2[i] <= n - 1
nums1 and nums2 are permutations of [0, 1, ..., n - 1].

Related LeetCode Problems

Practice more problems similar to Count Good Triplets in an Array.

Review your practice with PhantomCodeAI

After trying the problem yourself, use the app to discuss your approach, complexity, and edge cases.