Advertisement
GabrielHr00

02. HalfSumElement

Jan 26th, 2025
166
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.80 KB | None | 0 0
  1. package forLoop_Exercise;
  2.  
  3. import java.util.Scanner;
  4.  
  5. public class HalfSumElement {
  6.     public static void main(String[] args) {
  7.         Scanner scanner = new Scanner(System.in);
  8.         int n = Integer.parseInt(scanner.nextLine());
  9.  
  10.         int sum = 0;
  11.         int maxNum = Integer.MIN_VALUE;
  12.  
  13.         for (int i = 1; i <= n; i++) {
  14.             int num = Integer.parseInt(scanner.nextLine());
  15.  
  16.             sum = sum + num;
  17.  
  18.             if (num > maxNum) {
  19.                 maxNum = num;
  20.             }
  21.         }
  22.  
  23.         sum = sum - maxNum;
  24.  
  25.         if (sum == maxNum) {
  26.             System.out.printf("Yes\n" +
  27.                     "Sum = %d", maxNum);
  28.         } else {
  29.             System.out.printf("No\n" +
  30.                     "Diff = %d", Math.abs(maxNum - sum));
  31.         }
  32.  
  33.     }
  34. }
  35.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement