Given a vector x, return the indices to elements that will sum to exactly half of the sum of all elements.

Example:

Input x = [1 2 3 4 5 6 7] Output xi = [1 6 7]

because

sum(x) = 28 sum(x([1 6 7])) = 14

The answer is not necessarily unique and the order is unimportant. We will just test to make sure that `sum(x)/2` is `sum(x(xi))`

Show
11 older comments

AMITAVA BISWAS
on 11 Jun 2012

Could you please post the link to ASEE Challenge June 10-13? Thanks!

Ned Gulley
on 14 Jun 2012

Amitava, do you mean here? The link is http://www.mathworks.com/matlabcentral/cody/?sort=created+desc&term=group%3A%22ASEE+Challenge%22

Vieniava
on 6 Dec 2012

it seems that tests no3 and no4 are incorrectly defined. Ned, check this out please.

Ned Gulley
on 7 Dec 2012

@Vieniava, can you be more specific? The tests are the same in all cases. I did verify that Solution 173962 does indeed get an incorrect answer to tests 3 and 4.

Ted
on 16 Mar 2013

This was fun

Jean-Marie Sainthillier
on 17 Mar 2013

A lot a very beautiful solutions.

J.R.! Menzinger
on 18 Nov 2013

What was changed on 5th. November?? :-/

Ned Gulley
on 19 Nov 2013

I added a new test. I periodically strengthen test suites to defeat look-up table strategies.

Aryaa Ravieshancar
on 2 Feb 2014

I solved this with matlab inbuilt function 'combnk'. Is that not allowed? It gives me the error 'Undefined function' everytime I run my solution.

Ned Gulley
on 3 Feb 2014

Aryaa: Your solution looks good. The problem is that combnk is in the Statistics Toolbox, and Cody is designed to use only functions from basic MATLAB.

Hassan Dehghani
on 5 Apr 2014

I used random numbers, but couldn't get it. :( (worked fine in my laptop.) :( I guess I haven't completely understood the question. :((

Hassan Dehghani
on 7 Apr 2014

why do i get internal server error :(((

Ned Gulley
on 8 Apr 2014

Hassan, where are you getting an internal server error? Maybe leave your comment on the solution that has the related error? I'm guessing that your solution relies on randomness, so if you don't get lucky, the answer will time out. You may get lucky on your computer, but not on the Cody server.

John D'Errico
on 22 Aug 2016 at 3:16

A basic one liner with intlinprog, but not an option in Cody. :)

1 Comment

John D'Errico
on 22 Aug 2016 at 12:08

(lol) An expectedly poor score, but still fun to write it in recursive form. Accomplished by adding spare arguments both as input to and output from split_it, then calling split_it recursively as needed.

1 player likes this solution

1 Comment

Ramoflaple
on 12 Aug 2015

smart cheater :)

1 Comment

Kirk
on 23 Jul 2014

Would be much harder problem if you did not allow repeated index numbers and insisted on numerical index instead of logical

3 Comments

George Berken
on 12 Feb 2014

combnk is not accepted ! ! !
nchoosek works :)

Ramoflaple
on 12 Aug 2015

Should this work for the last test!!!
why does the error "out of memory" not show?

Peng Liu
on 25 Apr 2016

Because once found, the function returns, thus diminishing the possibility of large I

1 Comment

nimin bbs
on 31 Jan 2014

This is not the shortest, but it is a oneliner.

1 Comment

Thomas Vanaret
on 30 Oct 2013

Only allowed by a too simple test suite...

1 player likes this solution

1 Comment

Thomas Vanaret
on 30 Oct 2013

This should be the best noted solution :
- No brute-force search
- No "regexp" trick
- No cheating (allowed by a too simple test suite)

6 players like this solution

2 Comments

Jean-Marie Sainthillier
on 17 Mar 2013

Wonderful solution with a factorization of ans.

Ramoflaple
on 12 Aug 2015

make full use of "half of the sum"

**Tags**

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn moreOpportunities for recent engineering grads.

Apply Today
12 players like this problem

12 players like this problem