Algorithm to find maximum sum of elements in an array such that not more than k elements are adjacent
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
Introduction
This problem asks you to choose array elements so the total sum is as large as possible, with one constraint: you may not select more than k consecutive elements. That makes it a dynamic-programming problem, because the best choice at position i depends on how many consecutive elements have already been selected immediately before it.
Core Sections
Define the state correctly
A one-dimensional recurrence is usually not enough for this problem because the decision at each index depends on the current run length of chosen adjacent elements. A better state is:
- '
dp[i][c]= the best sum after processing the firstielements, wherecis the number of consecutive selected elements at the end'
Here:
- '
igoes from0ton' - '
cgoes from0tok' - '
c = 0means theith processed position was not selected, so the run resets'
This state captures exactly the information needed for the next decision.
The recurrence
At each element arr[i], you have two choices.
- Skip it. Then the run length resets to
0. - Take it. Then the previous run length must have been less than
k, and the new run length becomes the previous run length plus1.
That gives a natural transition.
This works for positive, mixed, and negative arrays because the skip transition is always available.
Why the state works
The reason this dynamic program is correct is that the future only needs to know one thing about the past: how many chosen elements are currently adjacent in a row. It does not need the full history.
That is the standard dynamic-programming pattern:
- keep only the information required for the next decision
- discard the rest
If you try to compress the problem into a simpler recurrence without tracking run length, you usually lose the ability to enforce the adjacency limit correctly.
Time and space complexity
The table has n * (k + 1) states, and each state performs constant work. So:
- time complexity is
O(nk) - space complexity is
O(nk)for the full table
You can reduce space to O(k) by keeping only the previous row, because each row depends only on the one before it.
The optimized version is usually the better production implementation.
Relationship to the classic house-robber problem
If k = 1, the problem becomes a familiar special case: you may not take two adjacent elements. That is essentially the classic house-robber constraint.
For larger k, the problem generalizes from “no two adjacent” to “no run longer than k.” Thinking of it that way helps when you sanity-check the recurrence.
Common Pitfalls
- Using a one-dimensional recurrence that ignores the current run length usually enforces the wrong constraint.
- Assuming the problem is trivial when all numbers are positive misses the fact that long runs still have to be broken once they exceed
k. - Forgetting the skip transition makes the algorithm unable to reset the consecutive count.
- Hard-coding the logic for
k = 1and then trying to generalize it without redesigning the state usually fails. - Ignoring negative values can produce incorrect logic if the algorithm assumes taking more elements is always better.
Summary
- The right dynamic-programming state must track how many selected elements are currently adjacent in a row.
- A clean recurrence is
O(nk)in time and can be reduced toO(k)space. - The key transitions are “skip and reset the run” or “take and extend the run if allowed.”
- The classic non-adjacent maximum-sum problem is the special case
k = 1. - Once the state is defined correctly, the implementation becomes straightforward and reliable.
Related reading
- Algorithm to find minimum spanning tree of chosen vertices
- Algorithm to find multiple string matches
- Algorithm to find next greater permutation of a given string
- Algorithm to find non-dominated pairs
- Algorithm to find peaks in 2D array
- algorithm to find the largest area
- Algorithm to find optimal groups
- Algorithm to find out whether the matches for two Glob patterns or Regular Expressions intersect

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 courseTrack 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.