-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path027_CheckPalindromeArray.java
35 lines (29 loc) · 1.07 KB
/
027_CheckPalindromeArray.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
// https://www.geeksforgeeks.org/program-to-check-if-an-array-is-palindrome-or-not/
public class CheckPalindromeArray {
// TC = O(n), SC = O(1)
// Method to check if an array is a palindrome
public static boolean isPalindrome(int[] arr) {
int st = 0, end = arr.length - 1;
// Compare elements from start and end moving towards the center
while (st < end) {
if (arr[st] != arr[end]) {
// If any elements are not equal, array is not a palindrome
return false;
}
st++;
end--;
}
// If all elements are equal, array is a palindrome
return true;
}
public static void main(String[] args) {
// Sample array to test the palindrome check
int[] arr = { 1, 2, 3, 2, 1 };
// Check if the array is a palindrome and print the result
if (isPalindrome(arr)) {
System.out.println("Array is Palindrome");
} else {
System.out.println("Array is not Palindrome");
}
}
}