0

I have the following array

var nums = [1,2,3,4];

Now i want to convert this into 2 new arrays where the sum of both arrays should be same, so the final result will be something

arr1 = [2,3];
arr2 = [1,4];
Aman Virk
  • 3,909
  • 8
  • 40
  • 51
  • It's good to know what you want. What have you tried? – Karl-André Gagnon Apr 18 '14 at 18:31
  • In addition, do the arrays have to be the same? – Richard Apr 18 '14 at 18:32
  • `Now i want to convert this into 2 new arrays where the sum of both arrays should be same` and what if you can't? – Matt Burland Apr 18 '14 at 18:33
  • Yes they need to be same, and so far all i am just finding a way on where to start from – Aman Virk Apr 18 '14 at 18:34
  • Then i will post it on stackoverflow to find out how dumb i can be – Aman Virk Apr 18 '14 at 18:34
  • 2
    @AmanVirk: You misunderstood. Consider: `var nums = [1,2,3,5]` it is impossible to divide this into two arrays with the same sum. What now? You need to define the domain of your problem. – Matt Burland Apr 18 '14 at 18:35
  • 1
    I hate to ask this question but..why? does this need to work with any size array? will the numbers always be in order? will they always be just increments of 1? ...EDIT: yeah what Aman Virk said – RustyH Apr 18 '14 at 18:36
  • 1
    How many elements will be in the array? The length can always be divided by 2? – Ragnar Apr 18 '14 at 18:37
  • Actually this an algorithm i am trying to solve , i also found it senseless earlier and then i did a quick google where so many people have tried to solve this, myself i am trying to find a center point which can tell me what the final total is going to be ..or some kind of a solid start where i can start from – Aman Virk Apr 18 '14 at 18:38
  • Here is the link to the question http://www.geeksforgeeks.org/forums/topic/nagarro-interview-question-for-software-engineerdeveloper-about-arrays/ – Aman Virk Apr 18 '14 at 18:38
  • Here is another SO thread http://stackoverflow.com/questions/5898104/how-to-optimally-divide-an-array-into-two-subarrays-so-that-sum-of-elements-in-b – Aman Virk Apr 18 '14 at 18:40
  • I little hint i got , the sum of the actual array should be divisible by 2 and the final total of each new array will be equal to the result after dividing the actual array .. so the total in this case is 10 , which means each array will have total of 5 coz 10 /2 is 5 – Aman Virk Apr 18 '14 at 18:42

0 Answers0