Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- public class Main {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- int n = Integer.parseInt(scanner.nextLine());
- // For this problem, since we will be comparing two pairs at one time, we will be using two sums:
- int sum1 = 0;
- int sum2 = 0;
- // We'll also have a variable for the max difference since if they aren't all equal we will need it:
- int maxDifference = 0;
- for (int i = 0; i < n; i++) {
- // In a single for loop we are reading a pair with each iteration:
- int num1 = Integer.parseInt(scanner.nextLine());
- int num2 = Integer.parseInt(scanner.nextLine());
- // On each even iteration (0, 2, 4...) we will be changing the first sum:
- if (i % 2 == 0){
- sum1 = num1 + num2;
- }else{ // On each odd iteration we'll be changing the second one:
- sum2 = num1 + num2;
- }
- // This variable will keep the current difference between the pairs:
- int difference = 0;
- // We check whether the sums aren't equal. Notice the first condition (i != 0) - it's important to have it,
- // since otherwise we will be comparing the first sum with 0, since the second sum won't be changed yet:
- if (i != 0 && sum1 != sum2){
- // The difference is calculated with the help of the abs method since we don't know which sum is bigger:
- difference = Math.abs(sum1 - sum2);
- // Then if the current difference is bigger than the maximum difference, we change its value:
- if (difference != 0 && difference > maxDifference){
- maxDifference = difference;
- }
- }
- }
- // Print the results:
- // If the maxDifference is still 0, as we initialized it, then all pairs were equal:
- if (maxDifference == 0){
- System.out.printf("Yes, value=%d", sum1);
- }
- else{
- System.out.printf("No, maxdiff=%d", maxDifference);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement