Maximize sum of selected integers from an Array of pair of integers as per given condition
Given an array arr[] having N pair of integers of the form (x, y), the task is to maximize the sum y values in selected pairs such that if a pair (xi, yi) is selected, the next xi pairs cannot be selected.
Examples:
Input: arr[]= {{1, 5}, {2, 7}, {1, 4}, {1, 5}, {1, 10}}
Output: 19
Explanation: Choose the pair at the index i = 0 i.e, (1, 5). Hence the next 1 pair cannot be selected. Similarly, select the pairs at index 2 and 4 i.e, (1, 4) and (1, 10). Therefore, the sum of all the y values of the selected pairs is 5+4+10 = 19, which is the maximum possible.Input: arr[] = {{1, 5}, {2, 10}, {3, 3}, {7, 4}}
Output: 10
Approach: The given problem can be solved using dynamic programming. Create an array dp[], where dp[i] represents the maximum possible sum of selected elements of the array arr[] in the range [i, N) and initialize the value of all indices with 0. Hence, the dp[] array can be calculated using the below relation:
dp[i] = max( dp[i + 1], dp[i + xi + 1] + yi)
Therefore, calculate the value for each state in dp[i] for all values of i in the range (N, 0]. The value stored at dp[0] will be the required answer.
Below is the implementation of the above approach:
C++
// C++ program for the above approach #include <bits/stdc++.h> using namespace std; // Function to maximize sum of selected // integers from an array of pair // of integers as per given condition int maximizeSum(vector<pair< int , int > > arr, int N) { // Stores the DP states vector< int > dp(N + 1, 0); // Initial Condition dp[N - 1] = arr[N - 1].second; // Loop to traverse the array // in the range (N - 1, 0] for ( int i = N - 2; i >= 0; i--) { // If it is in range if (i + arr[i].first < N) // Calculate dp[i] dp[i] = max(dp[i + 1], dp[i + arr[i].first + 1] + arr[i].second); else dp[i] = dp[i + 1]; } // Return Answer return dp[0]; } // Driver Code int main() { vector<pair< int , int > > arr = { { 1, 5 }, { 2, 7 }, { 1, 4 }, { 1, 5 }, { 1, 10 } }; cout << maximizeSum(arr, arr.size()); return 0; } |
Java
// Java program for the above approach import java.io.*; import java.lang.*; import java.util.*; // User defined Pair class class Pair { int first; int second; // Constructor public Pair( int first, int second) { this .first = first; this .second = second; } } class GFG { // Function to maximize sum of selected // integers from an array of pair // of integers as per given condition static int maximizeSum(Pair arr[ ], int N) { // Stores the DP states int dp[] = new int [N + 1 ]; // Initial Condition dp[N - 1 ] = arr[N - 1 ].second; // Loop to traverse the array // in the range (N - 1, 0] for ( int i = N - 2 ; i >= 0 ; i--) { // If it is in range if (i + arr[i].first < N) // Calculate dp[i] dp[i] = Math.max(dp[i + 1 ], dp[i + arr[i].first + 1 ] + arr[i].second); else dp[i] = dp[i + 1 ]; } // Return Answer return dp[ 0 ]; } // Driver Code public static void main (String[] args) { int n = 5 ; // Array of Pair Pair arr[] = new Pair[n]; arr[ 0 ] = new Pair( 1 , 5 ); arr[ 1 ] = new Pair( 2 , 7 ); arr[ 2 ] = new Pair( 1 , 4 ); arr[ 3 ] = new Pair( 1 , 5 ); arr[ 4 ] = new Pair( 1 , 10 ); System.out.print(maximizeSum(arr, n)); } } // This code is contributed by hrithikgarg03188. |
Python3
# Python code for the above approach # Function to maximize sum of selected # integers from an array of pair # of integers as per given condition def maximizeSum(arr, N): # Stores the DP states dp = [ 0 ] * (N + 1 ) # Initial Condition dp[N - 1 ] = arr[N - 1 ][ "second" ] # Loop to traverse the array # in the range (N - 1, 0] for i in range (N - 2 , - 1 , - 1 ): # If it is in range if (i + arr[i][ "first" ] < N): # Calculate dp[i] dp[i] = max (dp[i + 1 ], dp[i + arr[i][ "first" ] + 1 ] + arr[i][ "second" ]) else : dp[i] = dp[i + 1 ] # Return Answer return dp[ 0 ] # Driver Code arr = [{ "first" : 1 , "second" : 5 }, { "first" : 2 , "second" : 7 }, { "first" : 1 , "second" : 4 }, { "first" : 1 , "second" : 5 }, { "first" : 1 , "second" : 10 } ] print (maximizeSum(arr, len (arr))) # This code is contributed by gfgking |
C#
// C# program for the above approach using System; using System.Collections.Generic; // User defined Pair class class Pair { public int first; public int second; // Constructor public Pair( int first, int second) { this .first = first; this .second = second; } } public class GFG { // Function to maximize sum of selected // integers from an array of pair // of integers as per given condition static int maximizeSum(Pair []arr, int N) { // Stores the DP states int []dp = new int [N + 1]; // Initial Condition dp[N - 1] = arr[N - 1].second; // Loop to traverse the array // in the range (N - 1, 0] for ( int i = N - 2; i >= 0; i--) { // If it is in range if (i + arr[i].first < N) // Calculate dp[i] dp[i] = Math.Max(dp[i + 1], dp[i + arr[i].first + 1] + arr[i].second); else dp[i] = dp[i + 1]; } // Return Answer return dp[0]; } // Driver Code public static void Main(String[] args) { int n = 5; // Array of Pair Pair []arr = new Pair[n]; arr[0] = new Pair(1, 5); arr[1] = new Pair(2, 7); arr[2] = new Pair(1, 4); arr[3] = new Pair(1, 5); arr[4] = new Pair(1, 10); Console.Write(maximizeSum(arr, n)); } } // This code is contributed by shikhasingrajput |
Javascript
<script> // JavaScript code for the above approach // Function to maximize sum of selected // integers from an array of pair // of integers as per given condition function maximizeSum(arr, N) { // Stores the DP states let dp = new Array(N + 1).fill(0); // Initial Condition dp[N - 1] = arr[N - 1].second; // Loop to traverse the array // in the range (N - 1, 0] for (let i = N - 2; i >= 0; i--) { // If it is in range if (i + arr[i].first < N) // Calculate dp[i] dp[i] = Math.max(dp[i + 1], dp[i + arr[i].first + 1] + arr[i].second); else dp[i] = dp[i + 1]; } // Return Answer return dp[0]; } // Driver Code let arr = [{ first: 1, second: 5 }, { first: 2, second: 7 }, { first: 1, second: 4 }, { first: 1, second: 5 }, { first: 1, second: 10 } ]; document.write(maximizeSum(arr, arr.length)); // This code is contributed by Potta Lokesh </script> |
19
Time Complexity: O(N)
Auxiliary Space: O(N)