Dsa
Dynamic Programming
Warm-up
Maximize Non-Consecutive Candies
You have a row of 'n' candy jars, each containing a certain number of candies. The goal is to maximize the number of candies you can collect, but you cannot take candies from two adjacent jars.
Write a function that takes the number of jars and their respective candy counts and returns the maximum number of candies you can collect without taking from adjacent jars.
Input Format:
- The first line contains the integer n (1 <= n <= 100).
- The second line contains n space-separated integers representing the number of candies in each jar.
Output Format:
- An integer that is the maximum number of candies you can collect.
Example:
Input:
5
1 2 3 4 5
Output:
9
Note: Taking from the jars with 3 and 5 gives the maximum non-adjacent total.
Key concepts
dynamic_programminggreedyarray
Practise this out loud — free
Start a mock interview on THIS exact question — a voice AI interviewer opens with it, pushes back like a real onsite, then hands you an instant scorecard.
🎙 Practise this question now