|
| 1 | +package algorithms.dp; |
| 2 | + |
| 3 | +import java.util.Arrays; |
| 4 | + |
| 5 | +/* |
| 6 | + * Maximum Sum of Non-Consecutive Elements |
| 7 | + * |
| 8 | + * Given an array of integers, find the maximum sum of elements such that |
| 9 | + * no two chosen elements are adjacent in the array. |
| 10 | + * |
| 11 | + * This is a classic Dynamic Programming problem, often seen as a variation |
| 12 | + * of the "House Robber Problem". |
| 13 | + * |
| 14 | + * Approaches: |
| 15 | + * 1. Pure Recursion → Exponential time (O(2^n)), O(n) stack space |
| 16 | + * 2. Recursion with Memoization → O(n) time, O(n) space |
| 17 | + * 3. Tabulation (Bottom-Up DP) → O(n) time, O(n) space |
| 18 | + * 4. Space-Optimized DP → O(n) time, O(1) space |
| 19 | + * |
| 20 | + * Example: |
| 21 | + * arr = [8, 7, 6, 10] |
| 22 | + * → Max sum = 18 (from 8 + 10) |
| 23 | + */ |
| 24 | + |
| 25 | +public class MaxSumNoConsecutive { |
| 26 | + public static void main(String[] args) { |
| 27 | + int[] arr = {8, 7, 6, 10}; |
| 28 | + int n = arr.length; |
| 29 | + |
| 30 | + System.out.println("Max sum without taking two consecutive elements : "+maxSumRec(arr, n)); //18 (from 8 and 10) |
| 31 | + |
| 32 | + int[] memo = new int[n+1]; |
| 33 | + Arrays.fill(memo, -1); |
| 34 | + System.out.println("Max sum without taking two consecutive elements : "+maxSumMemo(arr, n, memo)); |
| 35 | + |
| 36 | + System.out.println("Max sum without taking two consecutive elements : "+maxSumTabulation(arr, n)); |
| 37 | + } |
| 38 | + |
| 39 | + //Time Complexiy : O(2^n), Space Complexity: O(n) for recurvise call stack |
| 40 | + private static int maxSumRec(int[] arr, int n){ |
| 41 | + if(n <= 0) |
| 42 | + return 0; |
| 43 | + |
| 44 | + if(n == 1) |
| 45 | + return arr[0]; |
| 46 | + |
| 47 | + if(n == 2) |
| 48 | + return Math.max(arr[0], arr[1]); |
| 49 | + |
| 50 | + //either skip curr element or take curr element and skip one before it |
| 51 | + return Math.max(maxSumRec(arr, n-1), arr[n-1] + maxSumRec(arr, n-2)); |
| 52 | + } |
| 53 | + |
| 54 | + //Same procedure as recursive approach |
| 55 | + //Time Complexiy : O(n), Space Complexity: O(n) for memo and recurvise call stack |
| 56 | + private static int maxSumMemo(int[] arr, int n, int[] memo){ |
| 57 | + if(n <= 0) |
| 58 | + return 0; |
| 59 | + |
| 60 | + //returning result if computed previously |
| 61 | + if(memo[n]!=-1) |
| 62 | + return memo[n]; |
| 63 | + |
| 64 | + if(n == 1) |
| 65 | + return memo[n] = arr[0]; |
| 66 | + |
| 67 | + if(n == 2) |
| 68 | + return memo[n] = Math.max(arr[0], arr[1]); |
| 69 | + |
| 70 | + return memo[n] = Math.max(maxSumMemo(arr, n-1, memo), arr[n-1] + maxSumMemo(arr, n-2, memo)); |
| 71 | + } |
| 72 | + |
| 73 | + //Time Complexiy : O(n), Space Complexity: O(n) |
| 74 | + private static int maxSumTabulation(int[] arr, int n){ |
| 75 | + |
| 76 | + if(n == 0) |
| 77 | + return 0; |
| 78 | + if(n == 1) |
| 79 | + return arr[0]; |
| 80 | + |
| 81 | + if(n == 2) |
| 82 | + return Math.max(arr[0], arr[1]); |
| 83 | + |
| 84 | + |
| 85 | + //It will have Space complexity O(n) |
| 86 | + /*int[] dp = new int[n]; |
| 87 | +
|
| 88 | + dp[0] = arr[0]; |
| 89 | + dp[1] = Math.max(arr[0], arr[1]); |
| 90 | +
|
| 91 | + for(int i=2; i<n; i++){ |
| 92 | + dp[i] = Math.max(arr[i] + dp[i-2], dp[i-1]); |
| 93 | + } |
| 94 | + return dp[n-1];*/ |
| 95 | + |
| 96 | + //Optimized approach with Space Complexity O(1) |
| 97 | + |
| 98 | + int prev1 = arr[0]; |
| 99 | + int prev2 = Math.max(arr[0], arr[1]); |
| 100 | + |
| 101 | + for(int i=2; i<n; i++){ |
| 102 | + int curr = Math.max(arr[i] + prev1, prev2); |
| 103 | + |
| 104 | + prev1 = prev2; |
| 105 | + prev2 = curr; |
| 106 | + } |
| 107 | + |
| 108 | + return prev2; |
| 109 | + } |
| 110 | +} |
0 commit comments