veronikaaa86

06. Equal Arrays

Sep 28th, 2022
1,097
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.22 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 //10 20 30
  11.                 .stream(scanner.nextLine().split(" "))
  12.                 .mapToInt(Integer::parseInt)
  13.                 .toArray();
  14.         int[] secondArr = Arrays //10 20 30 40 50
  15.                 .stream(scanner.nextLine().split(" "))
  16.                 .mapToInt(Integer::parseInt)
  17.                 .toArray();
  18.  
  19.         int sum = 0;
  20.         int diffIndex = 0;
  21.         boolean isIdentical = true;
  22.         for (int i = 0; i < firstArr.length; i++) {
  23.             int firstNum = firstArr[i];
  24.             int secondNum = secondArr[i];
  25.  
  26.             if (firstNum == secondNum) {
  27.                 sum += firstNum;
  28.             } else {
  29.                 diffIndex = i;
  30.                 isIdentical = false;
  31.                 break;
  32.             }
  33.         }
  34.  
  35.         if (isIdentical) {
  36.             System.out.printf("Arrays are identical. Sum: %d", sum);
  37.         } else {
  38.             System.out.printf("Arrays are not identical. Found difference at %d index.", diffIndex);
  39.         }
  40.     }
  41. }
  42.  
Advertisement
Add Comment
Please, Sign In to add comment