-
Notifications
You must be signed in to change notification settings - Fork 35
Description
🎯 Problem Information
- Problem Name:Maximum Sum of Non-Adjacent Elements
- CSES Link: N/A
- Category: Dynamic Programming
- Estimated Difficulty: [Medium]
📋 Problem Description
Brief description of the problem and what it asks for.
Given an array of positive integers, find the maximum sum of elements such that no two selected elements are adjacent in the array.
You can only choose elements that are not next to each other. The goal is to maximize the total sum of chosen numbers.
💡 Proposed Approach
Describe the algorithm/approach you plan to use:
Use a Dynamic Programming approach with space optimization:
Keep track of two values:
incl → max sum including the current element
excl → max sum excluding the current element
For each number:
Update incl as excl + nums[i]
Update excl as max(previous incl, previous excl)
The answer is max(incl, excl) after processing all elements.
Time Complexity: O(n)
Space Complexity: O(1)
📝 Implementation Notes
Any special considerations or optimizations needed:
- Requires fast I/O
- Needs modular arithmetic
- Large input constraints
- [ ✅] Edge cases to consider
🏷️ Labels
Please add appropriate labels:
- [ ✅]
hacktoberfest(if contributing during Hacktoberfest)
👤 Assignee
- [ ✅] I would like to work on this issue
📎 Additional Context
Add any other context or screenshots about the problem here.