plamenavylcheva

CondenseArrayToNumber

Jun 6th, 2023
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.79 KB | None | 0 0
  1. package Lab;
  2.  
  3. import java.util.Arrays;
  4. import java.util.Scanner;
  5.  
  6. public class CondenseArrayToNumber {
  7. public static void main(String[] args) {
  8. Scanner scanner = new Scanner(System.in);
  9. int[] array = Arrays.stream(scanner.nextLine().split(" ")).
  10. mapToInt(e -> Integer.parseInt(e)).toArray();
  11.  
  12. if (array.length == 1) {
  13. System.out.println(array[0]);
  14. } else {
  15. while (array.length != 1) {
  16. int[] condensedArr = new int[array.length - 1];
  17. for (int i = 0; i < array.length - 1; i++) {
  18. condensedArr[i] = array[i] + array[i + 1];
  19. }
  20. array=condensedArr;
  21. }
  22. System.out.println(array[0]);
  23.  
  24. }
  25.  
  26. }
  27. }
  28.  
  29.  
Advertisement
Add Comment
Please, Sign In to add comment