Maximum Points You Can Obtain from Cards

Counting Subarrays and Fixed Window DSA practice problem on Onlearn.

Difficulty: medium.

Topics: Maximum point you can obtain from cards, Arrays, Sliding Window, Optimization, Time Complexity, Space Complexity, greedy algorithm, sliding window, two pointer technique, prefix sum.

You are given an integer array cardPoints and an integer k. You can take k cards from the table. In one step, you can take one card from the left end or from the right end of the row. Your goal is to obtain the maximum possible score. Input: An integer array cardPoints, where cardPoints[i] represents the points of the i th card. An integer k, representing the number of cards you can take. Output: The maximum score you can obtain. Constraints: 1 <= cardPoints.length <= 10^5 1 <= cardPoints[i] <= 10^4 1 <= k <= cardPoints.length Sample Input 1: cardPoints = [1,2,3,4,5,6,1], k = 3 Sample Output 1: 12 Explanation: You can obtain a score of 12 by taking the three rightmost cards (5, 6, and 1). Sample Input 2: cardPoints = [2,2,2], k = 2 Sample Output 2: 4 Explanation: You can obtain a score of 4 by taking the two leftmost cards (2, 2) or the two rightmost cards (2, 2). Sample Input 3: cardPoints = [9,7,7,9,7,7,9], k = 7 Sample Output 3: 55 Explanation: All cards are taken, so the sum of all cards is 55.