Problem 660. Find a subset that divides the vector into equal halves
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))
Solution Stats
Problem Comments
-
14 Comments
why do i get internal server error :(((
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.
A basic one liner with intlinprog, but not an option in Cody. :)
Solution Comments
Show commentsProblem Recent Solvers391
Suggested Problems
-
257 Solvers
-
Change the sign of even index entries of the reversed vector
574 Solvers
-
Switch matrix to a column vector
360 Solvers
-
718 Solvers
-
Convert from Fahrenheit to Celsius
24965 Solvers
More from this Author50
Problem Tags
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!