martinvalchev

Half Sum Element

Nov 4th, 2017
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.77 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.         int sum = 0;
  8.         int  maxNum = Integer.MIN_VALUE;
  9.  
  10.         for (int i = 0; i < n; i++) {
  11.             int curentNum = Integer.parseInt(scanner.nextLine());
  12.             sum = sum + curentNum;
  13.             if (curentNum > maxNum) {
  14.                 maxNum = curentNum;
  15.             }
  16.         }
  17.  
  18.             if (sum - maxNum == maxNum) {
  19.                 System.out.printf("Yes %nSum = %d", (sum - maxNum));
  20.             } else {
  21.                 System.out.printf("No %nDiff = %d", Math.abs(maxNum - (sum - maxNum)));
  22.             }
  23.  
  24.         }
  25.  
  26.     }
Advertisement
Add Comment
Please, Sign In to add comment