Given an array, find the length of the longest increasing subarray (contiguous elements) such that it is possible to change at most one number (change one number to any integer you want) from the sequence to make the sequence strictly increasing.
Input : 6 7 2 3 1 5 10 Output : 5 Explanation : Here, we can choose subarray 2, 3, 1, 5, 10 and by changing its 3rd element (that is 1) to 4, it will become increasing sequence. Input : 2 10 10 Output : 2 Explanation : Here, we can choose subarray 10, 10 and by changing its 2nd element (that is 10) to 11, it will become increasing sequence.
Step 1: We first compute the longest increasing subarray ending at an index for every index in the given array. We store these values in l.
Step 2: Then calculate the longest increasing subarray starting at an index for every index in the given array. We store these values in r.
Step 3: Update the answer ans = max ( ans, l[i-1] + r[i+1] + 1), when a[i-1] + 1 < a[i+1].
Below is the implementation of the above idea:
This article is contributed by Abhishek Sharma. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Longest increasing subarray
- Length of longest increasing circular subarray
- Longest increasing subsequence which forms a subarray in the sorted representation of the array
- Length of the longest increasing subsequence which does not contain a given sequence as Subarray
- Length of longest subarray with increasing contiguous elements
- Longest Prime Subarray after removing one element
- Longest subarray having count of 1s one more than count of 0s
- Longest subarray such that adjacent elements have at least one common digit | Set 1
- Longest subarray such that the difference of max and min is at-most one
- Longest subarray such that adjacent elements have at least one common digit | Set - 2
- Length of longest Fibonacci subarray formed by removing only one element
- Find subarray with given sum with negatives allowed in constant space
- Find Maximum Sum Strictly Increasing Subarray
- Largest sum contiguous increasing subarray
- Minimum size Subarray with maximum sum in non-increasing order
- Longest Increasing Subsequence Size (N log N)
- Construction of Longest Increasing Subsequence (N log N)
- Longest Common Increasing Subsequence (LCS + LIS)
- Longest Monotonically Increasing Subsequence Size (N log N): Simple implementation
- Longest Increasing Odd Even Subsequence