galinyotsev123

ProgBasicsJavaBook5.1Loops10HalfSumElement

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