-4

I want to divide the input I get from the user in different num sections in java. And then as output there have to come how many numbers are in each section. E.g. 1 1 2 3 4 4 5 6 7 8 9 10 11 12

  • in section (0 - 3) there are 4 numbers
  • in section (4 - 6) there are 4 numbers
  • in section (7 - 9) there are 3 numbers
  • and in section (10 - 12) there are 3 numbers

I have no idea how to manage this.

  • You could set a limit that you increase by 3 every step and then iterate over your input list while the values are <= your limit – tannerli Nov 15 '19 at 11:40
  • It's a difference in logic if you want to divide only this specific array or any array of any size containing arbitrary `int`s (or `long`s`?)... Please explain in detail how the grouping should be done for an array of size `n`. – deHaar Nov 15 '19 at 11:41
  • "in section (4 - 6) there are 4 numbers " why? – Federico klez Culloca Nov 15 '19 at 11:41
  • @tannerli Thanks for the advice and the first thoughts – anonymoustf Nov 15 '19 at 11:42
  • @deHaar it is not for a specific array. I already implemented all and just stuck at this step. For easy understanding here I asked with this example – anonymoustf Nov 15 '19 at 11:42
  • @Federico klez Culloca 4, 4, 5, 6 like in example above – anonymoustf Nov 15 '19 at 11:43
  • Oh, so you mean you have an array and you want to count how many numbers in a specific range the array contains? – Federico klez Culloca Nov 15 '19 at 11:44
  • There are a number of different approaches that would work. A simple approach would be to loop through the numbers, and for each one, work out what group it's in. When you know the group, you can increment a group counter. But you'd get a better response to your question If you attempted to write some code yourself, and ask people how it could be improved. – NickJ Nov 15 '19 at 11:44
  • @Federico Exactly – anonymoustf Nov 15 '19 at 11:55
  • @Nick Ok I‘ll do it thanks for the advice – anonymoustf Nov 15 '19 at 11:56

2 Answers2

1

Idea is to put every list element into the corresponding bucket

The ID of a bucket is computed by element-1/3 where / is a division with the remainder

Such division produces a quotient and a remainder, and the ID of bucket is equal quotient

It works except for 0 so it can be put by if condition to the first bucket

snieguu
  • 2,073
  • 2
  • 20
  • 39
0

So.. I have one idea. In this particular example you have step 3 for groups. Maybe let's divide numbers on 3 with round up and use result as an group number. For example: Math.ceil(1/3.0) == 1, so this is 1st group (0-3), for Math.ceil(5/3.0) == 2, so this is 2nd group, and etc. But notice that Math.ceil(0/3.0) == 0.

Ref about rounding up: Java Round up Any Number

P.S.: Please, don't blame me, my first answer on stackoverflow.