12
0
The task in this challenge is to put elements of an array into time bins. The input will be a non-decreasing array of positive integers representing the time of events, and an integer which represents the size of each bin. Let us start with an example. We call the input array A
and the output array O
.
`A = [1,1,1,2,7,10]` and `bin_size = 2`.
`O = [4,0,0,1,1]`.
Why? With a bin_size = 2
, we'll have the following intervals: (0,2], (2,4], (4,6], (6,8], (8,10]
, where four items (1,1,1,2)
are within the first interval (0,2]
, none in the second and third intervals, one 7
in the interval (6,8]
, and one 10
in the interval (8,10]
.
Your code should consider every interval of length bin_size
starting from 0
and count how many numbers in A
there are in each. You should always include the right hand end of an interval in a bin so in the example above 2
is included in the count of 4
. Your code should run in linear time in the sum of the lengths of the input and output.
More examples:
`A = [1,2,7,12,15]` and `bin_size = 5`.
`O = [2, 1, 2]`.
`A = [1,2,7,12,15]` and `bin_size = 3`.
`O = [2,0,1,1,1]`.
You can assume that input and output can be given in any format you find convenient. You can use any languages and libraries you like.
Are outputs with trailing
0
s allowed? So returning[2,0,1,1,1,0]
instead of[2,0,1,1,1]
? – Kevin Cruijssen – 2018-03-30T14:10:00.983No trailing zeros please. – None – 2018-03-30T14:11:00.260
2What about situations where max array value is not a multiple of
bin_size
, should we really handle these? It seems that most answers do, but if so, it would be nice to add a test case for this scenario to prevent confusion. – Kirill L. – 2018-03-31T08:40:43.780@KirillL. Yes they should be handled too. – None – 2018-03-31T14:28:47.967
Can you please explain, in your first example, why is the value
2
not in the second interval(2,4]
– GPS – 2018-04-01T14:36:54.703@GPS The righthand end is included but not the left hand end. – None – 2018-04-01T15:04:48.690
So,
(a,b]
includesb
but nota
for alla<b
. If input array has a0
, which bin does that get into, or in other words, is0
a positive integer in context of this problem? – GPS – 2018-04-01T16:38:31.4431@GPS 0 is not a positive integer. This isn’t an accident :) – None – 2018-04-01T19:25:04.690