Advertisement
veronikaaa86

06. Equal Arrays

Jan 25th, 2023
854
0
Never
1
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.20 KB | None | 0 0
  1. package arrays;
  2.  
  3. import java.util.Arrays;
  4. import java.util.Scanner;
  5.  
  6. public class P06EqualArrays {
  7.     public static void main(String[] args) {
  8.         Scanner scanner = new Scanner(System.in);
  9.  
  10.         int[] firstArr = Arrays
  11.                 .stream(scanner.nextLine().split(" "))
  12.                 .mapToInt(Integer::parseInt)
  13.                 .toArray();
  14.  
  15.         int[] secondArr = Arrays
  16.                 .stream(scanner.nextLine().split(" "))
  17.                 .mapToInt(Integer::parseInt)
  18.                 .toArray();
  19.        
  20.         int sum = 0;
  21.         int diffIndex = -1;
  22.         boolean isIdentical = true;
  23.         for (int i = 0; i < firstArr.length; i++) {
  24.             int firstNum = firstArr[i];
  25.             int secondNum = secondArr[i];
  26.  
  27.             if (firstNum == secondNum) {
  28.                 sum += firstNum;
  29.             } else {
  30.                 isIdentical = false;
  31.                 diffIndex = i;
  32.                 break;
  33.             }
  34.         }
  35.  
  36.         if (isIdentical) {
  37.             System.out.printf("Arrays are identical. Sum: %d", sum);
  38.         } else {
  39.             System.out.printf("Arrays are not identical. Found difference at %d index.", diffIndex);
  40.         }
  41.     }
  42. }
  43.  
Advertisement
Comments
Add Comment
Please, Sign In to add comment
Advertisement