lundi 26 février 2018

Find 2 subsets in array that have minimum difference and consecutive elements

I came across a variation of the subset sum problem that i find quite interesting. So, given an array of (positive) elements, you have to partition it in 2 sets so that their difference is minimum. But here is the catch. The elements have to be consecutive and they also work in circular order. Here are 2 examples so you can understand what I mean:

EXAMPLE 1:

INPUT: 7 5 1 3 8 9 11 8

OUTPUT: 0 ( set 1: {11,8,7},set 2: {5,1,3,8,9}

result picture.


EXAMPLE 2:

INPUT:10 14 75 90 3 5 40 4 8

OUTPUT: 27 (set 1: {4,8,10,14,75},set 2: {90,3,5,40})

What is a possible solution using C++? Thanks!

Aucun commentaire:

Enregistrer un commentaire