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

Dynamic Programming Β· Guru Β· question 90 of 100

Implement the solution to the Maximum Sum Subsequence with Non-Adjacent Elements and Constraints problem using dynamic programming.?

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

The Maximum Sum Subsequence with Non-Adjacent Elements and Constraints problem is to find the maximum sum of a subsequence in an array of non-negative integers, where no adjacent elements in the subsequence are allowed and the subsequence must satisfy some additional constraints.

To solve this problem using dynamic programming, we can define an array β€˜dpβ€˜ where β€˜dp[i]β€˜ represents the maximum sum of a non-adjacent subsequence ending at index β€˜iβ€˜. We can start the array with β€˜dp[0] = arr[0]β€˜ and β€˜dp[1] = max(arr[0], arr[1])β€˜.

For each subsequent index β€˜iβ€˜ in the array, we can have two possible scenarios:

1. We include β€˜arr[i]β€˜ in the subsequence. In this case, we cannot include β€˜arr[i-1]β€˜ in the subsequence, so we need to look at β€˜dp[i-2]β€˜ (since β€˜dp[i-1]β€˜ would contain adjacent element β€˜arr[i-1]β€˜). The maximum sum must be β€˜arr[i] + dp[i-2]β€˜, so we can set β€˜dp[i] = arr[i] + dp[i-2]β€˜ in this case.

2. We exclude β€˜arr[i]β€˜ from the subsequence. In this case, we can consider the maximum sum that can be achieved up to index β€˜i-1β€˜, which is β€˜dp[i-1]β€˜. So β€˜dp[i] = dp[i-1]β€˜ in this case.

Finally, the maximum sum of a non-adjacent subsequence in the array would be the maximum value in the β€˜dpβ€˜ array, which can be obtained by iterating through the array and keeping track of the maximum value seen.

Here is the Java code to implement this solution:

public int maxSumNonAdjacent(int[] arr) {
    int n = arr.length;
    int[] dp = new int[n];
    dp[0] = arr[0];
    dp[1] = Math.max(arr[0], arr[1]);
    
    for (int i = 2; i < n; i++) {
        // Include arr[i] in subsequence
        int sum1 = arr[i] + dp[i-2];
        
        // Exclude arr[i] from subsequence
        int sum2 = dp[i-1];
        
        // Take maximum of the two cases
        dp[i] = Math.max(sum1, sum2);
    }

    // Find maximum value in dp array
    int maxSum = dp[0];
    for (int i = 1; i < n; i++) {
        maxSum = Math.max(maxSum, dp[i]);
    }
    return maxSum;
}

Let’s take an example of an array β€˜[1, 2, 3, 1]β€˜.

- For β€˜i=0β€˜, β€˜dp[0] = 1β€˜.

- For β€˜i=1β€˜, β€˜dp[1] = 2β€˜ (since we can choose either β€˜1β€˜ or β€˜2β€˜ as maximum sum contiguous subsequence).

- For β€˜i=2β€˜, we can either include or exclude β€˜3β€˜. If we include it, the maximum sum that can be achieved till this index is β€˜dp[0] + 3 = 4β€˜. If we exclude it, the maximum sum that can be achieved till this index is β€˜dp[1] = 2β€˜. So β€˜dp[2] = 4β€˜.

- For β€˜i=3β€˜, we can either include or exclude β€˜1β€˜. If we include it, the maximum sum that can be achieved till this index is β€˜dp[1] + 1 = 3β€˜. If we exclude it, the maximum sum that can be achieved till this index is β€˜dp[2] = 4β€˜. So β€˜dp[3] = 4β€˜.

Therefore, the maximum sum of a non-adjacent subsequence in the array is β€˜4β€˜.

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