Untitled Document
Untitled Document
Question:
Find ways to calculate a target from elements of the specified array. Given an
integer array, return the total number of ways to calculate the specified target from array
elements using only the addition and subtraction operator. The use of any other
operator is
forbidden.
Consider the array { 5, 3, -6, 2 }.
The total number of ways to reach a target of 6 using only + and – operators is 4 as:
(-)-6 = 6
(+) 5 (+) 3 (-) 2 = 6
(+) 5 (-) 3 (-) -6 (-) 2 = 6
(-) 5 (+) 3 (-) -6 (+) 2 = 6
Similarly, there are 4 ways to calculate the target of 4:
(-)-6 (-) 2 = 4
(-) 5 (+) 3 (-)-6 = 4
(+) 5 (-) 3 (+) 2 = 4
(+) 5 (+) 3 (+)-6 (+) 2 =4
Answer:
Problem Statement:
Given an integer array, return the total number of ways to calculate the specified target
from array elements using only the addition and subtraction operator.
Example:
Task:
Write a Python function find_target_ways that takes an integer array arr and a target
integer target as input and returns the total number of ways to calculate the target from
array elements using only the addition and subtraction operator.
Solution:
Test Case:
Output: