Advertisement
GabrielHr00

02. Half Sum Element

Jun 4th, 2023
240
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.80 KB | None | 0 0
  1. package S4_FoorLoops;
  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 digit = Integer.parseInt(scanner.nextLine());
  15.  
  16.             if (digit > maxNum) {
  17.                 maxNum = digit;
  18.             }
  19.  
  20.             //sum = sum + digit;
  21.             sum += digit;
  22.         }
  23.  
  24.         //sum = sum - maxNum;
  25.         sum -= maxNum;
  26.  
  27.         if(sum == maxNum) {
  28.             System.out.printf("Yes%nSum = %d", sum);
  29.         } else {
  30.             System.out.printf("No%nDiff = %d", Math.abs(sum - maxNum));
  31.         }
  32.  
  33.     }
  34. }
  35.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement