Dynamic Programming
Algorithms
Maximum Sum Problem
Non-Consecutive Elements
Computational Problem Solving

Maximum sum of non consecutive elements

Data Structures & Algorithms practice on Codemia

Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.

Practice algorithms

Introduction

The problem of finding the maximum sum of non-consecutive elements in an array is a classic example of a dynamic programming problem. It asks you to identify the largest possible sum you can get from an array or a list of numbers without picking two adjacent elements. This problem often serves as a gateway to understanding more complex dynamic programming techniques.

Problem Definition

Given an array AA of integers, the task is to find the maximum sum possible by selecting non-consecutive elements from this array.

Example

Consider the array A=[3,2,5,10,7]A = [3, 2, 5, 10, 7]. You want to find the maximum sum you can achieve by selecting non-consecutive numbers.

  1. If you select 33, the next eligible elements are 55, 1010, and 77.
  2. If you select 22, the next eligible elements are 1010 and 77.
  3. If you select 55, the next eligible elements are 77.

• Optimal selection in this case would be 3,103, 10, yielding a sum of 1313. • Alternatively, 3,5,73, 5, 7 also yields a sum of 1515. • However, the most optimal solution is 5,105, 10 which provides a sum of 1515.

Dynamic Programming Solution

The key idea here is to build up a solution incrementally using dynamic programming. We will maintain an array dpdp where dp[i]dp[i] will store the maximum sum we can get including the ithi^{th} element such that no two elements are adjacent.

Recursive Relationship

The recursive relationship can be defined as:

dp[i]=max(dp[i1],A[i]+dp[i2])dp[i] = \max(dp[i-1], A[i] + dp[i-2])

Where: • dp[i1]dp[i-1] is the maximum sum without including the ithi^{th} element. • A[i]+dp[i2]A[i] + dp[i-2] is the maximum sum including the ithi^{th} element and not including the consecutive i1thi-1^{th} element.

Base Cases

dp[0]=A[0]dp[0] = A[0]: If there is only one element, the maximum sum is that element itself. • dp[1]=max(A[0],A[1])dp[1] = \max(A[0], A[1]): For two elements, select the larger of the two.

Implementation

Here is a Python implementation of the above logic:

House Robber Problem: Finding the maximum amount a robber can rob without alerting police when houses are in a straight line. • Stock Market Analysis: To find the largest sum of gains from stocks in a non-consecutive timeframe.


Related reading
Course
Intermediate
27 lessons
15 hours
DSA Fundamentals

Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Data Structures & Algorithms practice on Codemia

Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.

Practice algorithms

All Rights Reserved.