fork download
  1. import java.util.Arrays;
  2. import java.util.stream.Stream;
  3.  
  4. class Solution {
  5. public static void main(String[] args) {
  6. int[] arr = { 1, 1, 1, 2, 3 };
  7.  
  8. int[] result = arr.length >= 2
  9. ? Stream.iterate(
  10. new int[] { arr[0], arr[1] },
  11. temp -> new int[] { temp[1], temp[0] + temp[1] }
  12. )
  13. .mapToInt(temp -> temp[1])
  14. .limit(arr.length)
  15. .toArray()
  16. : arr;
  17.  
  18. System.out.println(Arrays.toString(result));
  19. }
  20. }
Success #stdin #stdout 0.09s 48540KB
stdin
Standard input is empty
stdout
[1, 2, 3, 5, 8]