From b6fbbaaa60ed3bbb03d094b4c0177bf95888b097 Mon Sep 17 00:00:00 2001 From: namanhere23 Date: Tue, 28 Oct 2025 23:50:21 +0530 Subject: [PATCH] Added Equal Sum Partition through python --- .../Equal_Sum_Partition.py | 22 +++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 Python/dynamic_programming/Equal_Sum_Partition.py diff --git a/Python/dynamic_programming/Equal_Sum_Partition.py b/Python/dynamic_programming/Equal_Sum_Partition.py new file mode 100644 index 00000000..08cbf845 --- /dev/null +++ b/Python/dynamic_programming/Equal_Sum_Partition.py @@ -0,0 +1,22 @@ +def can_partition(nums): + total_sum = sum(nums) + + if total_sum % 2 != 0: + return False + + target = total_sum // 2 + n = len(nums) + + possible_sums = {0} + + for num in nums: + new_sums = set() + for s in possible_sums: + new_sum = s + num + if new_sum == target: + return True + if new_sum < target: + new_sums.add(new_sum) + possible_sums |= new_sums + + return target in possible_sums