Advertisement
veronikaaa86

06. Equal Arrays

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