VOOZH about

URL: https://www.geeksforgeeks.org/dsa/number-of-ways-to-calculate-a-target-number-using-only-array-elements/

⇱ Target Sum - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Target Sum

Last Updated : 4 Apr, 2026

Given an array arr[] and an integer target. We want to build an expression out of arr[] by adding one of the symbols '+' and '-' before each integer in arr[] and then concatenate all the integers. Determine the number of different expressions that can be built, which evaluates to target.

Example:

Input : arr[] = [1, 1, 1, 1, 1], target = 3
Output: 5
Explanation: There are 5 ways for target 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
+1 + 1 + 1 + 1 - 1 = 3

Input: arr[] = [1], target = 1
Output: 1

Input : arr[] = [1, 2, 2, 1], target = 2
Output: 2
Explanation: There are 2 ways for 2
1 - 2 + 2 + 1
1 + 2 - 2 + 1


[Naive Approach] Using Recursion - O(2^n) Time and O(n) Auxiliary Space

The idea is to explore all possible expressions by assigning either a '+' or '-' sign to each element of the array.

  • For every element, there are two choices: add it to the current sum or subtract it from the current sum.
  • At each step, the current index and the cumulative sum together define the state of the recursion.
  • When all elements have been processed, the current sum is compared with the target. If they are equal, one valid expression is found; otherwise, it is discarded.

Output
5

[Better Approach-1] Using Top Down DP (Memoization) - O(n*Sum) and O(n*Sum) Space

This approach optimizes the recursive solution by storing results of overlapping subproblems. Here, dp[i][s] stores the number of ways to achieve a cumulative sum s using elements from index i onward. Since the cumulative sum can become negative, an offset equal to the total sum of all elements is used to map sums to valid indices.


Output
5

[Better Approach-2] Using Bottom Up DP (Tabulation) - O(n*Sum) Time and O(n*Sum) Space

In this approach, a DP table is built iteratively. Here, dp[i][j] represents the number of ways to form a sum j using the first i elements of the array. The table is initialized to represent zero elements forming a sum of zero. For each element, the table is updated by propagating counts for both adding and subtracting the current value. Each state depends on results from the previous row, ensuring that all valid expressions are counted efficiently.


Output
5

[Expected Approach] Using Space Optimization - O(n*Sum) Time and O(Sum) Space

This approach reduces space usage by observing that each DP row depends only on the previous row. Instead of maintaining a full 2D table, two 1D arrays are used to represent the previous and current states.
Here, each index in the array stores the number of ways to achieve the corresponding sum after processing a certain number of elements.

After computing one row, the current array replaces the previous one. This optimization preserves correctness while significantly reducing memory usage.


Output
5
Comment
Article Tags:
Article Tags: