WalzoneInterview Prep
πŸ“ž Interviewing soon? Practice with a realistic AI mock phone interview β€” it calls you, then scores you. First 15 min FREE β†’

Dynamic Programming Β· Expert Β· question 75 of 100

Solve the problem of finding the Longest Increasing Subarray with One Change using dynamic programming.?

πŸ“• Buy this interview preparation book: 100 Dynamic Programming questions & answers β€” PDF + EPUB for $5

The problem of finding the Longest Increasing Subarray with One Change can be solved efficiently using dynamic programming. The problem statement can be summarized as follows: given an array of integers, find the length of the longest subarray that can be made into a strictly increasing sequence by changing at most one element.

Let us approach this problem using dynamic programming. We can define a one-dimensional array β€˜dpβ€˜ of size β€˜nβ€˜, where β€˜nβ€˜ is the length of the input array β€˜arrβ€˜. The β€˜ithβ€˜ element of array β€˜dpβ€˜ stores the length of the longest increasing subarray ending at index β€˜iβ€˜, with at most one change allowed.

We can start by initializing β€˜dp[0]β€˜ to β€˜1β€˜, as the longest increasing subarray ending at the first element of the array is just the first element itself. Then, for each subsequent element, we can consider two cases: either we include the current element in the increasing subarray, or we do not.

If we include the current element in the subarray, we can check if by changing this element, we can extend the previous longest increasing subarray ending at β€˜i-1β€˜. If changing the element at β€˜iβ€˜ allows us to form a strictly increasing subarray with the previous elements, then the length of the longest increasing subarray ending at index β€˜iβ€˜ is β€˜dp[i-1] + 1β€˜.

Otherwise, we cannot include this element in the subarray, and the length of the longest increasing subarray ending at index β€˜iβ€˜ is just β€˜1β€˜. So, β€˜dp[i]β€˜ will be set to β€˜1β€˜ in this case.

We can iterate through the array β€˜arrβ€˜ from left to right, and build the β€˜dpβ€˜ array as we go. After we have filled up the β€˜dpβ€˜ array, the maximum value in β€˜dpβ€˜ will be the length of the longest increasing subarray with at most one change.

Here is the Java code implementing the above approach:

public static int longestIncreasingSubarrayWithOneChange(int[] arr) {
    int n = arr.length;
    int[] dp = new int[n];
    dp[0] = 1;
    int maxLen = 1;
    
    for (int i = 1; i < n; i++) {
        if (arr[i] > arr[i-1]) {
            dp[i] = dp[i-1] + 1;
            maxLen = Math.max(maxLen, dp[i]);
        } else {
            dp[i] = 1;
        }
    }
    
    for (int i = 1; i < n-1; i++) {
        if (arr[i-1] < arr[i+1]) {
            maxLen = Math.max(maxLen, dp[i-1]+dp[i+1]);
        }
    }
    
    return maxLen;
}

In the above code, we first initialize β€˜dp[0]β€˜ to β€˜1β€˜. Then, in the loop from β€˜1β€˜ to β€˜n-1β€˜, we consider two cases: either β€˜arr[i]β€˜ is greater than β€˜arr[i-1]β€˜, in which case we can extend the longest increasing subarray ending at index β€˜i-1β€˜ to include this element, or β€˜arr[i]β€˜ is not greater than β€˜arr[i-1]β€˜, in which case the longest increasing subarray ending at index β€˜iβ€˜ will be just the current element. We track the maximum length seen so far in the β€˜maxLenβ€˜ variable.

After we have filled up the β€˜dpβ€˜ array, we iterate over the array again using another loop from β€˜1β€˜ to β€˜n-2β€˜. Here, we consider a subarray of length 3, consisting of β€˜arr[i-1]β€˜, β€˜arr[i]β€˜ and β€˜arr[i+1]β€˜. If this subarray is non-decreasing, i.e., β€˜arr[i-1] < arr[i] <= arr[i+1]β€˜, then by replacing β€˜arr[i]β€˜ with the maximum of β€˜arr[i-1]β€˜ and β€˜arr[i+1]β€˜, we can potentially extend the longest increasing subarray. We compute the length of the new extended subarray as β€˜dp[i-1] + dp[i+1]β€˜, and update β€˜maxLenβ€˜ if this value is greater.

Finally, we return β€˜maxLenβ€˜ as the result.

This approach has a time complexity of O(n) and a space complexity of O(n), as we are storing the intermediate results in the β€˜dpβ€˜ array.

Reading is step one. Saying it out loud is the interview. Our AI interviewer calls your phone and runs a realistic Dynamic Programming interview β€” then scores it.
πŸ“ž Practice Dynamic Programming β€” free 15 min
πŸ“• Buy this interview preparation book: 100 Dynamic Programming questions & answers β€” PDF + EPUB for $5

All 100 Dynamic Programming questions Β· All topics