Watch 10 video solutions for Count Good Triplets in an Array, a hard level problem involving Array, Binary Search, Divide and Conquer. This walkthrough by codestorywithMIK has 13,742 views views. Want to try solving it yourself? Practice on FleetCode or read the detailed text solution.
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.
Example 1:
Input: nums1 = [2,0,1,3], nums2 = [0,1,2,3] Output: 1 Explanation: There are 4 triplets (x,y,z) such that pos1x < pos1y < pos1z. They are (2,0,1), (2,0,3), (2,1,3), and (0,1,3). Out of those triplets, only the triplet (0,1,3) satisfies pos2x < pos2y < pos2z. Hence, there is only 1 good triplet.
Example 2:
Input: nums1 = [4,0,1,3,2], nums2 = [4,1,0,2,3] Output: 4 Explanation: The 4 good triplets are (4,0,3), (4,0,2), (4,1,3), and (4,1,2).
Constraints:
n == nums1.length == nums2.length3 <= n <= 1050 <= nums1[i], nums2[i] <= n - 1nums1 and nums2 are permutations of [0, 1, ..., n - 1].Problem Overview: You receive two permutations nums1 and nums2 of length n. A triplet (i, j, k) is considered good if i < j < k and the relative order of the values in nums2 is also increasing. The task reduces to counting how many index triplets maintain the same ordering in both arrays.
The key observation: instead of comparing elements across two arrays repeatedly, map each value in nums1 to its index in nums2. This converts the problem into counting increasing triplets in a single array of positions.
Approach 1: Brute Force Enumeration (O(n³) time, O(1) space)
Check every possible triple (i, j, k) with three nested loops. For each triplet, compare their positions in nums2 and verify the order constraint. This works because both arrays contain the same elements. The approach is simple and confirms the definition of a good triplet, but the cubic runtime becomes impractical even for moderate n. Still useful for reasoning about the problem and validating smaller test cases.
Approach 2: Relative Position Array + Binary Indexed Tree (O(n log n) time, O(n) space)
Create a map from value to index in nums2. Transform nums1 into a new array pos where pos[i] represents the position of nums1[i] inside nums2. Now the task becomes counting increasing triplets in pos. For each index j, compute how many values smaller than pos[j] appear to the left and how many larger values appear to the right. Multiply these counts to determine how many triplets use j as the middle element.
A Binary Indexed Tree efficiently maintains prefix counts while iterating. Each query finds how many processed elements are smaller than the current position, and updates insert the current position into the structure. This converts the naive counting problem into logarithmic updates and queries.
Other structures like a Segment Tree or divide-and-conquer methods such as merge-based counting from divide and conquer can solve the same increasing-triplet counting task with similar O(n log n) complexity.
Recommended for interviews: The relative position + Binary Indexed Tree approach is the expected solution. Interviewers want to see that you recognize the permutation mapping trick and reduce the problem to counting increasing subsequence triplets. Mentioning the brute force method shows you understand the baseline, but implementing the O(n log n) counting solution demonstrates strong data structure skills.
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Brute Force Triplet Check | O(n^3) | O(1) | Small inputs or when demonstrating the basic definition of a good triplet |
| Relative Position Array + Binary Indexed Tree | O(n log n) | O(n) | General case; efficient for large permutations and common interview solution |
| Merge Sort / Divide and Conquer Counting | O(n log n) | O(n) | Alternative when using merge-based counting techniques for ordered triplets |