- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

# Program to find number of ways we can arrange symbols to get target in Python?

Suppose we have a list of non-negative numbers called nums and also have an integer target. We have to find the the number of ways to arrange + and - in nums such that the expression equals to target.

So, if the input is like nums = [2, 3, 3, 3, 2] target = 9, then the output will be 2, as we can have -2 + 3 + 3 + 3 + 2 and 2 + 3 + 3 + 3 – 2.

To solve this, we will follow these steps:

s := sum of all numbers in nums

if (s + target) mod 2 is not same as 0 or target > s, then

return 0

W := quotient of (s + target) / 2

dp1 := a list of size (W + 1) and fill with 0

dp1[0] := 1

dp2 := A list of size (W + 1) and fill with 0

for i in range 0 to size of nums, do

for j in range 0 to W + 1, do

if j >= nums[i], then

dp2[j] := dp2[j] + dp1[j - nums[i]]

for j in range 0 to W + 1, do

dp1[j] := dp1[j] + dp2[j]

dp2[j] := 0

return last element of dp1

Let us see the following implementation to get better understanding:

## Example

class Solution: def solve(self, nums, target): s = sum(nums) if (s + target) % 2 != 0 or target > s: return 0 W = (s + target) // 2 dp1 = [0] * (W + 1) dp1[0] = 1 dp2 = [0] * (W + 1) for i in range(len(nums)): for j in range(W + 1): if j >= nums[i]: dp2[j] += dp1[j - nums[i]] for j in range(W + 1): dp1[j] += dp2[j] dp2[j] = 0 return dp1[-1] ob = Solution() nums = [2, 3, 3, 3, 2] target = 9 print(ob.solve(nums, target))

## Input

[2, 3, 3, 3, 2], 9

## Output

2

- Related Articles
- Program to find number of ways we can get n R.s using Indian denominations in Python
- Program to find number of ways we can decode a message in Python
- Program to find number of ways we can split a palindrome in python
- Program to find number of ways we can concatenate words to make palindromes in Python
- Program to find number of ways we can arrange letters such that each prefix and suffix have more Bs than As in Python
- Program to find number of ways we can get a number which is sum of nth power of unique numbers in Python
- Program to find number of ways we can select sequence from Ajob Sequence in Python
- Program to find number of ways to form a target string given a dictionary in Python
- Program to find maximum number of coins we can get using Python
- Program to find number of ways we can reach to the next floor using stairs in Python
- Program to count number of ways we can distribute coins to workers in Python
- Program to count number of ways we can throw n dices in Python
- Program to find how many ways we can climb stairs in Python
- Program to find number of ways we can reach from top left point to bottom right point in Python
- Program to find number of ways to arrange n rooks so that they cannot attack each other in Python