Maximum Points You Can Obtain from Cards
Take exactly k cards, one at a time, from either end of the row. Return the maximum score obtainable.
Intuition
There are 2^k orders in which to take cards, but only k + 1 distinct outcomes: whatever you do, you end up with some prefix of length i and some suffix of length k - i. Order does not affect the total. So start with all k cards taken from the left, then repeatedly hand one back from the right end of that prefix and take one more from the far right instead — k + 1 splits, each an O(1) update.
Approach
Collapse the choice order
Taking left-then-right and right-then-left yield the same two cards and the same score. What matters is only how many came from each end. Since exactly k are taken, the outcome is fully described by one number i: the count taken from the left, with k - i from the right. That reduces 2^k orderings to k + 1 cases.
Slide between the splits in O(1)
Compute the sum of the first k cards — the i = k case. To move to i = k - 1, subtract the last card of the prefix and add the last card of the array. Each subsequent split is another subtract-and-add. Track the running maximum across all k + 1 splits. Total work: one O(k) setup and O(k) updates.
The complementary framing
An equivalent and sometimes cleaner view: since you take k cards from the ends, the cards you leave behind form a contiguous window of length n - k in the middle. Maximising what you take is minimising that window's sum, which is a fixed-size sliding-window minimum. Both are O(n); the prefix/suffix version avoids computing the total and reads more directly from the problem statement.
Solution & live demo
Edge cases
All cards are taken, so the answer is the total. The loop still runs and every split gives the same sum.
Only two splits exist — the first card or the last — and the maximum of the two is returned.
LeetCode constrains points to be positive, but the algorithm needs no change if they were not: it compares totals, never assuming any card is worth taking.
Every split gives the same total, which is returned.