Solution For Count Increasing Quadruplets
Problem Statement:
Given an array of integers nums, return the number of quadruplets
(a, b, c, d) such that:
nums[a] < nums[b] < nums[c] < nums[d]
Example:
Input: nums = [1,2,3,4]
Output: 1
Explanation: The only quadruplet that satisfies the condition is (0, 1, 2, 3)
Solution:
The given problem can be solved using nested loops in Time complexity of O(n^4). But such an approach is pretty inefficient as the input size of the array can be as large as 5000.
We can solve this using an optimized approach in O(n^2) time complexity.
The optimized solution involves using pre-processing to count all pairs and quadruplets that satisfy the condition. The main idea is to fix the two indices (i, j) in the array and then count the number of increasing pairs of elements that have indices greater than j. This can be done using a simple loop.
Algorithm:
- Initialize a variable count to zero, which will count all the valid quadruplets.
- For each pair of indices (i, j), where 0 ≤ i < j < n, count the number of increasing pairs of elements greater than j.
- The count for this step can be achieved by using a variable k initialized to j+1 and checking if nums[i] < nums[j] < nums[k] and nums[k] < nums[l], where l > k.
- If the condition is true we increment our count by (n-l).
- Return the count of valid quadruplets.
Code:
class Solution {
public:
int countQuadruplets(vector
int n = nums.size();
int count = 0;
for (int i = 0; i < n-3; i++) {
for (int j = i+1; j < n-2; j++) {
int k = j + 1, l = n – 1;
while (k < l) {
if (nums[i] < nums[j] && nums[j] < nums[k] && nums[k] < nums[l]) {
count += (n-l);
k++;
} else {
l–;
}
}
}
}
return count;
}
};
Time Complexity: O(n^2), Space Complexity: O(1)
Explanation:
In the above code, we maintain two loops to check each pair of indices and keep a count of the number of valid quadruplets. For each pair of indices, we set k to j+1, l to n-1, and start a while loop until k becomes greater than or equal to l.
We check the condition if nums[i] < nums[j] < nums[k] < nums[l], where k is the index of the third number in our quadruplet. If this condition is true, we add (n-l) to the count and increment k (count the number of valid quadruplets). If the condition is false, we decrement l. We continue to do this until k becomes greater than or equal to l.
Finally, we return the count of all valid quadruplets in the input array nums.
Step by Step Implementation For Count Increasing Quadruplets
public int countIncreasingQuadruplets(int[] nums) { int count = 0; for (int i = 0; i < nums.length - 3; i++) { for (int j = i + 1; j < nums.length - 2; j++) { for (int k = j + 1; k < nums.length - 1; k++) { for (int l = k + 1; l < nums.length; l++) { if (nums[i] < nums[j] && nums[j] < nums[k] && nums[k] < nums[l]) { count++; } } } } } return count; }
def count_increasing_quadruplets(nums): # Initialize count of quadruplets ans = 0 # Fix the first element as A[i] of the quadruplet, # iterate for j = i + 1 to A.length - 3 # Doing this fix avoids duplicates for i in range(0, len(nums) - 3): # Fix the second element as A[j] for j in range(i + 1, len(nums) - 2): # Now look for the third number for k in range(j + 1, len(nums) - 1): # if the first number is smaller than the second or # if the second number is smaller than the third number # continue the process if nums[i] < nums[j] or nums[j] < nums[k]: continue # if the first number is greater than the second & # third number, break the process if nums[i] > nums[k]: break # if the fourth number is greater than the third number # continue the process if nums[k + 1] > nums[k]: ans += 1 return ans
var countIncreasingQuadruplets = function(nums) { let count = 0; for (let i = 0; i < nums.length - 3; i++) { for (let j = i + 1; j < nums.length - 2; j++) { for (let k = j + 1; k < nums.length - 1; k++) { for (let l = k + 1; l < nums.length; l++) { if (nums[i] < nums[j] && nums[j] < nums[k] && nums[k] < nums[l]) { count++; } } } } } return count; };
The problem is to find a count of all increasing quadruplets in an array. One solution is to use a brute force approach, where we iterate through the array and check for each quadruplet if it is increasing. However, this approach is not very efficient, as it has a time complexity of O(n^4). A more efficient solution is to use a sorting algorithm, such as quicksort, to sort the array first. Then, we can iterate through the array and for each element, check if there are any other elements that are larger than it and form a quadruplet. This approach has a time complexity of O(n^3).
public int CountIncreasingQuadruplets(int[] nums) { // create a hashset to store all unique quadruplets // iterate through the array for (int i = 0; i < nums.Length - 3; i++) { for (int j = i + 1; j < nums.Length - 2; j++) { for (int k = j + 1; k < nums.Length - 1; k++) { for (int l = k + 1; l < nums.Length; l++) { // if all four numbers are increasing if (nums[i] < nums[j] && nums[j] < nums[k] && nums[k] < nums[l]) { // add the quadruplet to the hashset HashSet.Add(new int[] { nums[i], nums[j], nums[k], nums[l] }); } } } } } // return the count of unique quadruplets return HashSet.Count; }