Skip to main content

Count Increasing Quadruplets - Solution & Explanation

HardArrayDynamic ProgrammingBinary Indexed TreeEnumeration16 min readAsked at: Deutsche Bank, SAP
Practice this problem

Problem Statement

Given a 0-indexed integer array nums of size n containing all numbers from 1 to n, return the number of increasing quadruplets.

A quadruplet (i, j, k, l) is increasing if:

  • 0 <= i < j < k < l < n, and
  • nums[i] < nums[k] < nums[j] < nums[l].

 

Example 1:

Input: nums = [1,3,2,4,5]
Output: 2
Explanation: 
- When i = 0, j = 1, k = 2, and l = 3, nums[i] < nums[k] < nums[j] < nums[l].
- When i = 0, j = 1, k = 2, and l = 4, nums[i] < nums[k] < nums[j] < nums[l]. 
There are no other quadruplets, so we return 2.

Example 2:

Input: nums = [1,2,3,4]
Output: 0
Explanation: There exists only one quadruplet with i = 0, j = 1, k = 2, l = 3, but since nums[j] < nums[k], we return 0.

 

Constraints:

  • 4 <= nums.length <= 4000
  • 1 <= nums[i] <= nums.length
  • All the integers of nums are unique. nums is a permutation.

Approach Overview

Problem Overview: Given an array nums, count the number of index quadruplets (i, j, k, l) such that i < j < k < l and nums[i] < nums[k] < nums[j] < nums[l]. The challenge is enforcing both index ordering and value ordering efficiently without checking every possible combination.

Approach 1: Brute Force with Early Pruning (O(n^4) time, O(1) space)

Enumerate all quadruplets using four nested loops over indices i, j, k, and l. For each combination, check the ordering constraint directly. Small optimizations prune cases early, such as skipping when nums[i] >= nums[k] or nums[j] >= nums[l]. The algorithm is straightforward but scales poorly because the search space grows as n^4. Useful mainly for understanding the problem or validating optimized solutions.

Approach 2: Nested Loop with Counting (O(n^3) time, O(1) space)

Fix two middle indices and reduce the remaining search to counting valid values. Iterate j and k, ensuring j < k. For each pair where nums[k] < nums[j], scan the left side for indices i where nums[i] < nums[k] and scan the right side for indices l where nums[l] > nums[j]. Multiply the counts to get the number of quadruplets using this (j,k) pair. This eliminates one loop but still requires scanning ranges repeatedly.

Approach 3: Improved Counting with Preprocessing (O(n^2) time, O(n^2) space)

Precompute helpful counts to avoid rescanning the array. For each index pair, track how many elements to the right are greater than a given value and how many to the left are smaller. When iterating over (j, k) pairs with j < k and nums[k] < nums[j], combine these precomputed values to determine how many valid i and l indices exist. This converts repeated scans into constant‑time lookups, reducing the overall complexity to O(n^2). The approach relies heavily on careful counting and is closely related to techniques used in dynamic programming and prefix sum style preprocessing.

Approach 4: Optimized Approach with Preprocessing / Fenwick Tree (O(n^2) time, O(n log n) updates)

A more scalable variant uses frequency structures such as a Fenwick Tree (Binary Indexed Tree). While iterating through the array, maintain counts of values already seen and values remaining. For each potential (j, k) pair, query the structure to count elements smaller than nums[k] on the left and larger than nums[j] on the right. Fenwick Trees support prefix queries and updates in O(log n), making the counting step efficient. This technique is common in advanced array counting problems and when using a Binary Indexed Tree.

Recommended for interviews: Start by describing the brute force enumeration to demonstrate understanding of the constraints. Interviewers usually expect the optimized counting approach with preprocessing or Fenwick Tree, which reduces the problem to O(n^2) time. The key insight is fixing the middle pair (j, k) and counting valid elements on both sides instead of explicitly enumerating all four indices.

Approach 1: Brute Force with Early Pruning

The brute force approach involves checking all possible quadruplets (i, j, k, l) in the array that satisfy the required conditions. This can be done using four nested loops. Given the constraints, however, this approach is highly inefficient with a time complexity of O(n^4). However, optimizations can be applied by pruning and breaking out early if conditions are not met to slightly ease the computation.

The solution checks each combination of indices (i, j, k, l) to see if the quadruplet satisfies the conditions nums[i] < nums[k] < nums[j] < nums[l]. While primarily a brute force method, minor optimizations reduce unnecessary checks based on conditions.

Code

Python

Complexity

The time complexity is O(n^4) due to four nested loops. Space complexity is O(1) as no additional space is used other than variables.

Try this approach in the editor →

Approach 2: Optimized Approach with Preprocessing

An optimized approach involves preprocessing information to avoid unnecessary checks. The idea is to use auxiliary arrays to store useful information that indicates how many elements suitable for being part of a quadruplet are smaller than or larger than a particular element. This helps in reducing the time taken to check conditions for the quadruplets.

This solution uses two auxiliary arrays: one (less_left) that records how many elements less than the current element are to its left, and another (great_right) that records how many elements greater than it are to its right. This precomputation allows us to efficiently count valid quadruplets.

Code

Python

Complexity

The time complexity is O(n^2) due to the need to construct and utilize the auxiliary arrays. Space complexity is O(n) for the storage of two additional arrays.

Try this approach in the editor →

Approach 3: Nested Loop with Counting

In this approach, iterate through each possible quadruplet using nested loops, ensuring the indices match the constraints: 0 <= i < j < k < l < n. Count valid quadruplets by checking if the conditions nums[i] < nums[k] < nums[j] < nums[l] are satisfied.

