Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • DSA
  • Algorithms
  • Analysis of Algorithms
  • Sorting
  • Searching
  • Greedy
  • Recursion
  • Backtracking
  • Dynamic Programming
  • Divide and Conquer
  • Geometric Algorithms
  • Mathematical Algorithms
  • Pattern Searching
  • Bitwise Algorithms
  • Branch & Bound
  • Randomized Algorithms
Open In App
Next Article:
How to define a Target Market for a Product?
Next article icon

Target Sum

Last Updated : 13 Nov, 2023
Comments
Improve
Suggest changes
Like Article
Like
Report
Try it on GfG Practice
redirect icon

Given an array arr[] of length N and an integer target. You 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. Return the number of different expressions that can be built, which evaluates to target.

Example: 

Input : N = 5, arr[] = {1, 1, 1, 1, 1}, target = 3
Output: 5
Explanation:
There are 5 ways to assign symbols to
make the sum of array be 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: N = 1, arr[] = {1}, target = 1
Output: 1

Target Sum using Recursion:

The problem can be solved by recursively exploring all possible ways to combine the integers in the array while adding or subtracting them to reach the target sum. At each step, there are two choices: either add the current element to the running total or subtract it.
Let’s findTotalWays(arr, i, s, target) tells us number of ways to reach target, if first i elements have been considered and current sum is s.

Recurrence Relation:

findTotalWays(arr, i, s, target) = findTotalWays(arr, i+1,s +arr[i], target) + findTotalWays(arr, i+1, s-arr[i], target)

For each element, there are 2 options.

  • Add the current element: If we add the current element to running total then current sum will become s+arr[i]. Call the findTotalWays() function recursively with the updated current sum and the remaining elements of array i.e., findTotalWays(arr, i+1,s +arr[i], target)
  • Subtract the current element: If we subtract the current element from running total then current sum will become s-arr[i]. Call the findTotalWays() function recursively with the updated current sum and the remaining elements of array i.e., findTotalWays(arr, i+1,s -arr[i], target)

Base case:

  • If the target sum is reached (i.e., s becomes equal to target sum) and all elements in the array have been processed, return 1. This represents a valid way to form the target sum.
  • If all elements in the array have been processed but the target sum has not been reached (i.e., s is not equal to target sum), return 0. This indicates expression does not evaluates to target.

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 <iostream>
#include <vector>
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 s, int target)
{
 
    // If target is reached, return 1
    if (s == target && i == arr.size())
        return 1;
 
    // If all elements are processed and
    // target is not reached, return 0
    if (i >= arr.size())
        return 0;
 
    // Return total count of two cases
    // 1. Consider current element and add it to current sum
    // target
    // 2. Consider current element and subtract it from
    // current sum.
    return findTotalWays(arr, i + 1, s + arr[i], target)
           + findTotalWays(arr, i + 1, s - arr[i], target);
}
 
// Driver Program
int main()
{
    vector<int> arr = { 1, 1, 1, 1, 1 };
 
    // target number
    int target = 3;
 
    cout << findTotalWays(arr, 0, 0, target) << endl;
 
    return 0;
}
 
 

Java




import java.util.ArrayList;
import java.util.List;
 
public class Main {
    // 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<Integer> arr, int i, int s, int target) {
        // If target is reached, return 1
        if (s == target && i == arr.size()) {
            return 1;
        }
 
        // If all elements are processed and
        // target is not reached, return 0
        if (i >= arr.size()) {
            return 0;
        }
 
        // Return total count of two cases:
        // 1. Consider the current element and add it to the current sum target
        // 2. Consider the current element and subtract it from the current sum.
        return findTotalWays(arr, i + 1, s + arr.get(i), target)
             + findTotalWays(arr, i + 1, s - arr.get(i), target);
    }
 
    // Driver Program
    public static void main(String[] args) {
        List<Integer> arr = new ArrayList<>();
        arr.add(1);
        arr.add(1);
        arr.add(1);
        arr.add(1);
        arr.add(1);
 
        // target number
        int target = 3;
 
        System.out.println(findTotalWays(arr, 0, 0, target));
    }
}
 
 

Python3




# 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, s, target):
    # If target is reached, return 1
    if s == target and i == len(arr):
        return 1
 
    # If all elements are processed and
    # target is not reached, return 0
    if i >= len(arr):
        return 0
 
    # Return total count of two cases:
    # 1. Consider the current element and add it to the current sum target
    # 2. Consider the current element and subtract it from the current sum.
    return findTotalWays(arr, i + 1, s + arr[i], target) + findTotalWays(arr, i + 1, s - arr[i], target)
 
# Driver Program
if __name__ == "__main__":
    arr = [1, 1, 1, 1, 1]
 
    # target number
    target = 3
 
    print(findTotalWays(arr, 0, 0, target))
 
 

C#




using System;
using System.Collections.Generic;
 
class MainClass
{
    // 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 s, int target)
    {
        // If target is reached, return 1
        if (s == target && i == arr.Count)
            return 1;
 
        // If all elements are processed and
        // target is not reached, return 0
        if (i >= arr.Count)
            return 0;
 
        // Return the total count of two cases:
        // 1. Consider the current element and add it to the current sum target
        // 2. Consider the current element and subtract it from the current sum.
        return FindTotalWays(arr, i + 1, s + arr[i], target)
             + FindTotalWays(arr, i + 1, s - arr[i], target);
    }
 
    public static void Main(string[] args)
    {
        List<int> arr = new List<int> { 1, 1, 1, 1, 1 };
 
        // target number
        int target = 3;
 
        Console.WriteLine(FindTotalWays(arr, 0, 0, target));
    }
}
 
 

Javascript




// Function to find the number of ways to calculate
// a target number using only array elements and
// addition or subtraction operator.
function findTotalWays(arr, i, s, target) {
    // If target is reached, return 1
    if (s === target && i === arr.length) {
        return 1;
    }
 
    // If all elements are processed and
    // target is not reached, return 0
    if (i >= arr.length) {
        return 0;
    }
 
    // Return total count of two cases:
    // 1. Consider the current element and add it to the current sum target
    // 2. Consider the current element and subtract it from the current sum.
    return findTotalWays(arr, i + 1, s + arr[i], target)
         + findTotalWays(arr, i + 1, s - arr[i], target);
}
 
// Driver Program
const arr = [1, 1, 1, 1, 1];
 
// target number
const target = 3;
 
console.log(findTotalWays(arr, 0, 0, target));
 
 
Output
5        

Time Complexity: O(2n), where n is size of array.
Auxiliary Space: O(n) 

Target Sum using Dynamic Programming (Memoization):

The above recursive solution has Optimal Substructure and Overlapping Subproblems so Dynamic programming (Memoization) can be used to solve the problem. So, 2D array can be used to store results of previously calculated subproblems.

Follow the below steps to Implement the idea:

  • Create a 2D dp array to store the results of previously solved subproblems.
  • dp[i][j] will represent the number of distinct expressions to make the target sum if first i elements have even considered and the current sum is j.
  • During the recursion call, if the same state is called more than once, then we can directly return the answer stored in dp for that state instead of calculating again.
  • Notice that total_sum is added to current sum in recursive function to ensure that the memoization table can handle both positive and negative sums.

Below is the implementation of the above approach:

C++




// C++ program to find the number of ways to calculate
// a target number using only array elements and
// addition or subtraction operator.
#include <iostream>
#include <vector>
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,
                  vector<vector<int> >& dp, int i, int s,
                  int target, int total_sum)
{
 
    // If target is reached, return 1
    if (s == target && i == arr.size())
        return 1;
 
    // If all elements are processed and
    // target is not reached, return 0
    if (i >= arr.size())
        return 0;
 
    // If the result for the current state (i, s +
    // total_sum) has already been computed,
    // return it from the DP table to avoid redundant
    // calculations.
    if (dp[i][s + total_sum] != -1)
        return dp[i][s + total_sum];
 
    // Return total count of two cases
    // 1. Consider current element and add it to current sum
    // target
    // 2. Consider current element and subtract it from
    // current sum.
    return dp[i][s + total_sum]
           = findTotalWays(arr, dp, i + 1, s + arr[i],
                           target, total_sum)
             + findTotalWays(arr, dp, i + 1, s - arr[i],
                             target, total_sum);
}
 
// Driver Program
int main()
{
    vector<int> arr = { 1, 1, 1, 1, 1 };
    int total_sum = 0;
    for (int i = 0; i < arr.size(); i++)
        total_sum += arr[i];
    // target number
    int target = 3;
    vector<vector<int> > dp(
        arr.size(), vector<int>(2 * total_sum + 1, -1));
    cout << findTotalWays(arr, dp, 0, 0, target, total_sum)
         << endl;
 
    return 0;
}
 
 

Java




import java.util.ArrayList;
 
public class Main {
    // Function to find the number of ways to calculate
    // a target number using only array elements and
    // addition or subtraction operator.
    static int findTotalWays(ArrayList<Integer> arr,
                             int[][] dp, int i, int s,
                             int target, int totalSum)
    {
 
        // If target is reached, return 1
        if (s == target && i == arr.size())
            return 1;
 
        // If all elements are processed and
        // target is not reached, return 0
        if (i >= arr.size())
            return 0;
 
        // If the result for the current state (i, s +
        // totalSum) has already been computed,
        // return it from the DP table to avoid redundant
        // calculations.
        if (dp[i][s + totalSum] != -1)
            return dp[i][s + totalSum];
 
        // Return total count of two cases
        // 1. Consider the current element and add it to the
        // current sum
        //    to reach the target.
        // 2. Consider the current element and subtract it
        // from
        //    the current sum.
        return dp[i][s + totalSum]
            = findTotalWays(arr, dp, i + 1, s + arr.get(i),
                            target, totalSum)
              + findTotalWays(arr, dp, i + 1,
                              s - arr.get(i), target,
                              totalSum);
    }
 
    // Driver Program
    public static void main(String[] args)
    {
        ArrayList<Integer> arr = new ArrayList<>();
        arr.add(1);
        arr.add(1);
        arr.add(1);
        arr.add(1);
        arr.add(1);
 
        int totalSum = 0;
        for (int i = 0; i < arr.size(); i++)
            totalSum += arr.get(i);
 
        // Target number
        int target = 3;
 
        int[][] dp = new int[arr.size()][2 * totalSum + 1];
        for (int i = 0; i < arr.size(); i++) {
            for (int j = 0; j < 2 * totalSum + 1; j++) {
                dp[i][j] = -1;
            }
        }
 
        System.out.println(
            findTotalWays(arr, dp, 0, 0, target, totalSum));
    }
}
 
 

Python




# Function to find the number of ways to calculate
# a target number using only array elements and
# addition or subtraction operator.
def find_total_ways(arr, dp, i, s, target, total_sum):
    # If target is reached, return 1
    if s == target and i == len(arr):
        return 1
 
    # If all elements are processed and
    # target is not reached, return 0
    if i >= len(arr):
        return 0
 
    # If the result for the current state (i, s +
    # total_sum) has already been computed,
    # return it from the DP table to avoid redundant
    # calculations.
    if dp[i][s + total_sum] != -1:
        return dp[i][s + total_sum]
 
    # Return the total count of two cases:
    # 1. Consider the current element and add it to the current sum target.
    # 2. Consider the current element and subtract it from the current sum.
    dp[i][s + total_sum] = find_total_ways(arr, dp, i + 1, s + arr[i], target, total_sum) + \
                          find_total_ways(arr, dp, i + 1, s - arr[i], target, total_sum)
     
    return dp[i][s + total_sum]
 
# Driver Program
if __name__ == "__main__":
    arr = [1, 1, 1, 1, 1]
    total_sum = sum(arr)
     
    # Target number
    target = 3
     
    # Create a DP table
    dp = [[-1] * (2 * total_sum + 1) for _ in range(len(arr))]
     
    # Calculate and print the result
    print(find_total_ways(arr, dp, 0, 0, target, total_sum))
 
 

C#




using System;
using System.Collections.Generic;
 
class Program {
    // 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[, ] dp,
                             int i, int s, int target,
                             int totalSum)
    {
        // If target is reached, return 1
        if (s == target && i == arr.Count)
            return 1;
 
        // If all elements are processed and target is not
        // reached, return 0
        if (i >= arr.Count)
            return 0;
 
        // If the result for the current state (i, s +
        // totalSum) has already been computed, return it
        // from the DP table to avoid redundant
        // calculations.
        if (dp[i, s + totalSum] != -1)
            return dp[i, s + totalSum];
 
        // Return the total count of two cases:
        // 1. Consider the current element and add it to the
        // current sum to reach the target.
        // 2. Consider the current element and subtract it
        // from the current sum.
        return dp[i, s + totalSum]
            = FindTotalWays(arr, dp, i + 1, s + arr[i],
                            target, totalSum)
              + FindTotalWays(arr, dp, i + 1, s - arr[i],
                              target, totalSum);
    }
 
    // Driver Program
    static void Main()
    {
        List<int> arr = new List<int>{ 1, 1, 1, 1, 1 };
        int totalSum = 0;
        for (int i = 0; i < arr.Count; i++)
            totalSum += arr[i];
 
        // Target number
        int target = 3;
        int[, ] dp = new int[arr.Count, 2 * totalSum + 1];
        for (int i = 0; i < arr.Count; i++) {
            for (int j = 0; j < 2 * totalSum + 1; j++) {
                dp[i, j] = -1;
            }
        }
 
        Console.WriteLine(
            FindTotalWays(arr, dp, 0, 0, target, totalSum));
    }
}
 
 

Javascript




// Function to find the number of ways to calculate
// a target number using only array elements and
// addition or subtraction operator.
function findTotalWays(arr, dp, i, s, target, totalSum) {
    // If the target is reached, return 1
    if (s === target && i === arr.length)
        return 1;
 
    // If all elements are processed and the target is not reached, return 0
    if (i >= arr.length)
        return 0;
 
    // If the result for the current state (i, s + totalSum) has already been computed,
    // return it from the DP table to avoid redundant calculations.
    if (dp[i][s + totalSum] !== -1)
        return dp[i][s + totalSum];
 
    // Return the total count of two cases:
    // 1. Consider the current element and add it to the current sum to reach the target.
    // 2. Consider the current element and subtract it from the current sum to reach the target.
    dp[i][s + totalSum] = findTotalWays(arr, dp, i + 1, s + arr[i], target, totalSum)
                      + findTotalWays(arr, dp, i + 1, s - arr[i], target, totalSum);
 
    return dp[i][s + totalSum];
}
 
// Driver Program
function main() {
    const arr = [1, 1, 1, 1, 1];
    let totalSum = 0;
    for (let i = 0; i < arr.length; i++)
        totalSum += arr[i];
 
    // Target number
    const target = 3;
 
    // Create a 2D DP table
    const dp = new Array(arr.length);
    for (let i = 0; i < arr.length; i++) {
        dp[i] = new Array(2 * totalSum + 1).fill(-1);
    }
 
    console.log(findTotalWays(arr, dp, 0, 0, target, totalSum));
}
 
main();
 
 
Output
5        

Time Complexity: O(N*S), where N is size of array and S is total space.
Auxiliary Space: O(N*S) 



Next Article
How to define a Target Market for a Product?

A

Aditya Goel
Improve
Article Tags :
  • Algorithms
  • DSA
  • knapsack
Practice Tags :
  • Algorithms

Similar Reads

  • Emerging Attack Vectors in Cyber Security
    In Cyber Security, knowing about attack vectors is key to keeping information safe and systems secure. An attack vector is a way that cybercriminals use to break into a network, system, or application by taking advantage of weaknesses. Attack vectors refer to the various paths or methods that attack
    7 min read
  • How to define a Target Market for a Product?
    A Target market is a group of people that have been identified as the most likely potential customers for a product because of their shared characteristics, such as age, income, and lifestyle. Identifying the target market is a key part of the decision-making process when a company designs, packages
    11 min read
  • HTML target Attribute
    The HTML target Attribute is used to specify where to open the linked document. It can be used on various elements such as: HTML <a> target AttributeHTML <area> target AttributeHTML <base> target AttributeHTML <form> target AttributeSyntax: <element target="_blank|_self|_p
    2 min read
  • CSS :target Selector
    The target selector is used to represent a unique element (the target element) with an id matching the URL's fragment. It can be used to style the current active target element. URLs with a # followed by an anchor name link to a certain element within a document. The element being linked to is the t
    2 min read
  • How to Change the Target of a Link in HTML ?
    In HTML, for changing the target of a link we can use the target attribute in the <a> anchor tag. This attribute mainly specifies where the linked content should be displayed. Below are the approaches that can be used to change the target link in HTML: Table of Content Using target Attribute O
    3 min read
  • HTML | DOM target Event Property
    The target event property in HTML DOM is used to return the element that triggered the event. Syntax: event.target Return Value: This property returns the reference of the object on which the event originally occurred. Below example illustrates the target Event Property in HTML DOM: Example: C/C++ C
    1 min read
  • HTML | <base> target Attribute
    The <base> target Attribute in HTML is used to specify the default target for all hyperlinks and forms in the webpage. This attribute could also be overridden by the use of the target attribute for each hyperlink and the Form. Syntax: <base target="_blank|_self|_parent|_top|framename" >
    1 min read
  • HTML | DOM Base target Property
    The Base target Property in HTML DOM is used to set or return the value of the target attribute of a <base> element. The target attribute is used to specify the default target for all hyperlinks and forms in the page. Syntax: It returns the Base target property.baseObject.targetIt is used to s
    2 min read
  • HTML <a> target Attribute
    The HTML <a> target Attribute is used to specify where to open the linked document. This attribute enhances the functionality and user experience by allowing developers to control the behavior of links. Syntax<a target="_blank | _self | _parent | _top | framename"\> Attribute Values_blan
    1 min read
  • Market Targeting Strategies
    What are Marketing Targeting Strategies?Marketing Targeting Strategies play a crucial role in helping companies effectively reach and engage their desired customer segments. These strategies involve the careful selection of target markets based on various factors such as resources, product character
    12 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences