2

Suppose the elements of an input array are arranged in ascending order, as in this sample:

int[] a= {23,24,25,30,34,36,40,41,43,45,50};

I need to split the array into different sub-arrays. Each sub-array will contain elements such that their sum will be less than or equal to 100.

For the above input, my output should be:

b1[]= {23,24,25};

b2[]= {30,34,36};

b3[]= {40,41};

b4[]= {43,45};

b5[]= {50};

The value and number of elements in the input array may vary.

I am very new to Java, and I am completely stuck. Please suggest a way for me to proceed.

Pops
  • 30,199
  • 37
  • 136
  • 151
ramansingh
  • 45
  • 8
  • 3
    What have you tried? You will tend to get better responses to your questions if you first make an attempt and then post a specific issue you are having along with the non-working code. – James Montagne Oct 05 '12 at 19:58
  • 3
    This involves looping & summing, have you attempted this? – Reimeus Oct 05 '12 at 19:58
  • 1
    @JustinKSU: Note that the homework tag is now [officially deprecated](http://meta.stackexchange.com/questions/147100/the-homework-tag-is-now-officially-deprecated) and should no longer be added to questions – David Robinson Oct 05 '12 at 20:09
  • in a loop, continue to sum each element and check its value, if it is <=100, add elements into subarray and when the sum==100, reset it to zero and start new subarray. continue till you reach the end of your source array. – Victor Mukherjee Oct 05 '12 at 20:11
  • Can you describe, in English (or your native language), how to solve this problem? Try writing it out with pen and paper. – Code-Apprentice Oct 05 '12 at 20:16
  • Hmm... on second read, this question could be worth answering in spite of the fact that it's pretty localized. Voting to close might have been the wrong move. – Pops Oct 05 '12 at 20:57

2 Answers2

1

if the order of numbers is important you can write like this (this code just print sun arrays)

int sum = 0;
for(int i=0;i<a.length;i++){
   if(sum + a[i] <= 100){
       sum += a[i];
       System.out.print(a[i]+" ");
   }else{
       sum = 0;
       System.out.println("");
   }
}
Farnabaz
  • 4,030
  • 1
  • 22
  • 42
1

I'm going to give you some ideas on how to do this, and leave the coding up to you. If you have issues with specific bits of the implementation, feel free to ask.

The first thing you'll have to do here is create a place to store the sub-arrays. Since you don't know how many sub-arrays you'll end up with, something dynamic like a List would be a good choice, but if you haven't learned about those yet, a big array could do the job too.

How long should the big array be? Well, each sub-array is smaller than 100, right? You could add up all the elements in the input and divide by 100 to get an estimate.

Once you've done that initial setup, you need to figure out how to split up the input array into sub-arrays. You know the rule for sub-arrays is "less than 100," so you have to do some addition.

Look at the input elements one by one. Each one of them has to end up in a sub-array; the only question is when to stop one sub-array and start the next one. So, if adding the next input number would make your sub-array total go over 100, store the current sub-array in the big array, and put that input into a brand-new sub-array.

Of course, adding up a bunch of numbers you've added before is a waste of time. You could keep a separate variable on the side for storing the total of the current sub-array so you don't have to add all the elements on the current sub-array for every input. Remember to re-set that variable to 0 when you create a new sub-array!

Finally, it's possible that you'll run out of space in a sub-array or the big array. If that happens, just create a new one that's twice as big and copy the current values in.

Pops
  • 30,199
  • 37
  • 136
  • 151
  • I couldn't think of a way to take advantage of the fact that the input was sorted in ascending order. I'd be interested in seeing a solution that does. – Pops Oct 05 '12 at 21:27