Advertisement
Spocoman

02. Half Sum Element

Aug 27th, 2024
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.75 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class HalfSumElement {
  4.     public static void main(String[] args) {
  5.         Scanner scanner = new Scanner(System.in);
  6.         int n = Integer.parseInt(scanner.nextLine());
  7.  
  8.         int sumNumbers = 0, maxNumber = 0;
  9.  
  10.         for (int i = 1; i <= n; i++) {
  11.             int number = Integer.parseInt(scanner.nextLine());
  12.             sumNumbers += number;
  13.             if (number > maxNumber) {
  14.                 maxNumber = number;
  15.             }
  16.         }
  17.  
  18.         int result = sumNumbers - maxNumber;
  19.  
  20.         if (maxNumber == result) {
  21.             System.out.printf("Yes\nSum = %d\n", maxNumber);
  22.         } else {
  23.             System.out.printf("No\nDiff = %d\n", Math.abs(maxNumber - result));
  24.         }
  25.     }
  26. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement