Advertisement
Ligh7_of_H3av3n

7. CondenseArrayToNumber

Jan 26th, 2024
48
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.91 KB | None | 0 0
  1. package Lekcii;
  2.  
  3. import java.util.Scanner;
  4.  
  5. public class CondenseArrayToNumber {
  6.     public static void main(String[] args) {
  7.         Scanner scanner = new Scanner(System.in);
  8.  
  9.         // Read the integers
  10.         String input = scanner.nextLine();
  11.  
  12.         // Extract integers from the input string
  13.         String[] inputs = input.split("\\s+");
  14.         int[] nums = new int[inputs.length];
  15.         for (int i = 0; i < inputs.length; i++) {
  16.             nums[i] = Integer.parseInt(inputs[i]);
  17.         }
  18.  
  19.         // Condense the array
  20.         while (nums.length > 1) {
  21.             int[] condensed = new int[nums.length - 1];
  22.             for (int i = 0; i < nums.length - 1; i++) {
  23.                 condensed[i] = nums[i] + nums[i + 1];
  24.             }
  25.             nums = condensed;
  26.         }
  27.  
  28.         // Print the condensed result
  29.         System.out.println(nums[0]);
  30.  
  31.         scanner.close();
  32.     }
  33. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement