Programming Puzzles & Code Golf Stack Exchange is a question and answer site for programming puzzle enthusiasts and code golfers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

This question already has an answer here:

Given an integer array. You have to check whether it is possible to divide the array in two parts such that the sum of elements of one part is equal to the sum of the element of the other part.If it is possible print "YES" otherwise "NO".

Sample Input

4 3 5 5 3

Sample Output



Array can be divided in two parts {4,3,3} and {5,5}

Sample Input

1 3

Sample Output


Sample Input

5 5

Sample Output

share|improve this question

marked as duplicate by Peter Taylor, Justin, Hosch250, Cruncher, Paul R Feb 7 '14 at 7:43

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

This looks like it simplifies to the subset sum problem. – Kendall Frey Feb 6 '14 at 13:51
Do you accept True/False as output? – DavidC Feb 6 '14 at 15:11
@PeterTaylor That asked people to find if the sum of a group in the array equals 0. This asks people if the sum of one group is the same as the sum of another group within the same array. The techniques used are very similar, but the outcome is completely different. – Hosch250 Feb 6 '14 at 19:03
@user2509848, applying the standard rule of thumb for duplicates (how much effort is required to port a solution from the older question?), I think the answer here is "not enough". All that is required is a trivial change to the input (add -sum(input)/2 to the input array) and a trivial change to the output. – Peter Taylor Feb 6 '14 at 19:07
OK, I guess it is a duplicate. I was thinking of a completely different approach. – Hosch250 Feb 6 '14 at 19:18

11 Answers 11

up vote 4 down vote accepted

Ruby 98

f=->a{s=a.inject :+;(1..a.count).any?{|i|a.combination(i).any?{|r|r.inject(:+)*2==s}}?'Yes':'No'}
share|improve this answer
Why not (1..a.size) ? – Cyoce Apr 4 at 18:42


Without proper output (92 chars)

s([H|T],B,C):-D is B+H,s(T,D,C).
s([H|T],B,C):-D is C+H,s(T,B,D).

With proper output (121 chars)

s([H|T],B,C):-D is B+H,s(T,D,C).
s([H|T],B,C):-D is C+H,s(T,B,D).

To query the rules:

share|improve this answer
+1 for Prolog. Thanks for memories. – VisioN Feb 6 '14 at 20:00

Ruby with ActiveSupport 74

p (1..a.size).any?{|b|a.combination(b).any?{|c|c.sum*2==a.sum}}?'YES':'NO'

Ruby 88

p (1..a.size).any?{|b|a.combination(b).any?{|c|c.reduce(:+)*2==a.reduce(:+)}}?'YES':'NO'
share|improve this answer

Mathematica 59

With spaces for readability,

f@a_ := If[Select[Subsets@a, Tr@# == Tr@a/2 &] == {}, "NO\n", "YES\n"]

The Logic

If there exists no subset of the input integer array, a, having, as its total, one-half of the total of a, then NO. Else, YES.


f[{4, 3, 5, 5, 3}]


f[{4, 3, 5}]


share|improve this answer
It prints YES and NO for the same example? – Hosch250 Feb 6 '14 at 16:20
No. I mis-entered the second input! – DavidC Feb 6 '14 at 16:56

Haskell - 113 (30 chars for imports)

import Control.Monad(filterM)
f x|t="YES"
 |1>0="NO"where t=or$map(==(sum x)/2)$map(sum)$filterM(\_->[0>1,1>0])x

Query by calling f on a list:

*Main> f [4,3,5,5,3]
*Main> f [1,3]
*Main> f [5,5]

This is my first attempt at golfing in Haskell, if anyone has any tips for shortening my code they are welcomed.

share|improve this answer

MATLAB 75 64

Quite happy with this. Too bad the function names are so much characters.

@(x)['NO '+[11,-10,51]*ismember(sum(x)/2,cumsum(perms(x),2)) '']
share|improve this answer


Only works in browsers with ECMAScript 6 support (Firefox works). Defines a function that can be called with a list of integers, the target sum value, and the length of the list.

53 characters (sans formatting and specific criteria)


This will work with both positive and negative floating point numbers as well, not just integers. It can also find whether the list at any point can sum to any arbitrary sum, which is much more general than the problem requires.

106 Characters (with formatting and starting criteria)


Fully half of the code here is just boilerplate required to limit the solution to this specific case, and format the output according to the criteria.

A non recursive solution that builds the table from the bottom is more efficient but also takes more characters.

share|improve this answer

GolfScript 64 47


Explanation: Find a subset (using this subset method) whose sum is exactly half of the total sum of the input.

Previous solution:


Explanation: This uses base to count and find every possible array of length n comprised of only 1s and 0s. Then it uses this array to negate or do nothing to elements of the initial array. For example, for input [1 2 3], we would have [1 2 3][1 2 -3][1 -2 3][1 -2 -3][-1 2 3][-1 2 -3][-1 -2 3][-1 -2 -3]. The program then returns 'YES' if and only if the sum of the elements of any of these arrays is 0. Negating some subset of the array, then adding all the elements and checking if it equals 0 is clearly equivalent to checking for a 2-part partition.

I'm sure this implementation can be improved upon, but I think the idea is there.

share|improve this answer

GolfScript, 34 characters


The code basically calculates the totals of all possible subsets and then searches for (half of) the overall total in this list. (Actualy the code calculates doubles all the subset sums and searches for the list's total in order to not conflict with integer division.) You can see the example running online.

> 4 3 5 5 3

> 4 5 5 5 3
share|improve this answer

C#, 169 bytes

It enumerates through a series of arrays of the same length as the input. Each array contains a list of +1 and -1 values which are multiplied against each corresponding input value. If the sum of the multiplied arrays is zero, then some numbers sum to the same as the rest of the numbers.

For the sake of golfing the +1/-1 array is actually +2/0 and 1 subtracted from it when used.


input    = n = {  4,  3,  5,  5,  3 }
polarity = p = { +1, +1, -1, -1, +1 }
n*p      =     { +4, +3, -5, -5, +3 }
result   = x = Sum(n*p) == 0


bool S(int[]n){
    int i=0,l=n.Length,s;
    var x=false;
    var p=new int[l+1];
    return x;





// Presuming Sum{0} == Sum{}
share|improve this answer

C++ — 294 275

Not very compact, but you don't often see C++ in code challenges!


  • can get NO result in O(1) time for many inputs
  • uses no external algorithms for generating subsets, all in code
  • C++ was used for easy vector creation


#include <iostream>
#include <vector>
using namespace std;int main(){vector<int>v;int s,e,w,g,t;for(s=0;cin>>e;s+=e)v.push_back(e);if(!(s&1)){s/=2;w=v.size();for(e=0;e<1<<w;e++){for(g=0,t=0;g<w;g++)if(e&1<<g)t+=v[g];if(t==s){cout<<"YES\n";return 0;}}}cout<<"NO\n";return 1;}

To compile

g++ -o partition partition.cpp


$ echo 4 3 5 5 3 | ./partition

$ echo {1..28} | ./partition
YES # after a slight delay

$ echo 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 29 | ./partition
NO # after a long delay

$ echo {1..29} | ./partition
NO # immediately


  • upper limit is 29 elements, could use unsigned long long int to extend to 61 or 62 elements, but the basic idea is the same
share|improve this answer

Not the answer you're looking for? Browse other questions tagged or ask your own question.