publicint[] distributeCandies(int candies, int num_people) { intn= num_people; // how many people received complete gifts intp= (int)(Math.sqrt(2 * candies + 0.25) - 0.5); intremaining= (int)(candies - (p + 1) * p * 0.5); introws= p / n, cols = p % n;
int[] d = newint[n]; for(inti=0; i < n; ++i) { // complete rows d[i] = (i + 1) * rows + (int)(rows * (rows - 1) * 0.5) * n; // cols in the last row if (i < cols) d[i] += i + 1 + rows * n; } // remaining candies d[cols] += remaining; return d; }