A
A
Andrey Vydrin2015-05-24 10:59:54
C++ / C#
Andrey Vydrin, 2015-05-24 10:59:54

How to split an array into two subarrays?

It is necessary to split the array into two parts so that the sum of the elements is equal. The number of elements in subarrays can vary. For example: there is an array A={1, 1, 1, 1, 4}. It should split into two B={1, 1, 1, 1} and C={4}. It is possible, even better, without code, only an algorithm.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
V
Vitaly Vitrenko, 2015-05-24
@Tufitko

Partition problem .
There is a lot of information on google regarding this.

M
mamkaololosha, 2015-05-24
@mamkaololosha

Most in the forehead for O (N):
1) we go with one iterator on the right, the other on the left, until they collide.
2) if the right side is smaller, then continue moving to the left until there are equal sums or reach the beginning of the array
3) if the left side is smaller, then continue moving to the right until there are equal sums or reach the end of the array

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question