Skip to main content
added 24 characters in body
Source Link
O.O.Balance
  • 1.6k
  • 1
  • 10
  • 19

Java 10, 96 8989 86 bytes

(a,b)->b->{var r=newint int[b];intr[]=new int[b],i=0,n=a.length;for(;i<n;)r[i/((n+b-1)/b)]+=a[i++];return r;} 

Try it online herehere.

Found a shorter way to write i/(n/b+(n%b==0?0:1) here: i/((n+b-1)/b)

Thanks to Olivier Grégoire for golfing 3 bytes.

Ungolfed version:

(input, -> bins) -> { // takeinput anis int[] as first parameter (original array), an intbins asis secondint (number of bins) varint resultresult[] = new int[bins];int[bins], // resulting array, initialized with all 0   int i = 0, // for iterating over the original array n = a.length; // length of the original array for(; i < n ;) // iterate over the original array result[i / ((n + bins - 1) / bins)] += input[i++]; // add the element to the right bin; that's bin n/bins if bins divides n, floor(n/bins)+1 otherwise return result; } 

Java 10, 96 89 bytes

(a,b)->{var r=new int[b];int i=0,n=a.length;for(;i<n;)r[i/((n+b-1)/b)]+=a[i++];return r;} 

Try it online here.

Found a shorter way to write i/(n/b+(n%b==0?0:1) here: i/((n+b-1)/b)

Ungolfed version:

(input, bins) -> { // take an int[] as first parameter (original array), an int as second (number of bins) var result = new int[bins]; // resulting array, initialized with all 0   int i = 0, // for iterating over the original array n = a.length; // length of the original array for(; i < n ;) // iterate over the original array result[i / ((n + bins - 1) / bins)] += input[i++]; // add the element to the right bin; that's bin n/bins if bins divides n, floor(n/bins)+1 otherwise return result; } 

Java 10, 96 89 86 bytes

a->b->{int r[]=new int[b],i=0,n=a.length;for(;i<n;)r[i/((n+b-1)/b)]+=a[i++];return r;} 

Try it online here.

Found a shorter way to write i/(n/b+(n%b==0?0:1) here: i/((n+b-1)/b)

Thanks to Olivier Grégoire for golfing 3 bytes.

Ungolfed version:

input -> bins -> { // input is int[] (original array), bins is int (number of bins) int result[] = new int[bins], // resulting array, initialized with all 0 i = 0, // for iterating over the original array n = a.length; // length of the original array for(; i < n ;) // iterate over the original array result[i / ((n + bins - 1) / bins)] += input[i++]; // add the element to the right bin; that's bin n/bins if bins divides n, floor(n/bins)+1 otherwise return result; } 
deleted 9 characters in body
Source Link
O.O.Balance
  • 1.6k
  • 1
  • 10
  • 19

Java 10, 9696 89 bytes

(a,b)->{var r=new int[b];int i=0,n=a.length;for(;i<n;)r[i/(n/b+(n%b==0?0:n+b-1)/b)]+=a[i++];return r;} 

Try it online herehere.

I'm not too happy with theFound a shorter way to write i/(n/b+(n%b==0?0:1) part; it seems too long but I haven't figured out a better way yet.here: i/((n+b-1)/b)

Ungolfed version:

(input, bins) -> { // take an int[] as first parameter (original array), an int as second (number of bins) var result = new int[bins]; // resulting array, initialized with all 0 int i = 0, // for iterating over the original array n = a.length; // length of the original array for(; i < n ;) // iterate over the original array result[i / (n/bins + (n %+ bins == 0 ? 0 :- 1) / bins)] += input[i++]; // add the element to the right bin; that's bin n/bins if bins divides n, floor(n/bins)+1 otherwise return result; } 

Java 10, 96 bytes

(a,b)->{var r=new int[b];int i=0,n=a.length;for(;i<n;)r[i/(n/b+(n%b==0?0:1))]+=a[i++];return r;} 

Try it online here.

I'm not too happy with the i/(n/b+(n%b==0?0:1) part; it seems too long but I haven't figured out a better way yet.

Ungolfed version:

(input, bins) -> { // take an int[] as first parameter (original array), an int as second (number of bins) var result = new int[bins]; // resulting array, initialized with all 0 int i = 0, // for iterating over the original array n = a.length; // length of the original array for(; i < n ;) // iterate over the original array result[i / (n/bins + (n % bins == 0 ? 0 : 1))] += input[i++]; // add the element to the right bin; that's bin n/bins if bins divides n, floor(n/bins)+1 otherwise return result; } 

Java 10, 96 89 bytes

(a,b)->{var r=new int[b];int i=0,n=a.length;for(;i<n;)r[i/((n+b-1)/b)]+=a[i++];return r;} 

Try it online here.

Found a shorter way to write i/(n/b+(n%b==0?0:1) here: i/((n+b-1)/b)

Ungolfed version:

(input, bins) -> { // take an int[] as first parameter (original array), an int as second (number of bins) var result = new int[bins]; // resulting array, initialized with all 0 int i = 0, // for iterating over the original array n = a.length; // length of the original array for(; i < n ;) // iterate over the original array result[i / ((n + bins - 1) / bins)] += input[i++]; // add the element to the right bin; that's bin n/bins if bins divides n, floor(n/bins)+1 otherwise return result; } 
Source Link
O.O.Balance
  • 1.6k
  • 1
  • 10
  • 19

Java 10, 96 bytes

(a,b)->{var r=new int[b];int i=0,n=a.length;for(;i<n;)r[i/(n/b+(n%b==0?0:1))]+=a[i++];return r;} 

Try it online here.

I'm not too happy with the i/(n/b+(n%b==0?0:1) part; it seems too long but I haven't figured out a better way yet.

Ungolfed version:

(input, bins) -> { // take an int[] as first parameter (original array), an int as second (number of bins) var result = new int[bins]; // resulting array, initialized with all 0 int i = 0, // for iterating over the original array n = a.length; // length of the original array for(; i < n ;) // iterate over the original array result[i / (n/bins + (n % bins == 0 ? 0 : 1))] += input[i++]; // add the element to the right bin; that's bin n/bins if bins divides n, floor(n/bins)+1 otherwise return result; }