Maximize sum of values in total K steps starting at position M
Given a sorted array arr[] containing N pairs [A, B], wherer A is the position in X axis and B is the value on that position. All the positions are distinct. The array is sorted in increasing order of position.
Given two integers M and K. The task is to maximize the sum of values that can be visited starting from the position M and taking total K steps where:
- One can move to right or left by 1 position (from x to x+1 or x-1) by taking 1 step.
- The value of any position can be added to the final sum only once.
- There is no movement along the Y axis.
Examples:
Input: arr[][] = {{2, 8}, {6, 3}, {8, 6}}, M = 5, K = 4
Output: 9
Explanation: The optimal way is to:
Move right to position 6 and add 3. Movement 1 position.
Move right to position 8 and add 6. Movement 2 position.
Total movement 3 steps and sum = 3 + 6 = 9.Input: arr[][] = {{1, 7}, {3, 6}, {4, 5}, {6, 5}}, M = 3, K = 4
Output: 18
Explanation: The optimal way of movement is:
Add value at position 3 to answer.
Move right to position 4 and add 5.
Move left to position 1 and add 7.
Total sum = 6 + 5 + 7 = 18.
Notice that position 3 can be visited twice but value is added only once.Input: arr[][] = {{0, 3}, {6, 4}, {8, 5}}, M = 3, K = 2
Output: 0
Explanation: Movement can be most K = 2 positions and cannot reach any position with points .
Approach: The approach is based on the concept of prefix sum. The range which can be covered standing at any position that needs to be determined at then the total points can be calculated from prefix sum array. Follow the steps below:
- Find the Prefix Sum for the maximum possible position. After that, perform two loops.
- The first loop will be when travelling to the right first and then to the left is considered.
- The second loop will occur when moving to the left first, then to the right is considered.
- The maximum number of Seeds covered in all feasible ranges is the final answer.
Below is the implementation of the above approach
C++
// C++ code to implement above approach#include <bits/stdc++.h>using namespace std; // Function to calculate maximum points // that can be collectedint maxTotalPoints(vector<vector<int> >& arr, int M, int K){ int MX = 2e5 + 2; int i, l, r, ans = 0; // Incremented positions by one // to make calculations easier. M++; vector<int> prefix_sum(MX); for (auto& it : arr) prefix_sum[it[0] + 1] = it[1]; for (i = 1; i < MX; i++) prefix_sum[i] += prefix_sum[i - 1]; for (r = M; r < MX && r <= M + K; r++) { l = min(M, M - (K - 2 * (r - M))); l = max(1, l); ans = max(ans, prefix_sum[r] - prefix_sum[l - 1]); } for (l = M; l > 0 && l >= M - K; l--) { r = max(M, M + (K - 2 * (M - l))); r = min(MX - 1, r); ans = max(ans, prefix_sum[r] - prefix_sum[l - 1]); } return ans;} // Driver codeint main(){ vector<vector<int>> arr{{2, 8}, {6, 3}, {8, 6}}; int M = 5; int K = 4; cout<<maxTotalPoints(arr, M, K); return 0;} |
9
Time Complexity: O(X), where X is the maximum position
Auxiliary Space: O(X)


