We use cookies (including Google cookies) to personalize ads and analyze traffic. By continuing to use our site, you accept our Privacy Policy.

Adjacent Increasing Subarrays Detection II

Number: 3619

Difficulty: Medium

Paid? No

Companies: N/A


Problem Description

Given an array of n integers, the goal is to determine the maximum possible integer k such that there exist two adjacent subarrays, each of length k, and each subarray is strictly increasing. In other words, find the largest k for which there is some starting index a satisfying: • The subarray nums[a..a+k-1] is strictly increasing. • The subarray nums[a+k..a+2*k-1] is strictly increasing.


Key Insights

  • Precompute the length of the contiguous strictly increasing sequence starting from each index.
  • Use binary search over possible values of k (from 1 to n/2) to efficiently pinpoint the maximum valid k.
  • For a fixed candidate k, check for any index i (with appropriate bounds) such that both computed lengths at positions i and i+k are at least k.
  • The binary search approach yields O(n log n) time complexity and meets the problem constraints.

Space and Time Complexity

Time Complexity: O(n log n) – For each candidate k, scanning the array takes O(n), and binary search will perform O(log n) iterations. Space Complexity: O(n) – Storing the precomputed increasing-length array.


Solution

We first precompute an array (let’s call it increasing) where increasing[i] represents the number of consecutive elements starting from index i that form a strictly increasing sequence. This can be computed in one pass backward from the end of the array.

After precomputation, we use binary search on k (ranging between 1 and n/2) and for each candidate k, we scan the valid indices i (from 0 to n - 2*k) to check if both increasing[i] and increasing[i+k] are at least k. If a candidate k is valid, we attempt a larger k; otherwise, we search for a smaller k.

This approach efficiently finds the maximum valid k.


Code Solutions

# Python solution with line-by-line comments.
def max_adjacent_increasing_subarrays(nums):
    n = len(nums)
    # Precompute the length of strictly increasing subarray starting at each index.
    increasing = [1] * n
    # Traverse from second last index back to the beginning.
    for i in range(n - 2, -1, -1):
        # If current element is less than the next, it is part of an increasing sequence.
        if nums[i] < nums[i + 1]:
            increasing[i] = increasing[i + 1] + 1
        # Else, default value 1 remains.
    
    # Helper function to check if there exists two adjacent subarrays of length k.
    def valid(k):
        # Iterate through possible starting indices for the first subarray.
        for i in range(0, n - 2 * k + 1):
            # Check if both subarrays starting at i and i+k have at least 'k' increasing elements.
            if increasing[i] >= k and increasing[i + k] >= k:
                return True
        return False

    # Binary search for the maximum valid k, search range from 1 to n//2.
    low, high, ans = 1, n // 2, 0
    while low <= high:
        mid = (low + high) // 2
        if valid(mid):
            ans = mid  # mid is valid, update answer.
            low = mid + 1
        else:
            high = mid - 1
    return ans

# Example usage:
print(max_adjacent_increasing_subarrays([2,5,7,8,9,2,3,4,3,1]))  # Expected output: 3
← Back to All Questions