Problem#
Given an array, the task is to determine whether an array is a palindrome or not.
Examples#
Example 1:
1
2
| Input: arr = [3, 6, 0, 6, 3]
Output: true
|
Example 2:
1
2
| Input: arr = [1, 2, 3, 4, 5]
Output: false
|
Solution#
Method 1 - Iterative Using 2 Pointers#
Initially left is at 0 and right at n-1. Then just compare till left is less than right.
Code#
1
2
3
4
5
6
7
8
9
10
11
| boolean isPalindrome(int[] nums) {
int l = 0, r = nums.length - 1;
while(l <= r){
if(nums[l] != nums[r]){
return false;
}
l++;
r--;
}
return true;
}
|