Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package S4_ForLoops;
- import java.util.Scanner;
- public class HalfSumElement {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- int n = Integer.parseInt(scanner.nextLine());
- int maxNum = Integer.MIN_VALUE;
- int sum = 0;
- for (int i = 1; i <= n; i++) {
- // read n numbers
- int number = Integer.parseInt(scanner.nextLine());
- // find sum
- // sum = sum + number
- sum += number;
- // check for max number
- if(number > maxNum) {
- maxNum = number;
- }
- }
- int diff = sum - maxNum;
- if(maxNum == diff) {
- System.out.printf("Yes%nSum = %d", maxNum);
- } else {
- int diff2 = Math.abs(maxNum - diff);
- System.out.printf("No%nDiff = %d", diff2);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement