- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Longest Strictly Increasing or Strictly Decreasing Subarray
The Longest Strictly Increasing or Strictly Decreasing Subarray problem is used to find the maximum length of the contiguous subarray within a given array where the elements are either strictly increasing or strictly decreasing.
Problem Statement
Given an array of integers nums, return the length n of the longest subarray of n which is either strictly increasing or strictly decreasing.
Example Scenario 1
Input: nums = [1, 3, 2, 4, 3, 5, 4, 6]
Output: n = 2
The longest strictly increasing subarrays are [1, 3], [2, 4], [3, 5], and [4, 6]. The longest strictly decreasing subarrays are [3, 2] and [4, 3]. The longest subarray = [1, 3], [2, 4], [3, 5], or [4, 6], each of length 2.
Example Scenario 2
Input: nums = [5, 4, 3, 2, 1]
Output: n = 5
The entire array is strictly decreasing, so the longest subarray = [5, 4, 3, 2, 1], which has length 5.
Example Scenario 3
Input: nums = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
Output: n = 10
The entire array is strictly increasing, so the longest subarray = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], which has length 10.
Example Scenario 4
Input: nums = [10, 20, 30, 25, 20, 15]
Output: n = 4
The longest strictly increasing subarray = [10, 20, 30] and the longest strictly decreasing subarray = [30, 25, 20, 15]. The longest subarray = [30, 25, 20, 15], which has length 4.
Time Complexity
The time complexity of calculating the score of a string is O(n), where n is the maximum length of the string.
To solve this problem in various programming languages, use the following methods.
- Linear Scan Approach
- Two-Pass Approach
Linear Scan Approach
This approach involves a single pass through the array to keep track of the lengths of the current increasing or decreasing subarrays and update the maximum length.
Example
#include <iostream> #include <vector> using namespace std; int longestSubarray(vector<int>& nums) { int n = nums.size(); if (n == 0) return 0; int maxLength = 1, incLength = 1, decLength = 1; for (int i = 1; i < n; ++i) { if (nums[i] > nums[i - 1]) { incLength++; decLength = 1; } else if (nums[i] < nums[i - 1]) { decLength++; incLength = 1; } else { incLength = 1; decLength = 1; } maxLength = max(maxLength, max(incLength, decLength)); } return maxLength; } int main() { vector<int> nums = {10, 20, 30, 25, 20, 15}; cout << "Length of the longest subarray = " << longestSubarray(nums) << endl; return 0; }
Output
Length of the longest subarray = 4
public class LongestSubarray { public static int longestSubarray(int[] nums) { int n = nums.length; if (n == 0) return 0; int maxLength = 1, incLength = 1, decLength = 1; for (int i = 1; i < n; ++i) { if (nums[i] > nums[i - 1]) { incLength++; decLength = 1; } else if (nums[i] < nums[i - 1]) { decLength++; incLength = 1; } else { incLength = 1; decLength = 1; } maxLength = Math.max(maxLength, Math.max(incLength, decLength)); } return maxLength; } public static void main(String[] args) { int[] nums = {10, 20, 30, 25, 20, 15}; System.out.println("Length of the longest subarray = " + longestSubarray(nums)); } }
Output
Length of the longest subarray = 4
def longest_subarray(nums): n = len(nums) if n == 0: return 0 max_length = 1 inc_length = 1 dec_length = 1 for i in range(1, n): if nums[i] > nums[i - 1]: inc_length += 1 dec_length = 1 elif nums[i] < nums[i - 1]: dec_length += 1 inc_length = 1 else: inc_length = 1 dec_length = 1 max_length = max(max_length, inc_length, dec_length) return max_length # Example usage nums = [10, 20, 30, 25, 20, 15] print("Length of the longest subarray = ", longest_subarray(nums))
Output
Length of the longest subarray = 4
Two-Pass Approach
In this approach, we can perform two separate passes through the array. The first pass finds the length of the longest strictly increasing subarray and the second pass finds the length of the longest strictly decreasing subarray. so, from that you can calculate the maximum of the two lengths.
Example
#include <iostream> #include <vector> using namespace std; int longestIncreasingSubarray(vector<int>& nums) { int maxLength = 1, currentLength = 1; for (int i = 1; i < nums.size(); ++i) { if (nums[i] > nums[i - 1]) { currentLength++; } else { currentLength = 1; } maxLength = max(maxLength, currentLength); } return maxLength; } int longestDecreasingSubarray(vector<int>& nums) { int maxLength = 1, currentLength = 1; for (int i = 1; i < nums.size(); ++i) { if (nums[i] < nums[i - 1]) { currentLength++; } else { currentLength = 1; } maxLength = max(maxLength, currentLength); } return maxLength; } int longestSubarray(vector<int>& nums) { return max(longestIncreasingSubarray(nums), longestDecreasingSubarray(nums)); } int main() { vector<int> nums = {10, 20, 30, 25, 20, 15}; cout << "Length of the longest subarray = " << longestSubarray(nums) << endl; return 0; }
Output
Length of the longest subarray = 4
public class LongestSubarray { public static int longestIncreasingSubarray(int[] nums) { int maxLength = 1, currentLength = 1; for (int i = 1; i < nums.length; ++i) { if (nums[i] > nums[i - 1]) { currentLength++; } else { currentLength = 1; } maxLength = Math.max(maxLength, currentLength); } return maxLength; } public static int longestDecreasingSubarray(int[] nums) { int maxLength = 1, currentLength = 1; for (int i = 1; i < nums.length; ++i) { if (nums[i] < nums[i - 1]) { currentLength++; } else { currentLength = 1; } maxLength = Math.max(maxLength, currentLength); } return maxLength; } public static int longestSubarray(int[] nums) { return Math.max(longestIncreasingSubarray(nums), longestDecreasingSubarray(nums)); } public static void main(String[] args) { int[] nums = {10, 20, 30, 25, 20, 15}; System.out.println("Length of the longest subarray = " + longestSubarray(nums)); } }
Output
Length of the longest subarray = 4
def longest_increasing_subarray(nums): max_length = 1 current_length = 1 for i in range(1, len(nums)): if nums[i] > nums[i - 1]: current_length += 1 else: current_length = 1 max_length = max(max_length, current_length) return max_length def longest_decreasing_subarray(nums): max_length = 1 current_length = 1 for i in range(1, len(nums)): if nums[i] < nums[i - 1]: current_length += 1 else: current_length = 1 max_length = max(max_length, current_length) return max_length def longest_subarray(nums): return max(longest_increasing_subarray(nums), longest_decreasing_subarray(nums)) # Example usage nums = [10, 20, 30, 25, 20, 15] print("Length of the longest subarray = ", longest_subarray(nums))
Output
Length of the longest subarray = 4