Here's another challenge that I came across recently:
Given an array of integers greater than zero, find if it is possible to split it in two (without reordering the elements), such that the sum of the two resulting arrays is the same. Print the resulting arrays.
To clarify, given the array [1,2,3,3,2,1]
it can it be split into two arrays [1,2,3]
and [3,2,1]
where the sum of each is equal.
Here's a set of data to work with:
List lists = [
[1,2,3,3,2,1],
[1,2,3,4,5,6,21],
[1,90, 50, 30, 5, 3, 2, 1 ],
[1, 50, 900, 1000 ],
[500,400,100,777,223,2456,4,39,1,222,78,93,7,100,23,1000,3,20,555,345,64,36,689,100,211,2000],
[1,2,3],
[1]
]
Here's my solution:
Which provides the following result:
[[1, 2, 3], [3, 2, 1]]
[[1, 2, 3, 4, 5, 6], [21]]
[[1, 90], [50, 30, 5, 3, 2, 1]]
[]
[[500, 400, 100, 777, 223, 2456, 4, 39, 1, 222, 78, 93, 7, 100, 23], [1000, 3, 20, 555, 345, 64, 36, 689, 100, 211, 2000]]
[[1, 2], [3]]
[]
How would you solve it differently?
Image by Franc-Comtois from Pixabay
Last week I had a bit of free time and decided to see how difficult it would be to write a Texas Hold'em poker simulation in Groovy. My goal wasn't...
One of the handiest features of Grails is the "flash" scope. The flash scope is "A temporary storage map that stores objects within the session for...
One of the cooler things about the Spark Java framework is built in websocket support thanks to the embedded Jetty server. I've long been fascinated...