# Solution For Sum Game

The Sum Game problem on LeetCode asks us to find if Alice can win a game against Bob with certain conditions. The game has two piles of stones, each pile has n stones and each stone has a weight represented by an array of integers. Alice and Bob take turns to remove a stone from either pile. The player who removes the stone with the smaller weight loses one point, and the player who removes the stone with the larger weight gains one point. The game continues until there are no more stones. Alice wants to know if she can win the game no matter how Bob plays.

To solve this problem, we need to find the difference between the sum of the weights of the stones in pile A and the sum of the weights of the stones in pile B. If this difference is not equal to zero, then Alice can win the game. If the difference is equal to zero, then Alice cannot win the game.

Let’s say the sum of weights of stones in pile A is Sa and sum of weights of stones in pile B is Sb. Then we need to check if the following two conditions are true:

- Sa != Sb
- Alice can choose a stone from either pile such that the difference in the sums, after removing that stone from the pile, is not equal to (Sa – Sb) / 2.

If both of these conditions are true, then Alice can win the game no matter how Bob plays. Otherwise, Alice cannot win the game.

To check the second condition, we can use prefix sums. We can calculate the prefix sums of the weights in each pile. Let the prefix sums of the weights in pile A be P and prefix sums of the weights in pile B be Q. Then Alice can choose a stone from pile A such that the difference in the sums after removing that stone is not equal to (Sa – Sb) / 2 if and only if there is an index i such that (P[i] – Q[i]) != ((Sa – Sb) / 2).

Similarly, Alice can choose a stone from pile B such that the difference in the sums after removing that stone is not equal to (Sa – Sb) / 2 if and only if there is an index i such that (Q[i] – P[i]) != ((Sa – Sb) / 2).

We can combine these two conditions and check if Alice can win the game as follows:

- Calculate the sum of weights of stones in pile A and the sum of weights of stones in pile B
- If Sa = Sb, return False
- Calculate the prefix sums of the weights in pile A and in pile B
- For each index i, check if (P[i] – Q[i]) = ((Sa – Sb) / 2) or (Q[i] – P[i]) = ((Sa – Sb) / 2). If yes, continue to the next index. If no, return True.

Here is the Python code to implement the above algorithm:

`def sumGame(nums: List[int]) -> bool:`

n = len(nums)

Sa = sum(nums[:n//2])

Sb = sum(nums[n//2:])

if Sa == Sb:

return False

P = [0] * (n//2 + 1)

Q = [0] * (n//2 + 1)

for i in range(n//2):

P[i+1] = P[i] + nums[i]
Q[i+1] = Q[i] + nums[n//2+i]
for i in range(n//2 + 1):

if abs(P[i] - Q[i]) == abs(Sa - Sb) // 2:

continue

else:

return True

return False

The time complexity of the above algorithm is O(n) and the space complexity is O(n).

## Step by Step Implementation For Sum Game

class Solution { public int getSum(int a, int b) { // Iterate till there is no carry while (b != 0) { // carry now contains common // set bits of x and y int carry = a & b; // Sum of bits of x and y where at // least one of the bits is not set a = a ^ b; // Carry is shifted by one so that // adding it to x gives the required sum b = carry << 1; } return a; } }

def getSum(a, b): # initialize result sum = 0 # Iterate through all bits for i in range(1, a+b): sum = sum + i return sum

//Given an array of integers, return indices of the two numbers such that they add up to a specific target. //You may assume that each input would have exactly one solution, and you may not use the same element twice. //Given nums = [2, 7, 11, 15], target = 9, //Because nums[0] + nums[1] = 2 + 7 = 9, //return [0, 1]. var twoSum = function(nums, target) { // create empty hashmap let map = {}; // loop through array for (let i = 0; i < nums.length; i++) { // if the complement of the current number exists in the map, return the indices of the current number and the number in the map if (map[target - nums[i]] !== undefined) { return [i, map[target - nums[i]]]; } // otherwise, add the current number and its index to the map map[nums[i]] = i; } };

int sumGame(vector& nums) { int n = nums.size(); // Base case if(n == 1) return nums[0]; // Create an array to store results of subproblems int dp[n]; // Fill values in dp[] using above recursive formula for(int i=0; i public class Solution { public int GetMaximum(int a, int b, int c) { // Create an array to store the sums int[] sums = new int[3]; // Get the sum of a and b sums[0] = a + b; // Get the sum of b and c sums[1] = b + c; // Get the sum of a and c sums[2] = a + c; // Sort the array Array.Sort(sums); // Return the largest sum return sums[2]; } }