algoblazerEarly Access
LearnProblemsLeaderboard
Log inSign up

All problems

‹ Back to map
4 shown · 0/261 solved

Maximum Sum With No Two Consecutive Skips

SilverCommunity Beta
Asked atOracle
Solve problem →
2000ms256MBAdded Sep 4, 2026

You are given an integer array nums (values may be negative). Walk through it from left to right and, for each element, either take it (adding it to your total) or skip it, but you may never skip two consecutive elements. Taking every element is allowed, and the very first element may be skipped.

Return the maximum total you can collect.

Examples

Example 1

Input: nums = [3,-1,4]
Output: 7
Explanation: Take 3, skip -1, take 4.

Example 2

Input: nums = [-5,-6,-7]
Output: -6
Explanation: Skip -5, take -6, skip -7. Skipping both -5 and -7 is fine because they are not consecutive. Any other pattern takes at least two elements.

Example 3

Input: nums = [-2,5,-1,-1,3]
Output: 7
Explanation: Skip -2, take 5, skip the first -1, then the second -1 must be taken, take 3: 5 - 1 + 3 = 7.

Constraints

  • 1≤1 \leq1≤ nums.length ≤105\leq 10^5≤105
  • −109≤-10^9 \leq−109≤ nums[i] ≤109\leq 10^9≤109

Details

Solved by5 people
Time limit2000ms
Memory256MB
AddedSep 4, 20268 days ago