# Longest Increasing subarray with one change allowed

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.

Examples:

```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.
```

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

Approach :
Step 1: We first compute longest increasing subarray ending at an index for every index in given array. We store these values in l[].
Step 2: Then calculate longest increasing subarray starting at an index for every index in 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 approach:

## C++

 `// CPP program to find longest increasing subarray ` `// with one change allowed. ` `#include ` `using` `namespace` `std; ` ` `  `// Function to find length of ` `// subsequence ` `int` `seg(``int` `a[], ``int` `n) ` `{ ` `    ``int` `l[n], r[n+1], ans = 0; ` `     `  `    ``// calculating the l array. ` `    ``l = 1; ` `    ``for` `(``int` `i = 1; i < n; i++) { ` `        ``if` `(a[i] > a[i - 1]) { ` `            ``l[i] = l[i - 1] + 1; ` `            ``ans = max(ans, l[i]); ` `        ``} ` `        ``else` `            ``l[i] = 1; ` `    ``} ` `    ``if` `(ans != n) ` `        ``++ans; ` ` `  `    ``// calculating the r array. ` `    ``r[n] = 0; ` `    ``for` `(``int` `i = n - 1; i >= 0; i--) { ` `        ``if` `(a[i] < a[i + 1]) ` `            ``r[i] = r[i + 1] + 1; ` `        ``else` `            ``r[i] = 1; ` `    ``} ` ` `  `    ``// updating the answer. ` `    ``for` `(``int` `i = n - 1; i > 0; i--) { ` `        ``if` `(a[i + 1] - a[i - 1] > 1) ` `            ``ans = max(ans, l[i - 1] +  ` `                    ``r[i + 1] + 1); ` `    ``} ` `   `  `    ``return` `max(ans, r); ` `} ` ` `  `// driver function. ` `int` `main() ` `{ ` `    ``int` `a[] = { 9, 4, 5, 1, 13 }; ` `    ``int` `n = ``sizeof``(a)/``sizeof``(a); ` `    ``cout << seg(a, n); ` `    ``return` `0; ` `} `

## Java

 `// Java program to find longest increasing subarray  ` `// with one change allowed. ` `class` `GFG ` `{ ` ` `  `    ``// Function to find length of ` `    ``// subsequence ` `    ``static` `int` `seg(``int``[] a, ``int` `n) ` `    ``{ ` `        ``int``[] l = ``new` `int``[n]; ` `        ``int``[] r = ``new` `int``[n + ``1``]; ` `        ``int` `ans = ``0``; ` ` `  `        ``// calculating the l array. ` `        ``l[``0``] = ``1``; ` `        ``for` `(``int` `i = ``1``; i < n; i++) ` `        ``{ ` `            ``if` `(a[i] > a[i - ``1``]) ` `            ``{ ` `                ``l[i] = l[i - ``1``] + ``1``; ` `                ``ans = Math.max(ans, l[i]); ` `            ``}  ` `            ``else` `                ``l[i] = ``1``; ` `        ``} ` `        ``if` `(ans != n) ` `            ``++ans; ` ` `  `        ``// calculating the r array. ` `        ``r[n] = ``0``; ` `        ``for` `(``int` `i = n - ``1``; i > ``0``; i--) ` `        ``{ ` `            ``if` `(a[i - ``1``] < a[i]) ` `                ``r[i] = r[i + ``1``] + ``1``; ` `            ``else` `                ``r[i] = ``1``; ` `        ``} ` ` `  `        ``// updating the answer. ` `        ``for` `(``int` `i = n - ``2``; i > ``0``; i--)  ` `        ``{ ` `            ``if` `(a[i + ``1``] - a[i - ``1``] > ``1``) ` `                ``ans = Math.max(ans, l[i - ``1``] +  ` `                                    ``r[i + ``1``] + ``1``); ` `        ``} ` `        ``return` `Math.max(ans, r[``0``]); ` `    ``} ` ` `  `    ``// Driver Code ` `    ``public` `static` `void` `main(String[] args)  ` `    ``{ ` `        ``int``[] a = { ``9``, ``4``, ``5``, ``1``, ``13` `}; ` `        ``int` `n = a.length; ` ` `  `        ``System.out.println(seg(a, n)); ` `    ``} ` `} ` ` `  `// This code is contributed by ` `// sanjeev2552 `

Output:

```4
```

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 contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

My Personal Notes arrow_drop_up

Improved By : sanjeev2552

Article Tags :
Practice Tags :

1

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.