This C solution uses four nested loops to iterate over all possible quadruplets (i, j, k, l) as per given conditions. Each valid quadruplet that satisfies nums[i] < nums[k] < nums[j] < nums[l] increments the count. Finally, the function returns the count of such quadruplets.

Code

C

C++

Java

Python

C#

JavaScript

Complexity

Time Complexity: O(n^4) due to four nested loops.
Space Complexity: O(1) since we're using a constant amount of extra space.

Try this approach in the editor →

Approach 4: Improved Counting with Preprocessing

This approach optimizes the search for quadruplets by preprocessing the input array to limit repeated comparisons. It focuses on exploring possibilities for 'j' and then uses data structures to store potential i, k, and l values to avoid redundant operations, significantly improving efficiency compared to the brute-force method.

This Python solution preprocesses to count potential i indices for each possible k index up to j. It avoids redundant computation by focusing only on valid index combinations, generally reducing comparison operations through preprocessing.

Code

Python

Java

Complexity

Time Complexity: O(n^3) because the innermost computation is reduced by preprocessing.
Space Complexity: O(n) due to auxiliary storage for preprocessing count arrays.

Try this approach in the editor →

Approach 5: Enumeration + Preprocessing

We can enumerate j and k in the quadruplet, then the problem is transformed into, for the current j and k:

  • Count how many l satisfy l > k and nums[l] > nums[j];
  • Count how many i satisfy i < j and nums[i] < nums[k].

We can use two two-dimensional arrays f and g to record these two pieces of information. Where f[j][k] represents how many l satisfy l > k and nums[l] > nums[j], and g[j][k] represents how many i satisfy i < j and nums[i] < nums[k].

Therefore, the answer is the sum of all f[j][k] times g[j][k].

The time complexity is O(n^2), and the space complexity is O(n^2). Where n is the length of the array.

Code

Python

Java

C++

Go

Try this approach in the editor →

Complexity Comparison

ApproachComplexity
Brute Force with Early Pruning

The time complexity is O(n^4) due to four nested loops. Space complexity is O(1) as no additional space is used other than variables.

Optimized Approach with Preprocessing

The time complexity is O(n^2) due to the need to construct and utilize the auxiliary arrays. Space complexity is O(n) for the storage of two additional arrays.

Nested Loop with Counting

Time Complexity: O(n^4) due to four nested loops.
Space Complexity: O(1) since we're using a constant amount of extra space.

Improved Counting with Preprocessing

Time Complexity: O(n^3) because the innermost computation is reduced by preprocessing.
Space Complexity: O(n) due to auxiliary storage for preprocessing count arrays.

Enumeration + Preprocessing

Detailed Complexity Analysis

ApproachTimeSpaceWhen to Use
Brute Force with Early PruningO(n^4)O(1)Understanding the constraints or validating correctness for small inputs
Nested Loop with CountingO(n^3)O(1)When optimizing brute force but avoiding extra memory
Improved Counting with PreprocessingO(n^2)O(n^2)Best balance of clarity and speed for interview settings
Fenwick Tree / Binary Indexed TreeO(n^2 log n)O(n)Useful when using ordered counting structures or extending to larger value ranges

Video Solution

2552. Count Increasing Quadruplets | Leetcode weekly contestA Code Daily!1,977 views views

Watch 9 more video solutions →

Frequently Asked Questions

Is Count Increasing Quadruplets easy or hard?
LeetCode classifies Count Increasing Quadruplets as Hard because the brute force approach is obvious but far too slow. The challenge lies in recognizing that fixing the middle pair and counting valid elements on both sides transforms the problem into a quadratic-time counting problem.
How to solve Count Increasing Quadruplets in O(n^2)?
Iterate over the middle indices j and k with j < k. When nums[k] < nums[j], count how many indices i < j satisfy nums[i] < nums[k] and how many indices l > k satisfy nums[l] > nums[j]. Precomputing these counts with prefix or suffix arrays allows constant-time lookups, giving an overall O(n^2) algorithm.
Count Increasing Quadruplets Python or Java solution?
Python and Java solutions typically implement the O(n^2) counting strategy. They iterate through (j, k) pairs and use precomputed arrays or maps to retrieve counts of valid i and l indices. Both languages can implement the logic cleanly with arrays and nested loops.
What is the best approach for Count Increasing Quadruplets?
The most practical solution fixes the middle pair (j, k) and counts valid elements on both sides using preprocessing. This reduces the complexity from O(n^4) brute force to about O(n^2). Many implementations precompute how many values to the right are greater than a given number and combine them with counts of smaller elements on the left.
What data structure is used in Count Increasing Quadruplets?
Optimized implementations often use prefix arrays, frequency tables, or a Fenwick Tree (Binary Indexed Tree). These structures help count how many elements smaller or larger than a value exist on either side of a position without scanning the array repeatedly.
What is the time complexity of Count Increasing Quadruplets?
The naive solution runs in O(n^4) time because it checks every quadruplet. Optimized solutions reduce this to O(n^2) using counting and preprocessing, or O(n^2 log n) when a Binary Indexed Tree is used for prefix queries.
Is Count Increasing Quadruplets asked at Google, Amazon, or Meta?
Problems involving counting ordered tuples with value constraints appear frequently in interviews at companies like Google, Amazon, and Meta. The exact problem may vary, but the techniques—prefix counting, Fenwick Trees, and dynamic programming style preprocessing—are commonly tested.

Ready to solve this problem?

Practice Count Increasing Quadruplets with our built-in code editor and test cases.

Practice on FleetCode