Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class ArraySymmetry {
- public static void main(String[] args) {
- Scanner scan = new Scanner(System.in);
- // Count of the array elements
- int n = Integer.parseInt(scan.nextLine());
- // Initialize the array, and input the elements
- int[] array = new int[n];
- for (int i = 0; i < array.length; i++) {
- array[i] = Integer.parseInt(scan.nextLine());
- }
- // Index to start from first element
- int left = 0;
- // Index to start from last element
- int right = array.length - 1;
- // First we assume that the array is specular
- boolean isSymmetrical = true;
- while (isSymmetrical && (left <= right)) {
- // Using postfix incrementation and decrementation
- // 1. Check the elements at the current indexes
- // 2. Updating the indexes left = left + 1, right = right - 1
- isSymmetrical = array[left++] - array[right--];
- }
- // Prints the result
- System.out.println(isSymmetrical);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement