Code Logo

Deque Palindrome Check

Published at25 Jul 2026
Deque Easy 0 views
Like0

Check if an array is a palindrome (same forwards and backwards) — simulating checking from both ends of a deque.

Example Input & Output

Example 1
Input
[]
Output
true
Explanation

Empty

Example 2
Input
[1,2,3,2,1]
Output
true
Explanation

Palindrome

Example 3
Input
[1,2,3,3,2,1]
Output
true
Explanation

Even length palindrome

Example 4
Input
[1,2,3,4]
Output
false
Explanation

Not palindrome

Example 5
Input
[1]
Output
true
Explanation

Single element

Algorithm Flow

Recommendation Algorithm Flow for Deque Palindrome Check
Recommendation Algorithm Flow for Deque Palindrome Check

Solution Approach

function solution(arr) {
  var l=0,r=arr.length-1;
  while(l<r){if(arr[l]!==arr[r])return false;l++;r--;}
  return true;
}

Best Answers

java
class Solution {
    public boolean solution(int[] nums) {
        int l=0,r=nums.length-1;while(l<r){if(nums[l]!=nums[r])return false;l++;r--;}return true;
    }
}