
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Program to find maximum sum of two sets where sums are equal in C++
Suppose we have a list of numbers called nums, now find two sets as their sums are same and maximum, then find sum value.
So, if the input is like nums = [2, 5, 4, 6], then the output will be 6, as the sets are [2, 4] and [6].
To solve this, we will follow these steps −
- sum := 0
- for each number i in nums, do
- sum := sum + i
- n := size of nums
- Define one 2D array dp of size (n + 1) x (2 * sum + 5) and fill with -1
- dp[0, sum] := 0
- for initialize i := 1, when i <= n, update (increase i by 1), do −
- x := nums[i - 1]
- for initialize j := 0, when j < 2 * sum + 5, update (increase j by 1), do −
- if j - x >= 0 and dp[i - 1, j - x] is not equal to -1, then ^−
- dp[i, j] := maximum of dp[i, j] and (dp[i - 1, j - x] + x)
- if j + x < (2 * sum + 5) and dp[i - 1, j + x] is not equal to -1, then −
- dp[i, j] := maximum of dp[i, j] and (dp[i - 1, j + x])
- dp[i, j] := maximum of dp[i, j] and dp[i - 1, j]
- if j - x >= 0 and dp[i - 1, j - x] is not equal to -1, then ^−
- return dp[n, sum]
Example (C++)
Let us see the following implementation to get better understanding −
#include <bits/stdc++.h> using namespace std; class Solution { public: int solve(vector<int>& nums) { int sum = 0; for (int i : nums) sum += i; int n = nums.size(); vector<vector<int> > dp(n + 1, vector<int>(2 * sum + 5, -1)); dp[0][sum] = 0; for (int i = 1; i <= n; i++) { int x = nums[i - 1]; for (int j = 0; j < 2 * sum + 5; j++) { if (j - x >= 0 && dp[i - 1][j - x] != -1) { dp[i][j] = max(dp[i][j], dp[i - 1][j - x] + x); } if (j + x < 2 * sum + 5 && dp[i - 1][j + x] != -1) { dp[i][j] = max(dp[i][j], dp[i - 1][j + x]); } dp[i][j] = max(dp[i][j], dp[i - 1][j]); } } return dp[n][sum]; } }; int solve(vector<int>& nums) { return (new Solution())->solve(nums); } main(){ vector<int> v = {2, 5, 4, 6}; cout << solve(v); }
Input
{2, 5, 4, 6}
Output
6
- Related Articles
- Maximum sum subarray having sum less than or equal to given sums in C++
- Program to split a set into equal sum sets where elements in first set are smaller than second in Python
- Program to find index whose left and right elements sums are equal in Python
- Program to find maximum sum of two non-overlapping sublists in Python
- Find maximum sum possible equal sum of three stacks in C++
- Program to find maximum sum of the subsequence, where difference of two values is same as their position difference in Python
- Program to find two pairs of numbers where difference between sum of these pairs are minimized in python
- Program to find range sum of sorted subarray sums using Python
- Python Program to find Duplicate sets in list of sets
- Program to find minimum operations needed to make two arrays sum equal in Python
- Program to find number of ways where square of number is equal to product of two numbers in Python
- Program to find sum of contiguous sublist with maximum sum in Python
- Maximum Sum of Products of Two Array in C++ Program
- Find four factors of N with maximum product and sum equal to N - Set-2 in Python Program
- Program to find sum of the 2 power sum of all subarray sums of a given array in Python

Advertisements