Skip to main content

Target Sum

Learn Target Sum (Dynamic Programming, Advanced DSA) with worked examples, diagrams and interview questions. Free on JavaSchoolLab.

Assigning + or - to Each Number to Hit a Target Given numbers and a target, assign a + or - sign to EACH number so the total equals target; count how many ways this is possible. The algebraic trick: split numbers into a POSITIVE group P and NEGATIVE group N. Since sum(P) - sum(N) = target and sum(P) + sum(N) = total , solving gives sum(P) = (total + target) / 2 - turning this into counting Subset Sum ways for that specific value.

More lessons in Dynamic Programming