From de4968b8d959d694d2fc25b990973f70a97293a3 Mon Sep 17 00:00:00 2001 From: Dhruv Patel Date: Fri, 26 Sep 2025 22:15:55 -0400 Subject: [PATCH 1/2] Max sum without taking consecutive elements using recursion and memoization --- src/algorithms/dp/MaxSumNoConsecutive.java | 50 ++++++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 src/algorithms/dp/MaxSumNoConsecutive.java diff --git a/src/algorithms/dp/MaxSumNoConsecutive.java b/src/algorithms/dp/MaxSumNoConsecutive.java new file mode 100644 index 0000000..bc3f8ea --- /dev/null +++ b/src/algorithms/dp/MaxSumNoConsecutive.java @@ -0,0 +1,50 @@ +package algorithms.dp; + +import java.util.Arrays; + +public class MaxSumNoConsecutive { + public static void main(String[] args) { + int[] arr = {8, 7, 6, 10}; + int n = arr.length; + + System.out.println("Max sum without taking two consecutive elements : "+maxSumRec(arr, n)); //18 (from 8 and 10) + + int[] memo = new int[n+1]; + Arrays.fill(memo, -1); + System.out.println("Max sum without taking two consecutive elements : "+maxSumMemo(arr, n, memo)); + } + + //Time Complexiy : O(2^n), Space Complexity: O(n) for recurvise call stack + private static int maxSumRec(int[] arr, int n){ + if(n <= 0) + return 0; + + if(n == 1) + return arr[0]; + + if(n == 2) + return Math.max(arr[0], arr[1]); + + //either skip curr element or take curr element and skip one before it + return Math.max(maxSumRec(arr, n-1), arr[n-1] + maxSumRec(arr, n-2)); + } + + //Same procedure as recursive approach + //Time Complexiy : O(n),t Space Complexity: O(n) for memo and recurvise call stack + private static int maxSumMemo(int[] arr, int n, int[] memo){ + if(n <= 0) + return 0; + + //returning result if computed previously + if(memo[n]!=-1) + return memo[n]; + + if(n == 1) + return memo[n] = arr[0]; + + if(n == 2) + return memo[n] = Math.max(arr[0], arr[1]); + + return memo[n] = Math.max(maxSumMemo(arr, n-1, memo), arr[n-1] + maxSumMemo(arr, n-2, memo)); + } +} From ee120724dff9366ac02ef93a9b8f009c861380eb Mon Sep 17 00:00:00 2001 From: Dhruv Patel Date: Sat, 27 Sep 2025 20:45:01 -0400 Subject: [PATCH 2/2] Tabulation method with optimized space complexity and added problem brief introduction --- src/algorithms/dp/MaxSumNoConsecutive.java | 62 +++++++++++++++++++++- 1 file changed, 61 insertions(+), 1 deletion(-) diff --git a/src/algorithms/dp/MaxSumNoConsecutive.java b/src/algorithms/dp/MaxSumNoConsecutive.java index bc3f8ea..aef1b63 100644 --- a/src/algorithms/dp/MaxSumNoConsecutive.java +++ b/src/algorithms/dp/MaxSumNoConsecutive.java @@ -2,6 +2,26 @@ import java.util.Arrays; +/* + * Maximum Sum of Non-Consecutive Elements + * + * Given an array of integers, find the maximum sum of elements such that + * no two chosen elements are adjacent in the array. + * + * This is a classic Dynamic Programming problem, often seen as a variation + * of the "House Robber Problem". + * + * Approaches: + * 1. Pure Recursion → Exponential time (O(2^n)), O(n) stack space + * 2. Recursion with Memoization → O(n) time, O(n) space + * 3. Tabulation (Bottom-Up DP) → O(n) time, O(n) space + * 4. Space-Optimized DP → O(n) time, O(1) space + * + * Example: + * arr = [8, 7, 6, 10] + * → Max sum = 18 (from 8 + 10) + */ + public class MaxSumNoConsecutive { public static void main(String[] args) { int[] arr = {8, 7, 6, 10}; @@ -12,6 +32,8 @@ public static void main(String[] args) { int[] memo = new int[n+1]; Arrays.fill(memo, -1); System.out.println("Max sum without taking two consecutive elements : "+maxSumMemo(arr, n, memo)); + + System.out.println("Max sum without taking two consecutive elements : "+maxSumTabulation(arr, n)); } //Time Complexiy : O(2^n), Space Complexity: O(n) for recurvise call stack @@ -30,7 +52,7 @@ private static int maxSumRec(int[] arr, int n){ } //Same procedure as recursive approach - //Time Complexiy : O(n),t Space Complexity: O(n) for memo and recurvise call stack + //Time Complexiy : O(n), Space Complexity: O(n) for memo and recurvise call stack private static int maxSumMemo(int[] arr, int n, int[] memo){ if(n <= 0) return 0; @@ -47,4 +69,42 @@ private static int maxSumMemo(int[] arr, int n, int[] memo){ return memo[n] = Math.max(maxSumMemo(arr, n-1, memo), arr[n-1] + maxSumMemo(arr, n-2, memo)); } + + //Time Complexiy : O(n), Space Complexity: O(n) + private static int maxSumTabulation(int[] arr, int n){ + + if(n == 0) + return 0; + if(n == 1) + return arr[0]; + + if(n == 2) + return Math.max(arr[0], arr[1]); + + + //It will have Space complexity O(n) + /*int[] dp = new int[n]; + + dp[0] = arr[0]; + dp[1] = Math.max(arr[0], arr[1]); + + for(int i=2; i