veronikaaa86

07. Condense Array to Number

Jan 25th, 2023
393
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.90 KB | None | 0 0
  1. package arrays;
  2.  
  3. import java.util.Arrays;
  4. import java.util.Scanner;
  5.  
  6. public class P07CondenseArrayToNumber {
  7.     public static void main(String[] args) {
  8.         Scanner scanner = new Scanner(System.in);
  9.  
  10.         int[] nums = Arrays
  11.                 .stream(scanner.nextLine().split(" "))
  12.                 .mapToInt(Integer::parseInt)
  13.                 .toArray();
  14.  
  15.         int[] condensed = new int[nums.length - 1];
  16.         for (int i = 0; i < nums.length; i++) {
  17.             if (nums.length == 1) {
  18.                 break;
  19.             }
  20.  
  21.             if (i == nums.length - 1) {
  22.                 int[] condensedNew = new int[condensed.length - 1];
  23.                 i = -1;
  24.                 nums = condensed;
  25.                 condensed = condensedNew;
  26.             } else {
  27.                 condensed[i] = nums[i] + nums[i + 1];
  28.             }
  29.         }
  30.  
  31.         System.out.println(nums[0]);
  32.     }
  33. }
  34.  
Add Comment
Please, Sign In to add comment