# Number of ways to calculate a target number using only array elements

Given an integer array, find number of ways to calculate a target number using only array elements and addition or subtraction operator.

Example:

```Input: arr[] = {-3, 1, 3, 5}, k = 6
Output: 4

Explanation -
- (-3) + (3)
+ ( 1) + (5)
+ (-3) + (1) + (3) + (5)
- (-3) + (1) - (3) + (5)
```

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

The problem is similar to 0-1 Knapsack Problem where for every item, we either pick the complete item, or don’t pick it at all (0-1 property). The idea remains the same here i.e. we either include the current digit or ignore it. If we include the current digit, we subtract or add it from remaining target and recurse for remaining digits with new target. If target reaches 0, we increment the count. If we have processed all elements of the array and target is not reached, count remains unchanged.

Below is recursive implementation of above idea.

## C++

 `// C++ program to find the number of ways to calculate ` `// a target number using only array elements and ` `// addition or subtraction operator. ` `#include ` `#include ` `using` `namespace` `std; ` ` `  `// Function to find the number of ways to calculate ` `// a target number using only array elements and ` `// addition or subtraction operator. ` `int` `findTotalWays(vector<``int``> arr, ``int` `i, ``int` `k) ` `{ ` `    ``// If all elements are processed and ` `    ``// target is not reached, return 0 ` `    ``if` `(i >= arr.size() && k != 0) ` `        ``return` `0; ` ` `  `    ``// If target is reached, return 1 ` `    ``if` `(k == 0) ` `        ``return` `1; ` ` `  `    ``// Return total count of three cases ` `    ``// 1. Don't consider current element ` `    ``// 2. Consider current element and subtract it from target ` `    ``// 3. Consider current element and add it to target ` `    ``return` `findTotalWays(arr, i + 1, k) ` `           ``+ findTotalWays(arr, i + 1, k - arr[i]) ` `           ``+ findTotalWays(arr, i + 1, k + arr[i]); ` `} ` ` `  `// Driver Program ` `int` `main() ` `{ ` `    ``vector<``int``> arr = {-3, 1, 3, 5, 7}; ` ` `  `    ``// target number ` `    ``int` `k = 6; ` ` `  `    ``cout << findTotalWays(arr, 0, k) << endl; ` ` `  `    ``return` `0; ` `} `

## Java

 `// Java program to find the number  ` `// of ways to calculate a target ` `// number using only array elements and  ` `// addition or subtraction operator.  ` `import` `java.util.*; ` ` `  `class` `GFG  ` `{ ` ` `  `    ``// Function to find the number of ways to calculate  ` `    ``// a target number using only array elements and  ` `    ``// addition or subtraction operator.  ` `    ``static` `int` `findTotalWays(Vector arr, ``int` `i, ``int` `k)  ` `    ``{ ` `        ``// If all elements are processed and  ` `        ``// target is not reached, return 0  ` `        ``if` `(i >= arr.size() && k != ``0``)  ` `        ``{ ` `            ``return` `0``; ` `        ``} ` ` `  `        ``// If target is reached, return 1  ` `        ``if` `(k == ``0``)  ` `        ``{ ` `            ``return` `1``; ` `        ``} ` ` `  `        ``// Return total count of three cases  ` `        ``// 1. Don't consider current element  ` `        ``// 2. Consider current element and subtract it from target  ` `        ``// 3. Consider current element and add it to target  ` `        ``return` `findTotalWays(arr, i + ``1``, k) ` `                ``+ findTotalWays(arr, i + ``1``, k - arr.get(i)) ` `                ``+ findTotalWays(arr, i + ``1``, k + arr.get(i)); ` `    ``} ` ` `  `    ``// Driver code  ` `    ``public` `static` `void` `main(String[] args)  ` `    ``{ ` `        ``int``[] arr = {-``3``, ``1``, ``3``, ``5``, ``7``}; ` `        ``Vector v = ``new` `Vector(); ` `        ``for` `(``int` `a : arr) ` `        ``{ ` `            ``v.add(a); ` `        ``} ` `         `  `        ``// target number  ` `        ``int` `k = ``6``; ` ` `  `        ``System.out.println(findTotalWays(v, ``0``, k)); ` `    ``} ` `} ` ` `  `// This code contributed by Rajput-Ji `

