0

I can't get to solution how to check if the array is mirrored

public static void main(String[] args) {
    Scanner sc=new Scanner(System.in);
    int a=0 ;
    System.out.print("Array length:");
    int b= sc.nextInt();    
    int [] array = new int [b];
    for(int i=0;i<b;i++) {
        System.out.print("Type the number "+i+" element: ");
        a=sc.nextInt();
        array[i]=a;
    }
    System.out.println(Arrays.toString(array));
    sc.close();
}

and when I type like for example Array length: 3 and my digits are 1 2 1

Array length:3
Type the number 0 element: 1
Type the number 1 element: 2
Type the number 2 element: 1
[1, 2, 1]

I get this in console , but now I don't know how to say if it is mirrored or not.

Ruslan
  • 6,090
  • 1
  • 21
  • 36
  • 3
    How would you, as a human, determine if that array is mirrored? – Compass Mar 19 '19 at 20:25
  • 1
    Start off by checking if the last and first element are equal... then 2nd and pre-last... until you meet up in center – Taavi Kivimaa Mar 19 '19 at 20:27
  • If I understand correctly what you're trying to do, you compare the first array element to the last, the second to the penultimate, *etc*. until either you find a mismatch or you reach the middle. Since the number of elements is not determined until runtime, you'll want to use a loop for that. – John Bollinger Mar 19 '19 at 20:28

3 Answers3

1

You could check if the array is palindrome by comparing it with reversed copy of the original array. Using ArrayUtils.reverse from Apache commons:

int[] arrCopy = Arrays.copyOf(array, array.length);
ArrayUtils.reverse(arrCopy);
boolean isPalindrome = Arrays.equals(array, arrCopy);

See more How do I reverse an int array in Java?

Ruslan
  • 6,090
  • 1
  • 21
  • 36
1

I am not 100% sure about your definition of mirrored. But this will detect simmetric arrays. Hope it helps.

public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
        int a=0 ;
        System.out.print("Array length:");
        int b= sc.nextInt();    
        int [] array = new int [b];
        for(int i=0;i<b;i++) {
            System.out.print("Type the number "+i+" element: ");
            a=sc.nextInt();
            array[i]=a;
        }
        System.out.println(Arrays.toString(array));

        int l = (b - b%2)/2;
        boolean mirrored = true;
        for(int i=0;i<l;i++) {
            if(array[i]!=array[array.length-1-i]) {
                mirrored = false;
                break;
            }
        }      
        if(mirrored) {
            System.out.println("The array is mirrored!! Fantastic.");
        }
        sc.close();
    } 
Agustin Tena
  • 131
  • 4
0

made a small snippet if you are testing the code within main itself

int length = array.length;
boolean mirror = true;
for (int index = 0; index < array.length; index++) {
    if(mirror) {
        int start = array[index];
        int end = array[--length];
        if (length < index) {
            break;
        }
        if (start != end) {
            mirror = false;
        }
    }
}
if(mirror) {
    System.out.println("is mirror");
}
else {
    System.out.println("is not mirror");
}
Mischiefz
  • 127
  • 16