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
-
Calculate the Levenshtein distance between two strings
1453 Solvers
-
1554 Solvers
-
Make a run-length companion vector
644 Solvers
-
Magic is simple (for beginners)
9723 Solvers
-
4015 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!