Count Increasing Quadruplets - Problem
Count Increasing Quadruplets is a fascinating array problem that challenges you to find a specific pattern within a permutation.
You're given a 0-indexed integer array
An increasing quadruplet
1. Position constraint:
2. Value constraint:
Notice the interesting pattern: the values don't follow the same order as the indices! We need
Example: In array
You're given a 0-indexed integer array
nums of size n containing all numbers from 1 to n (a permutation). Your task is to count how many increasing quadruplets exist in this array.An increasing quadruplet
(i, j, k, l) must satisfy two conditions:1. Position constraint:
0 ≤ i < j < k < l < n2. Value constraint:
nums[i] < nums[k] < nums[j] < nums[l]Notice the interesting pattern: the values don't follow the same order as the indices! We need
nums[i] < nums[k] < nums[j] < nums[l] where k comes after j positionally but nums[k] is smaller than nums[j].Example: In array
[2, 1, 4, 3], one valid quadruplet is (0, 2, 1, 3) because nums[0]=2 < nums[1]=1 is false, but (1, 2, 0, 3) where nums[1]=1 < nums[0]=2 < nums[2]=4 < nums[3]=3 is also invalid due to position constraints. The correct interpretation requires careful analysis of both position and value constraints. Input & Output
example_1.py — Basic Case
$
Input:
nums = [1,3,2,4,5]
›
Output:
2
💡 Note:
The valid quadruplets are (0,2,1,3) and (0,2,1,4). For (0,2,1,3): nums[0]=1 < nums[1]=2 < nums[2]=3 < nums[3]=4, but we need nums[0] < nums[1] < nums[2] < nums[3] with indices 0 < 2 < 1 < 3 which is impossible. Actually, we need (0,1,2,3) where nums[0]=1 < nums[2]=2 < nums[1]=3 < nums[3]=4, which gives us the pattern 1 < 2 < 3 < 4. Let me recalculate: valid quadruplets have the pattern nums[i] < nums[k] < nums[j] < nums[l] where i < j < k < l.
example_2.py — No Valid Quadruplets
$
Input:
nums = [1,2,3,4]
›
Output:
0
💡 Note:
For any quadruplet (i,j,k,l) with i < j < k < l, we would have nums[i] < nums[j] < nums[k] < nums[l] due to the sorted order. But we need nums[i] < nums[k] < nums[j] < nums[l], which requires nums[k] < nums[j]. Since k > j, this is impossible in a sorted array.
example_3.py — Multiple Valid Patterns
$
Input:
nums = [2,1,4,3,6,5]
›
Output:
4
💡 Note:
This array has several valid increasing quadruplets. The pattern nums[i] < nums[k] < nums[j] < nums[l] can be satisfied in multiple ways. For example: (1,2,0,4) gives us nums[1]=1 < nums[0]=2 < nums[2]=4 < nums[4]=6, which matches our required pattern with i=1, j=2, k=0, l=4, but this violates i < j < k < l. We need to carefully identify valid index combinations.
Visualization
Tap to expand
Understanding the Visualization
1
Identify the Pattern
Understand that we need nums[i] < nums[k] < nums[j] < nums[l] with positions i < j < k < l
2
Fix Middle Positions
Choose positions j and k where j < k and nums[k] < nums[j] to satisfy the middle constraint
3
Count Valid Endpoints
For each (j,k) pair, count how many valid i positions (left) and l positions (right) exist
4
Multiply and Sum
The total quadruplets for each (j,k) is left_count × right_count
Key Takeaway
🎯 Key Insight: Fix the middle positions j,k first, then count valid endpoints efficiently to achieve O(n²) complexity instead of naive O(n⁴).
Time & Space Complexity
Time Complexity
O(n²)
O(n²) to precompute prefix/suffix arrays, O(n²) to check all (j,k) pairs
⚠ Quadratic Growth
Space Complexity
O(n)
Additional space for prefix and suffix counting arrays
⚡ Linearithmic Space
Constraints
- 4 ≤ nums.length ≤ 4000
- 1 ≤ nums[i] ≤ nums.length
- All values in nums are unique
- nums is a permutation of integers from 1 to n
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code