Tug of War in C++
In this article, we will discuss the tug of war in C++ with its example.
Problem Statement:
Divide a set of n integers into two subsets of size n/2 each, keeping the absolute difference between the two subsets as small as feasible. If n is odd, one subset's size must be (n-1)/2, and the second subset's size must be (n+1)/2. If n is even, both subset sizes must be strictly n/2.
As an illustration, consider the following set:
3, 4, 5, -3, 100, 1, 89, 54, 23, 20.
This set has a size of 10. This set should provide the outputs 4, 100, 1, 23, 20, and 3, 5, -3, 89, 54.
The sum of the elements in both of the output subsets, which are both of size 5, is the same (148 and 148).
Consider yet another instance when n is odd. Give the set the following values: 23, 45, -34, 12, 0, 98, -99, 4, 189, -1, 4. It is recommended that the output subsets be 45, -34, 12, 98, -1 and 23, 0, -99, 4, 189, 4. Elements in two subsets have sums of 120 and 121, respectively.
The next approach attempts each potential half-size subset. The remaining items make up the other subset if just one subset of half the size is generated. We start with an empty set and divide it together one by one. Either each element can be a part of the current set, or it can be a part of the remaining elements (another subset). We take into account both scenarios for every constituent. We determine whether this solution is superior to the best one up until the size of the current set reaches n/2. If so, we revise the ideal solution.
Algorithm:
- Enter an array of n weights and the number n of weights.
- Make a total sum of all the weights.
- Make a DP table with the dimensions (n + 1) x (totalSum + 1), initialize every element to false, and then.
- Since an empty subset can result in a sum of 0, initialize the first column of the DP table as true.
- Using the following loop, fill the DP table:
- For i between 1 and n:
- From 1 to totalSum for j:
- dp[i][j] should be set to dp[i - 1][j].
- Set the value of dp[i][j] to dp[i][j] || dp[i - 1][j - weights[i - 1]] if weights[i - 1] are less than or equal to j.
- A minimum difference between the two groups should be found:
- Set minDiff's initial value to a high number, like INT_MAX.
- From totalSum / 2 down to 0, for j:
- Upon dp[n][j] being true:
- Break out of the loop after setting minDiff equal to totalSum - 2 * j.
- Give the minimum difference between two groups as the value of minDiff.
By taking into account all feasible subsets of weights, this technique employs dynamic programming to determine the minimal difference between two groups of weights.
Program:
#include <iostream>
int main() {
int teamAStrength, teamBStrength;
// Input strengths of both teams
std::cout << "Enter the strength of Team A: ";
std::cin >> teamAStrength;
std::cout << "Enter the strength of Team B: ";
std::cin >> teamBStrength;
while (teamAStrength > 0 && teamBStrength > 0) {
// Simulate a round
int pull = rand() % 11; // Random number between 0 and 10
std::cout << "Team A pulls " << pull << " units." << std::endl;
// Subtract the pull from Team B's strength
teamBStrength -= pull;
// Check if Team B's strength has reached zero
if (teamBStrength <= 0) {
std::cout << "Team A wins!" << std::endl;
break;
}
pull = rand() % 11;
std::cout << "Team B pulls " << pull << " units." << std::endl;
// Subtract the pull from Team A's strength
teamAStrength -= pull;
// Check if Team A's strength has reached zero
if (teamAStrength <= 0) {
std::cout << "Team B wins!" << std::endl;
break;
}
}
return 0;
}
Output:
![Tug of War in C++](https://static.tutorialandexample.com/cpp/tug-of-war-in-cpp1.png)
Code Explanation:
1. Initialization:
- Team A and Team B's strengths are the first things the program asks for comments on.
2. Main Game Loop:
- The while loop in the center of the program runs until one of the teams' strengths is equal to zero.
3. Turn of Team A:
- For Team A, a random pull strength (between 0 and 10) is created for each iteration of the loop. It replicates Team A's advantage during that round. Team B's strength is reduced by the pull strength.
4. Check for Team B’s Win:
- After Team A pulls, the program checks to see if Team B's strength has decreased to zero or has increased. If so, "Team A wins!" is printed and the loop is ended.
5. Turn of Team B:
- Now, it is Team B's turn if Team B's strength is still positive. Once more, Team B's pull strength is produced at random, and this pull strength is deducted from Team A's strength.
6. Check for Team A’s Win:
- After Team B pulls, the program checks to see if Team A's strength has decreased to zero or has increased. If so, "Team B wins!" is printed and the loop is ended.
7. Repeat or End:
- Repeat until one team's strength reaches zero or negative, at which point the game is over. When that occurs, the program publishes a message stating which team won before leaving the loop.
8. Programme End:
- After successfully running, the program ends by returning 0.