3350. Adjacent Increasing Subarrays Detection II LeetCode Solution

In this guide, you will get 3350. Adjacent Increasing Subarrays Detection II LeetCode Solution with the best time and space complexity. The solution to Adjacent Increasing Subarrays Detection II problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.

Table of Contents

  1. Problem Statement
  2. Complexity Analysis
  3. Adjacent Increasing Subarrays Detection II solution in C++
  4. Adjacent Increasing Subarrays Detection II solution in Java
  5. Adjacent Increasing Subarrays Detection II solution in Python
  6. Additional Resources
3350. Adjacent Increasing Subarrays Detection II LeetCode Solution image

Problem Statement of Adjacent Increasing Subarrays Detection II

Given an array nums of n integers, your task is to find the maximum value of k for which there exist two adjacent subarrays of length k each, such that both subarrays are strictly increasing. Specifically, check if there are two subarrays of length k starting at indices a and b (a < b), where:

Both subarrays nums[a..a + k – 1] and nums[b..b + k – 1] are strictly increasing.
The subarrays must be adjacent, meaning b = a + k.

Return the maximum possible value of k.
A subarray is a contiguous non-empty sequence of elements within an array.

Example 1:

Input: nums = [2,5,7,8,9,2,3,4,3,1]
Output: 3
Explanation:

The subarray starting at index 2 is [7, 8, 9], which is strictly increasing.
The subarray starting at index 5 is [2, 3, 4], which is also strictly increasing.
These two subarrays are adjacent, and 3 is the maximum possible value of k for which two such adjacent strictly increasing subarrays exist.

Example 2:

Input: nums = [1,2,3,4,4,4,4,5,6,7]
Output: 2
Explanation:

The subarray starting at index 0 is [1, 2], which is strictly increasing.
The subarray starting at index 2 is [3, 4], which is also strictly increasing.
These two subarrays are adjacent, and 2 is the maximum possible value of k for which two such adjacent strictly increasing subarrays exist.

Constraints:

2 <= nums.length <= 2 * 105
-109 <= nums[i] <= 109

Complexity Analysis

  • Time Complexity: O(n)
  • Space Complexity: O(1)

3350. Adjacent Increasing Subarrays Detection II LeetCode Solution in C++

class Solution {
 public:
  // Similar to 3349. Adjacent Increasing Subarrays Detection I
  int maxIncreasingSubarrays(vector<int>& nums) {
    int ans = 0;
    int increasing = 1;
    int prevIncreasing = 0;

    for (int i = 1; i < nums.size(); ++i) {
      if (nums[i] > nums[i - 1]) {
        ++increasing;
      } else {
        prevIncreasing = increasing;
        increasing = 1;
      }
      ans = max(ans, increasing / 2);
      ans = max(ans, min(prevIncreasing, increasing));
    }

    return ans;
  }
};
/* code provided by PROGIEZ */

3350. Adjacent Increasing Subarrays Detection II LeetCode Solution in Java

class Solution {
  // Similar to 3349. Adjacent Increasing Subarrays Detection I
  public int maxIncreasingSubarrays(List<Integer> nums) {
    int ans = 0;
    int increasing = 1;
    int prevIncreasing = 0;

    for (int i = 1; i < nums.size(); ++i) {
      if (nums.get(i) > nums.get(i - 1)) {
        ++increasing;
      } else {
        prevIncreasing = increasing;
        increasing = 1;
      }
      ans = Math.max(ans, increasing / 2);
      ans = Math.max(ans, Math.min(prevIncreasing, increasing));
    }

    return ans;
  }
}
// code provided by PROGIEZ

3350. Adjacent Increasing Subarrays Detection II LeetCode Solution in Python

class Solution:
  # Similar to 3349. Adjacent Increasing Subarrays Detection I
  def maxIncreasingSubarrays(self, nums: list[int]) -> int:
    ans = 0
    increasing = 1
    prevIncreasing = 0

    for a, b in itertools.pairwise(nums):
      if b > a:
        increasing += 1
      else:
        prevIncreasing = increasing
        increasing = 1
      ans = max(ans, increasing // 2)
      ans = max(ans, min(prevIncreasing, increasing))

    return ans
# code by PROGIEZ

Additional Resources

Happy Coding! Keep following PROGIEZ for more updates and solutions.