Search code examples
algorithmnp

Polynomial Time algorithm to equate combination of right hand sum to left hand sum?


I have to write polynomial time algorithm for the problem which should say accept or reject if the number(s) to the left don't match to the number on right. You can group the numbers also, examples are below

X & Y .......... & N = SUM where X, Y, and N can be any integer

Case 1: 4 & 6 & 10 = 14 , accepts 
So case 1 accepts because first number and the third number together sum up to 14.  
Case 2: 4 & 6 & 10 = 8 rejects
Case 3: 4 & 6 & 10 = 6 , accepts 
Case 4: 4 & 6 & 10 = 11 rejects

Some more test cases:
Case 1: 4 & 6 & 10 = 4 , accepts 
Case 2: 4 & 6 & 10 = 21 rejects
Case 3: 4 & 6 & 10 = 20 , accepts 
Case 4: 4 & 6 & 10 = 17 rejects
Case 5: 4 & 6 & 10 = 16 , accepts 
Case 6: 4 & 6 & 10 = 200 rejects

Case 7: 4 & 6 & 10 = 111 , rejects 
Case 8: 4 & 6 & 10 = 7 rejects

some more test cases 

Case 1 : 1 & 1 & 1 = 3 accepts
Case 2 : 1 & 1 & 1 & 2 & 2 & 22 = 29 accepts

For this how can I write polynomial time algorithm? Is it NP Problem?


Solution

  • It seems to me too that this is the subset sum problem, which is NP-complete (meaning that a polynomial-time solution doesn't exist, unless P=NP) but can be solved in pseudo-polynomial time (i.e. time polynomial both in the number of numbers and the value of the sum) using dynamic programming.