Advertisement
GabrielHr00

HalfSumElement

Nov 20th, 2022
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.91 KB | None | 0 0
  1. package S4_ForLoops;
  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 maxNum = Integer.MIN_VALUE;
  11. int sum = 0;
  12.  
  13. for (int i = 1; i <= n; i++) {
  14. // read n numbers
  15. int number = Integer.parseInt(scanner.nextLine());
  16.  
  17. // find sum
  18. // sum = sum + number
  19. sum += number;
  20.  
  21. // check for max number
  22. if(number > maxNum) {
  23. maxNum = number;
  24. }
  25. }
  26.  
  27. int diff = sum - maxNum;
  28. if(maxNum == diff) {
  29. System.out.printf("Yes%nSum = %d", maxNum);
  30. } else {
  31. int diff2 = Math.abs(maxNum - diff);
  32. System.out.printf("No%nDiff = %d", diff2);
  33. }
  34. }
  35. }
  36.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement