Advertisement
Guest User

Untitled

a guest
Feb 9th, 2018
278
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.11 KB | None | 0 0
  1. public class ArraySymmetry {
  2.     public static void main(String[] args) {
  3.         Scanner scan = new Scanner(System.in);
  4.        
  5.         // Count of the array elements
  6.         int n = Integer.parseInt(scan.nextLine());
  7.        
  8.         // Initialize the array, and input the elements
  9.         int[] array = new int[n];
  10.         for (int i = 0; i < array.length; i++) {
  11.             array[i] = Integer.parseInt(scan.nextLine());
  12.         }
  13.        
  14.         // Index to start from first element
  15.         int left = 0;
  16.         // Index to start from last element
  17.         int right = array.length - 1;
  18.        
  19.         // First we assume that the array is specular
  20.         boolean isSymmetrical = true;
  21.        
  22.         while (isSymmetrical && (left <= right)) {
  23.             // Using postfix incrementation and decrementation
  24.             // 1. Check the elements at the current indexes
  25.             // 2. Updating the indexes left = left + 1, right = right - 1
  26.             isSymmetrical = array[left++] - array[right--];
  27.         }
  28.        
  29.         // Prints the result
  30.         System.out.println(isSymmetrical);
  31.     }
  32. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement