title: Divide an Array in equal size, such that value of given function is minimum
body: <p>I've came across the following problem statement. </p>
  
  <blockquote>
    <p>You have a list of natural numbers of size <code>N</code> and you must distribute the values in two lists <code>A</code> and <code>B</code> of size <code>N/2</code>, so that the squared sum of <code>A</code> elements is the nearest possible to the multiplication of the <code>B</code> elements.</p>
    
    <p><strong>Example:</strong>
    Consider the list 7 11 1 9 10 3 5 13 9 12.<br>
    The optimized distribution is:<br>
    List A: 5 9 9 12 13<br>
    List B: 1 3 7 10 11<br>
    which leads to the difference abs( (5+9+9+12+13)^2 - (1*3*7*10*11) ) = 6<br>
    Your program should therefore output 6, which is the minimum difference that can be achieved.</p>
  </blockquote>
  
  <p><strong>What I've tried:</strong></p>
  
  <p>I've tried Greedy approach in order to solve this problem. I took two variables <code>sum</code> and <code>mul</code>. Now I started taking elements from the given set one by one and tried adding it in both the variables and calculated current 
  square of sum and multiplication. Now finalize the element in one of the two sets, such that the combination gives minimum possible value.</p>
  
  <p>But this approach is not working in the given example itselt. I can't figure out what approach could be used here. </p>
  
  <p>I'm <strong>not</strong> asking for exact code for the solution. Any possible approach and the reason why it is working, would be fine. </p>
  
  <p><strong>EDIT:</strong></p>
  
  <p>Source: <a href="https://www.codingame.com/ide/puzzle/the-two-piles-difference" rel="nofollow noreferrer">CodinGame, Community puzzle</a></p>