## Python3

 `# Python 3 program to find the number of  ` `# ways to calculate a target number using  ` `# only array elements and addition or  ` `# subtraction operator. ` ` `  `# Function to find the number of ways to  ` `# calculate a target number using only  ` `# array elements and addition or  ` `# subtraction operator. ` `def` `findTotalWays(arr, i, k): ` `     `  `    ``# If all elements are processed and ` `    ``# target is not reached, return 0 ` `    ``if` `(i >``=` `len``(arr) ``and` `k !``=` `0``): ` `        ``return` `0` ` `  `    ``# If target is reached, return 1 ` `    ``if` `(k ``=``=` `0``): ` `        ``return` `1` ` `  `    ``# Return total count of three cases ` `    ``# 1. Don't consider current element ` `    ``# 2. Consider current element and  ` `    ``#    subtract it from target ` `    ``# 3. Consider current element and ` `    ``#    add it to target ` `    ``return` `(findTotalWays(arr, i ``+` `1``, k) ``+`  `            ``findTotalWays(arr, i ``+` `1``, k ``-` `arr[i]) ``+`  `            ``findTotalWays(arr, i ``+` `1``, k ``+` `arr[i])) ` ` `  `# Driver Code ` `if` `__name__ ``=``=` `'__main__'``: ` `    ``arr ``=` `[``-``3``, ``1``, ``3``, ``5``, ``7``] ` ` `  `    ``# target number ` `    ``k ``=` `6` ` `  `    ``print``(findTotalWays(arr, ``0``, k)) ` `     `  `# This code is contributed by ` `# Surendra_Gangwar `

## C#

 `// C# program to find the number  ` `// of ways to calculate a target ` `// number using only array elements and  ` `// addition or subtraction operator.  ` `using` `System; ` `using` `System.Collections.Generic; ` `     `  `class` `GFG  ` `{ ` ` `  `    ``// Function to find the number of ways to calculate  ` `    ``// a target number using only array elements and  ` `    ``// addition or subtraction operator.  ` `    ``static` `int` `findTotalWays(List<``int``> arr, ``int` `i, ``int` `k)  ` `    ``{ ` `        ``// If all elements are processed and  ` `        ``// target is not reached, return 0  ` `        ``if` `(i >= arr.Count && k != 0)  ` `        ``{ ` `            ``return` `0; ` `        ``} ` ` `  `        ``// If target is reached, return 1  ` `        ``if` `(k == 0)  ` `        ``{ ` `            ``return` `1; ` `        ``} ` ` `  `        ``// Return total count of three cases  ` `        ``// 1. Don't consider current element  ` `        ``// 2. Consider current element and subtract it from target  ` `        ``// 3. Consider current element and add it to target  ` `        ``return` `findTotalWays(arr, i + 1, k) ` `                ``+ findTotalWays(arr, i + 1, k - arr[i]) ` `                ``+ findTotalWays(arr, i + 1, k + arr[i]); ` `    ``} ` ` `  `    ``// Driver code  ` `    ``public` `static` `void` `Main(String[] args)  ` `    ``{ ` `        ``int``[] arr = {-3, 1, 3, 5, 7}; ` `        ``List<``int``> v = ``new` `List<``int``>(); ` `        ``foreach` `(``int` `a ``in` `arr) ` `        ``{ ` `            ``v.Add(a); ` `        ``} ` `         `  `        ``// target number  ` `        ``int` `k = 6; ` ` `  `        ``Console.WriteLine(findTotalWays(v, 0, k)); ` `    ``} ` `} ` ` `  `// This code has been contributed by 29AjayKumar `

Output :

`10`

Time Complexity of above solution is O(3^n) i.e. exponential.

GeeksforGeeks has prepared a complete interview preparation course with premium videos, theory, practice problems, TA support and many more features. Please refer Placement 100 for details

My Personal Notes arrow_drop_up

Article Tags :
Practice Tags :

1

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.