Given an array arr[], consisting of N non-negative integers and an integer S, the task is to find the number of ways to obtain the sum S by adding or subtracting array elements.

Note: All the array elements need to be involved in generating the sum.

Examples:

Input:_ arr[] = {1, 1, 1, 1, 1}, S = 3_

Output:_ 5_

Explanation:

Following are the possible ways to obtain the sum S:

  • -1 + 1 + 1 + 1 + 1 = 3
  • 1 -1 + 1 + 1 + 1 = 3
  • 1 + 1 – 1 + 1 + 1 = 3
  • 1 + 1 + 1 – 1 + 1 = 3
  • 1 + 1 + 1 + 1 – 1 = 3

Input:_ arr[] = {1, 2, 3, 4, 5}, S = 3_

Output:_ 3_

Explanation:

Following are the possible ways to obtain the sum S:

  • -1 -2 -3 + 4 + 5 = 3
  • -1 + 2 + 3 + 4 – 5 = 3
  • 1 – 2 + 3 – 4 + 5 = 3

Recursive** Approach:** It can be observed that each array element can either be added or subtracted to obtain sum. Therefore, for each array element, recursively check for both the possibilities and increase count when sum S is obtained after reaching the end of the array.

Below is the implementation of the above approach:

  • Java

// Java Program to implement

// the above approach

**import** java.io.*;

**class** GFG {

// Function to call dfs() to

// calculate the number of ways

**static** **int** findWays(``**int**``[] nums, **int** S)

{

**return** dfs(nums, S, 0``, 0``);

}

// Function to count the number of ways

**static** **int** dfs(``**int**``[] nums, **int** S,

**int** curr_sum, **int** index)

{

// Base Case: Reached the

// end of the array

**if** (index == nums.length) {

// Sum is equal to the

// required sum

**if** (S == curr_sum)

**return** 1``;

**else**

**return** 0``;

}

// Recursively check if required sum

// can be obtained by adding current

// element or by subtracting the

// current index element

**return** dfs(nums, S, curr_sum + nums[index],

index + 1``)

+ dfs(nums, S, curr_sum - nums[index],

index + 1``);

}

// Driver Code

**public** **static** **void** main(String[] args)

{

**int** S = 3``;

**int**``[] arr = **new** **int**``[] { 1``, 2``, 3``, 4``, 5 };

**int** answer = findWays(arr, S);

System.out.println(answer);

}

}

Output:

3

Time Complexity:_ O(2N)_

Auxiliary Space:_ O(1)_

**Dynamic Programming ****Approach: **The above recursive approach can be optimized by using Memoization.

Below is the implementation of the above approach:

  • Java

// C++ Program to implement

// the above approach

**import** java.io.*;

**import** java.util.*;

**class** GFG {

// Function to call dfs

// to calculate the number of ways

**static** **int** findWays(``**int**``[] nums, **int** S)

{

**int** sum = 0``;

// Iterate till the length of array

**for** (``**int** i = 0``; i < nums.length; i++)

sum += nums[i];

// Initialize the memorization table

**int**``[][] memo

= **new** **int**``[nums.length + 1``][``2 * sum + 1``];

**for** (``**int**``[] m : memo) {

Arrays.fill(m, Integer.MIN_VALUE);

}

**return** dfs(memo, nums, S, 0``, 0``, sum);

}

// Function to perform the DFS to calculate the

// number of ways

**static** **int** dfs(``**int**``[][] memo, **int**``[] nums, **int** S,

**int** curr_sum, **int** index, **int** sum)

{

// Base case: Reached the end of array

**if** (index == nums.length) {

// If current sum is obtained

**if** (S == curr_sum)

**return** 1``;

// Otherwise

**else**

**return** 0``;

}

// If previously calculated

// subproblem occurred

**if** (memo[index][curr_sum + sum]

!= Integer.MIN_VALUE) {

**return** memo[index][curr_sum + sum];

}

// Check if the required sum can

// be obtained by adding current

// element or by subtracting the

// current index element

**int** ans = dfs(memo, nums, index + 1``,

curr_sum + nums[index], S, sum)

+ dfs(memo, nums, index + 1``,

curr_sum - nums[index], S, sum);

// Store the count of ways

memo[index][curr_sum + sum] = ans;

**return** ans;

}

// Driver Code

**public** **static** **void** main(String[] args)

{

**int** S = 3``;

**int**``[] arr = **new** **int**``[] { 1``, 2``, 3``, 4``, 5 };

**int** answer = findWays(arr, S);

System.out.println(answer);

}

}

Output:

3

Time Complexity:_ O(N * S)_

Auxiliary Space:_ O(N * S)_

Knapsack Approach: The idea is to implement the 0/1 Knapsack problem. Follow the steps below:

#arrays #dynamic programming #mathematical #recursion #searching #knapsack #memoization #subset

Count of Ways to obtain given Sum from the given Array elements
4.40 GEEK