3349. Adjacent Increasing Subarrays Detection I LeetCode Solution

In this guide, you will get 3349. Adjacent Increasing Subarrays Detection I LeetCode Solution with the best time and space complexity. The solution to Adjacent Increasing Subarrays Detection I 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 I solution in C++
  4. Adjacent Increasing Subarrays Detection I solution in Java
  5. Adjacent Increasing Subarrays Detection I solution in Python
  6. Additional Resources
3349. Adjacent Increasing Subarrays Detection I LeetCode Solution image

Problem Statement of Adjacent Increasing Subarrays Detection I

Given an array nums of n integers and an integer k, determine whether there exist two adjacent subarrays of length k such that both subarrays are strictly increasing. Specifically, check if there are two subarrays 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 true if it is possible to find two such subarrays, and false otherwise.

Example 1:

Input: nums = [2,5,7,8,9,2,3,4,3,1], k = 3
Output: true
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, so the result is true.

Example 2:

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

Constraints:

2 <= nums.length <= 100
1 < 2 * k <= nums.length
-1000 <= nums[i] <= 1000

Complexity Analysis

  • Time Complexity: O(n)
  • Space Complexity: O(1)
See also  829. Consecutive Numbers Sum LeetCode Solution

3349. Adjacent Increasing Subarrays Detection I LeetCode Solution in C++

class Solution {
 public:
  bool hasIncreasingSubarrays(vector<int>& nums, int k) {
    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;
      }
      if (increasing / 2 >= k || min(prevIncreasing, increasing) >= k)
        return true;
    }

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

3349. Adjacent Increasing Subarrays Detection I LeetCode Solution in Java

class Solution {
  public boolean hasIncreasingSubarrays(List<Integer> nums, int k) {
    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;
      }
      if (increasing / 2 >= k || Math.min(prevIncreasing, increasing) >= k)
        return true;
    }

    return false;
  }
}
// code provided by PROGIEZ

3349. Adjacent Increasing Subarrays Detection I LeetCode Solution in Python

class Solution:
  def hasIncreasingSubarrays(self, nums: list[int], k: int) -> bool:
    increasing = 1
    prevIncreasing = 0

    for a, b in itertools.pairwise(nums):
      if b > a:
        increasing += 1
      else:
        prevIncreasing = increasing
        increasing = 1
      if increasing // 2 >= k or min(prevIncreasing, increasing) >= k:
        return True

    return False
# code by PROGIEZ

Additional Resources

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