BackmediumArraysPaytm

Min Length Alternating Subarray Solution

Problem Statement

Given an array of integers arr of length n and an integer i, find the minimum length of a subarray that contains exactly i pairs of consecutive elements where the value of the first element in the pair is greater than the second element in the pair. If no such subarray exists, return -1. The array is 0-indexed and the first element is at index 0. The problem statement assumes that the input array is non-empty and i is non-negative.

Example 1
Input
[5, 4, 3, 2, 1]
Output
0

Explanation: Step-by-step: with input [5, 4, 3, 2, 1], we iterate through the array and check each pair of consecutive elements. Since no pair has the first element greater than the second element, the minimum length of the subarray is 0.

Example 2
Input
[]
Output
-1

Explanation: Step-by-step: with input [], we check if the array is empty. Since it is, we return -1 as per the problem statement.

Constraints

  • 1 <= array length <= 10^5
  • All elements are distinct integers in the range [-10^9, 10^9].
Live Compiler1 Free Run Available
Loading Editor...
Test Cases & Output
Click "Run" to test your 1 free compile trial!

šŸš€ Practice this problem

Run code, get AI hints & track streak

Sign Up Free

Min Length Alternating Subarray — Problem Statement & Solution Guide

ArraysMediumSliding Window
TimeO(n^2)
|
SpaceO(1)

Problem Description

Given an array of integers arr of length n and an integer i, find the minimum length of a subarray that contains exactly i pairs of consecutive elements where the value of the first element in the pair is greater than the second element in the pair. If no such subarray exists, return -1. The array is 0-indexed and the first element is at index 0. The problem statement assumes that the input array is non-empty and i is non-negative.

Examples

Example 1

Input

[5, 4, 3, 2, 1]

Output

0

Explanation: Step-by-step: with input [5, 4, 3, 2, 1], we iterate through the array and check each pair of consecutive elements. Since no pair has the first element greater than the second element, the minimum length of the subarray is 0.

Example 2

Input

[]

Output

-1

Explanation: Step-by-step: with input [], we check if the array is empty. Since it is, we return -1 as per the problem statement.

Constraints

  • 1 <= array length <= 10^5
  • All elements are distinct integers in the range [-10^9, 10^9].

Optimal Approach & Strategy

The optimal approach uses a sliding window technique, maintaining a window of elements that satisfy the alternating condition and updating the minimum length as the window moves, resulting in a linear time complexity. This approach efficiently tracks the pattern without needing to examine all possible subarrays.

Brute Force Approach

A naive approach would involve checking all possible subarrays to find the one with the minimum length that satisfies the alternating condition, resulting in a time complexity of O(n²). This approach is inefficient for large inputs. It iterates through the array, generating all possible subarrays and checking each for the desired pattern.

Verified Code Solutions

JavaScript Solution
Time: O(n^2)
function minLengthAlternatingSubarray(arr, i) { if (arr.length === 0 || i <= 0) return -1; let minLen = Infinity; for (let start = 0; start < arr.length; start++) { let count = 0; for (let end = start; end < arr.length - 1; end++) { if (arr[end] > arr[end + 1]) count++; if (count === i) minLen = Math.min(minLen, end - start + 1); } } return minLen === Infinity ? -1 : minLen; }

Asked in Top Tech Interviews

Paytm

Solve in Interative Editor

Ready to test your code? Open our built-in compiler, run custom test suites, and see detailed complexity analysis reports instantly.