Maximum Points You Can Obtain from Cards
Problem Statement
Some cards are laid out in a row, and card i is worth cardPoints[i] points.
You make exactly k moves. In each move you take one card from either end of the row — the leftmost or the rightmost card that is still there. Your score is the total of the cards you took.
Return the highest score you can get.
An integer array cardPoints and an integer k.
The maximum total points from taking exactly k cards, each from the left or right end.
Take the three rightmost cards: 1 + 6 + 5 = 12. No other choice does better.
Any two cards are worth 2 + 2 = 4.
k equals the number of cards, so you take all of them: the total is 55.
- • 1 ≤ cardPoints.length ≤ 10^5
- • 1 ≤ cardPoints[i] ≤ 10^4
- • 1 ≤ k ≤ cardPoints.length
Pattern Recognition Clues
Recognising these key signals in the problem description immediately identifies the pattern:
Approach Complexity Ladder
For each i in 0..k, add the first i and last k − i cards from scratch
Total minus the smallest sum of a size n − k window
Any sequence of moves ends with some i cards taken from the left and k − i from the right. Try every i from 0 to k, add up those cards from scratch, and keep the best total.
Recognition rule for this sub-pattern:
Unlock Complete Visual Walkthrough & Optimal C++
The interactive animated dry-run player, optimized C++ implementation, state invariant derivations, and edge-case breakdown for this question are part of Brute2Best Pro. The anchor question of every sub-pattern is completely free.
First anchor question per pattern, Part 1 LLD & Module 1 C++ basics are 100% Free forever.
How this concept connects to System Design & C++ Systems
Top engineering interviews test whether you understand how algorithms like Sliding Window translate into production architectures and low-level memory mechanics.