 All Problems
Max Points You Can Obtain from Cards
medium
sliding window
arrays
amazon
google
microsoft

There are several cards arranged in a row. You can take k cards from the beginning or end of the row, one at a time. Your score is the sum of the taken cards. Return the maximum score.

Example 1:

Input: 1 2 3 4 5 6 1
k: 3
Output: 12
Explanation: Take 1+6+5 = 12 from right side.

Example 2:

Input: 2 2 2
k: 2
Output: 4

Constraints:

  • 1 ≤ cards.length ≤ 10⁵
  • 1 ≤ cards[i] ≤ 10⁴
  • 1 ≤ k ≤ cards.length

Input format: First line: space-separated cards array. Second line: k.

Output format: Maximum score.

Run to check your code against the sample cases, or submit to run every case