fork download
  1. import java.util.*;
  2. import java.io.*;
  3.  
  4. class Main
  5. {
  6. public static void main (final String[] args)
  7. {
  8. final int[] input = { 1, 4, 5, 2, 6, 7, 9, 8, 7, 10 };
  9. // size * (size + 1) / 2 = input.length
  10. int size = (-1 + (int) Math.sqrt(1 + 8 * input.length)) / 2;
  11. // derived by quadratic formula
  12. final int[] result = new int[size];
  13. final int[] sum = new int[input.length + 1];
  14. for (int i = 1; i <= input.length; i++) {
  15. sum[i] = sum[i - 1] + input[i - 1];
  16. }
  17. for (int i = 1, j = 0; i <= input.length; i += ++j) {
  18. result[j] = sum[i + j] - sum[i - 1];
  19. }
  20. System.out.println(Arrays.toString(result));
  21. }
  22. }
Success #stdin #stdout 0.06s 32520KB
stdin
Standard input is empty
stdout
[1, 9, 15, 34]