Given is an array **Arr** of integers. The task is to determine if the array has any subsequence of at least length 3 that is a palindrome.

**Examples:**

Input:Arr[] = [1, 2, 1]Output:YESExplanation:Here 1 2 1 is a palindrome.Input:Arr[] = [1, 1, 2, 2, 3, 3, 4, 4, 5, 5]Output:NOExplanation:Here no subsequence of length at least 3 exists which is a palindrome.

**Approach:**

- The idea is to check only for length 3 because if a palindromic subsequence of length greater than 3 exists then there is always a palindromic subsequence of length 3.
- To find the palindromic subsequence of length 3 we just need to find a pair of equal non-adjacent number.

Below is the implementation of the above approach:

## C++

`// C++ code to check if ` `// palindromic subsequece of ` `// length atleast 3 exist or not ` `#include<bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `string SubPalindrome(` `int` `n, ` `int` `arr[]) ` `{ ` ` ` `bool` `ok = ` `false` `; ` ` ` `for` `(` `int` `i = 0; i < n; i++) ` ` ` `{ ` ` ` `for` `(` `int` `j = i + 2; j < n; j++) ` ` ` `{ ` ` ` `if` `(arr[i] == arr[j]) ` ` ` `ok = ` `true` `; ` ` ` `} ` ` ` `} ` ` ` `if` `(ok) ` ` ` `return` `"YES"` `; ` ` ` `else` ` ` `return` `"NO"` `; ` `} ` ` ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` ` ` `// Input values to list ` ` ` `int` `Arr[] = {1, 2, 2, 3, 2}; ` ` ` ` ` `// Calculating length of array ` ` ` `int` `N = ` `sizeof` `(Arr)/` `sizeof` `(Arr[0]); ` ` ` ` ` `cout << SubPalindrome(N, Arr); ` `} ` ` ` `// This code is contributed by Bhupendra_Singh ` |

*chevron_right*

*filter_none*

## Java

`// Java code to check if ` `// palindromic subsequece of ` `// length atleast 3 exist or not ` ` ` `import` `java.util.*; ` ` ` `class` `GFG{ ` ` ` `static` `String SubPalindrome(` `int` `n, ` `int` `arr[]) ` `{ ` ` ` `boolean` `ok = ` `false` `; ` ` ` `for` `(` `int` `i = ` `0` `; i < n; i++) ` ` ` `{ ` ` ` `for` `(` `int` `j = i + ` `2` `; j < n; j++) ` ` ` `{ ` ` ` `if` `(arr[i] == arr[j]) ` ` ` `ok = ` `true` `; ` ` ` `} ` ` ` `} ` ` ` `if` `(ok) ` ` ` `return` `"YES"` `; ` ` ` `else` ` ` `return` `"NO"` `; ` `} ` ` ` ` ` `// Driver code ` `public` `static` `void` `main(String[] args) ` `{ ` ` ` ` ` `// Input values to list ` ` ` `int` `Arr[] = {` `1` `, ` `2` `, ` `2` `, ` `3` `, ` `2` `}; ` ` ` ` ` `// Calculating length of array ` ` ` `int` `N = Arr.length; ` ` ` ` ` `System.out.print(SubPalindrome(N, Arr)); ` `} ` `} ` ` ` `// This code contributed by sapnasingh4991 ` |

*chevron_right*

*filter_none*

## Python3

`# Python 3 code to check if ` `# palindromic subsequece of ` `# length atleast 3 exist or not ` `def` `SubPalindrome (n, arr): ` ` ` `ok ` `=` `False` ` ` `for` `i ` `in` `range` `(n): ` ` ` `for` `j ` `in` `range` `(i ` `+` `2` `, n): ` ` ` `if` `arr[i] ` `=` `=` `arr[j]: ` ` ` `ok ` `=` `True` ` ` `return` `(` `'YES'` `if` `ok ` `else` `'NO'` `) ` ` ` `# Driver code ` ` ` `# Input values to list ` `Arr ` `=` `[` `1` `, ` `2` `, ` `2` `, ` `3` `, ` `2` `] ` ` ` `# Calculating length of the array ` `N ` `=` `len` `(arr) ` ` ` `print` `(SubPalindrome(N, Arr)) ` |

*chevron_right*

*filter_none*

## C#

`// C# code to check if ` `// palindromic subsequece of ` `// length atleast 3 exist or not ` `using` `System; ` ` ` `public` `class` `GFG{ ` ` ` `static` `string` `SubPalindrome(` `int` `n, ` `int` `[]arr) ` `{ ` ` ` `bool` `ok = ` `false` `; ` ` ` `for` `(` `int` `i = 0; i < n; i++) ` ` ` `{ ` ` ` `for` `(` `int` `j = i + 2; j < n; j++) ` ` ` `{ ` ` ` `if` `(arr[i] == arr[j]) ` ` ` `ok = ` `true` `; ` ` ` `} ` ` ` `} ` ` ` `if` `(ok) ` ` ` `return` `"YES"` `; ` ` ` `else` ` ` `return` `"NO"` `; ` `} ` ` ` `// Driver code ` `static` `public` `void` `Main () ` `{ ` ` ` `// Input values to list ` ` ` `int` `[]Arr = { 1, 2, 2, 3, 2 }; ` ` ` ` ` `// Calculating length of array ` ` ` `int` `N = Arr.Length; ` ` ` `Console.WriteLine(SubPalindrome(N, Arr)); ` `} ` `} ` ` ` `// This code is contributed by shivanisinghss2110 ` |

*chevron_right*

*filter_none*

**Output:**

YES

**Time Complexity:**O(N^2)

## Recommended Posts:

- Length of longest Palindromic Subsequence of even length with no two adjacent characters same
- Check if at least half array is reducible to zero by performing some operations
- Smallest Palindromic Subsequence of Even Length in Range [L, R]
- Count pairs (p, q) such that p occurs in array at least q times and q occurs at least p times
- Longest Palindromic Substring using Palindromic Tree | Set 3
- Make palindromic string non-palindromic by rearranging its letters
- Minimum cuts required to convert a palindromic string to a different palindromic string
- Maximum value K such that array has at-least K elements that are >= K
- Maximum length subsequence such that adjacent elements in the subsequence have a common factor
- Length of longest subarray of length at least 2 with maximum GCD
- Construct a string of length L such that each substring of length X has exactly Y distinct letters
- Check if every pair of 1 in the array is at least K length apart from each other
- Check if the given array contains all the divisors of some integer
- Check if all the palindromic sub-strings are of odd length
- Check if a string contains a palindromic sub-string of even length
- Check if a string can be split into even length palindromic substrings
- Longest Palindromic Subsequence | DP-12
- Find the lexicographically largest palindromic Subsequence of a String
- Longest Palindromic Subsequence of two distinct characters
- Count All Palindromic Subsequence in a given String

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.