{x}
blog image

Subarray With Elements Greater Than Varying Threshold

You are given an integer array nums and an integer threshold.

Find any subarray of nums of length k such that every element in the subarray is greater than threshold / k.

Return the size of any such subarray. If there is no such subarray, return -1.

A subarray is a contiguous non-empty sequence of elements within an array.

 

Example 1:

Input: nums = [1,3,4,3,1], threshold = 6
Output: 3
Explanation: The subarray [3,4,3] has a size of 3, and every element is greater than 6 / 3 = 2.
Note that this is the only valid subarray.

Example 2:

Input: nums = [6,5,6,5,8], threshold = 7
Output: 1
Explanation: The subarray [8] has a size of 1, and 8 > 7 / 1 = 7. So 1 is returned.
Note that the subarray [6,5] has a size of 2, and every element is greater than 7 / 2 = 3.5. 
Similarly, the subarrays [6,5,6], [6,5,6,5], [6,5,6,5,8] also satisfy the given conditions.
Therefore, 2, 3, 4, or 5 may also be returned.

 

Constraints:

  • 1 <= nums.length <= 105
  • 1 <= nums[i], threshold <= 109

Solution Explanation:

This problem asks to find the size of any subarray within nums of length k where every element is greater than threshold / k. Two approaches are presented, both leveraging the concept of finding the longest subarray fulfilling the condition.

Solution 1: Union Find

This approach cleverly uses a Union-Find data structure to efficiently merge adjacent subarrays.

  1. Initialization:

    • p: A parent array for the Union-Find, initially setting each element to itself (representing individual subarrays).
    • size: An array storing the size of each subarray (initially 1).
    • arr: A sorted array of pairs (num, index), sorted in descending order of num. This allows us to process the largest elements first.
  2. Iteration and Merging:

    • The code iterates through arr (largest elements first).
    • For each element (v, i):
      • It checks if the left and right neighbors (i-1 and i+1) are already visited (vis). If so, it merges their subarrays using the merge function.
      • The merge function performs the Union-Find operation, updating the parent and size arrays.
      • If v > threshold / size[find(i)] (the current element is greater than the threshold divided by its subarray size), then it means a valid subarray is found, and its size is returned.
    • Finally, if no such subarray is found, it returns -1.
  3. Union Find Functions:

    • find(x): Finds the root parent of element x (representing its subarray).
    • merge(a, b): Merges the subarrays containing elements a and b.

Time Complexity Analysis (Solution 1):

  • Sorting arr takes O(N log N) time.
  • The find and merge operations in Union Find take (amortized) O(α(N)) time, where α(N) is the inverse Ackermann function, which is practically constant for all realistic input sizes.
  • The main loop iterates N times.
  • Therefore, the overall time complexity is dominated by the sorting step: O(N log N).

Space Complexity Analysis (Solution 1):

  • The space complexity is O(N) due to the arrays p, size, arr, and vis.

Solution 2: Monotonic Stack

This approach utilizes two monotonic stacks to efficiently find the left and right boundaries of the subarray for each element.

  1. Left Boundary:

    • A monotonic decreasing stack stk is used.
    • The stack maintains indices of elements in descending order.
    • When an element v is processed, the stack is popped until the top element is smaller than v.
    • The left[i] array stores the index of the leftmost element that is smaller than nums[i].
  2. Right Boundary:

    • A similar process is applied to find the right[i] array, using a monotonic decreasing stack iterating from right to left.
  3. Verification:

    • The code iterates through the array, checking the condition v > threshold / k for each element, where k = right[i] - left[i] - 1. If this is true, it means a valid subarray of length k is found.

Time Complexity Analysis (Solution 2):

  • Each element is pushed and popped from the stack at most once, resulting in a linear time complexity O(N) for each stack.
  • Therefore, the overall time complexity is O(N).

Space Complexity Analysis (Solution 2):

  • The space complexity is O(N) for the arrays left, right, and the stacks.

Conclusion:

Both solutions correctly solve the problem. Solution 2 (Monotonic Stack) is generally preferred because of its superior O(N) time complexity compared to Solution 1's O(N log N) time complexity. However, Solution 1's Union Find approach is a creative and elegant method that could be advantageous in slightly different variations of the problem. The choice depends on the specific constraints and performance requirements.