# Smallest Palindromic Subsequence of Even Length in Range [L, R]

Given a string of size **N** and some queries, the task is to find the lexicographically smallest palindromic subsequence of even length in range **[L, R]** for each query. If no such palindromic subsequence exists then the print **-1**.

**Examples:**

Input:str = “dbdeke”, query[][] = {{0, 5}, {1, 5}, {1, 3}}

Output:dd

ee

-1

Explanation:dd

In the first query, possible palindromic subsequences are “dd”, “ee”, “ddee” and “dd” which are lexicographically smallest.

In the second query, only possible palindromic subsequence is “ee”.

In the third query, no such palindromic subsequence is possible.

Input:str = “abcd”, query[][] = {{0, 3}}

Output:-1

**Approach:** The main observation of this problem is if there exists a palindromic subsequence, then it must be of **length > 2**. Therefore the resultant subsequence will be a string of **length > 2** with the same characters. Choose the smallest character among those characters which have a frequency greater than **1** in the range **[L, R]** and print that character twice. If there is no such character exists then print **-1**.

Below is the implementation of above approach:

## C++

`// C++ program to find lexicographically smallest ` `// palindromic subsequence of even length ` ` ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` `const` `int` `N = 100001; ` ` ` `// Frequency array for each character ` `int` `f[26][N]; ` ` ` `// Preprocess the frequency array calculation ` `void` `precompute(string s, ` `int` `n) ` `{ ` ` ` `// Frequency array to track each character ` ` ` `// in position 'i' ` ` ` `for` `(` `int` `i = 0; i < n; i++) { ` ` ` `f[s[i] - ` `'a'` `][i]++; ` ` ` `} ` ` ` ` ` `// Calculating prefix sum ` ` ` `// over this frequency array ` ` ` `// to get frequency of a character ` ` ` `// in a range [L, R]. ` ` ` `for` `(` `int` `i = 0; i < 26; i++) { ` ` ` `for` `(` `int` `j = 1; j < n; j++) { ` ` ` `f[i][j] += f[i][j - 1]; ` ` ` `} ` ` ` `} ` `} ` ` ` `// Util function for palindromic subsequences ` `int` `palindromicSubsequencesUtil(` `int` `L, ` `int` `R) ` `{ ` ` ` ` ` `int` `c, ok = 0; ` ` ` ` ` `// Find frequency of all characters ` ` ` `for` `(` `int` `i = 0; i < 26; i++) { ` ` ` ` ` `// For each character ` ` ` `// find it's frequency ` ` ` `// in range [L, R] ` ` ` `int` `cnt = f[i][R]; ` ` ` `if` `(L > 0) ` ` ` `cnt -= f[i][L - 1]; ` ` ` ` ` `if` `(cnt > 1) { ` ` ` ` ` `// If frequency in this range is > 1, ` ` ` `// then we must take this character, ` ` ` `// as it will give ` ` ` `// lexicographically smallest one ` ` ` `ok = 1; ` ` ` `c = i; ` ` ` `break` `; ` ` ` `} ` ` ` `} ` ` ` ` ` `// There is no character ` ` ` `// in range [L, R] such ` ` ` `// that it's frequency is > 1. ` ` ` `if` `(ok == 0) { ` ` ` ` ` `return` `-1; ` ` ` `} ` ` ` ` ` `// Return the character's value ` ` ` `return` `c; ` `} ` ` ` `// Function to find lexicographically smallest ` `// palindromic subsequence of even length ` `void` `palindromicSubsequences(` `int` `Q[][2], ` `int` `l) ` `{ ` ` ` `for` `(` `int` `i = 0; i < l; i++) { ` ` ` ` ` `// Find in the palindromic subsequences ` ` ` `int` `x ` ` ` `= palindromicSubsequencesUtil( ` ` ` `Q[i][0], Q[i][1]); ` ` ` ` ` `// No such subsequence exists ` ` ` `if` `(x == -1) { ` ` ` `cout << -1 << ` `"\n"` `; ` ` ` `} ` ` ` `else` `{ ` ` ` `char` `c = ` `'a'` `+ x; ` ` ` `cout << c << c << ` `"\n"` `; ` ` ` `} ` ` ` `} ` `} ` ` ` `// Driver Code ` `int` `main() ` `{ ` ` ` `string str = ` `"dbdeke"` `; ` ` ` `int` `Q[][2] = { { 0, 5 }, ` ` ` `{ 1, 5 }, ` ` ` `{ 1, 3 } }; ` ` ` `int` `n = str.size(); ` ` ` `int` `l = ` `sizeof` `(Q) / ` `sizeof` `(Q[0]); ` ` ` ` ` `// Function calls ` ` ` `precompute(str, n); ` ` ` ` ` `palindromicSubsequences(Q, l); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java program to find lexicographically smallest ` `// palindromic subsequence of even length ` `import` `java.util.*; ` ` ` `class` `GFG{ ` `static` `int` `N = ` `100001` `; ` ` ` `// Frequency array for each character ` `static` `int` `[][]f = ` `new` `int` `[` `26` `][N]; ` ` ` `// Preprocess the frequency array calculation ` `static` `void` `precompute(String s, ` `int` `n) ` `{ ` ` ` `// Frequency array to track each character ` ` ` `// in position 'i' ` ` ` `for` `(` `int` `i = ` `0` `; i < n; i++) ` ` ` `{ ` ` ` `f[s.charAt(i) - ` `'a'` `][i]++; ` ` ` `} ` ` ` ` ` `// Calculating prefix sum ` ` ` `// over this frequency array ` ` ` `// to get frequency of a character ` ` ` `// in a range [L, R]. ` ` ` `for` `(` `int` `i = ` `0` `; i < ` `26` `; i++) ` ` ` `{ ` ` ` `for` `(` `int` `j = ` `1` `; j < n; j++) ` ` ` `{ ` ` ` `f[i][j] += f[i][j - ` `1` `]; ` ` ` `} ` ` ` `} ` `} ` ` ` `// Util function for palindromic subsequences ` `static` `int` `palindromicSubsequencesUtil(` `int` `L, ` `int` `R) ` `{ ` ` ` `int` `c = ` `0` `, ok = ` `0` `; ` ` ` ` ` `// Find frequency of all characters ` ` ` `for` `(` `int` `i = ` `0` `; i < ` `26` `; i++) ` ` ` `{ ` ` ` ` ` `// For each character ` ` ` `// find it's frequency ` ` ` `// in range [L, R] ` ` ` `int` `cnt = f[i][R]; ` ` ` `if` `(L > ` `0` `) ` ` ` `cnt -= f[i][L - ` `1` `]; ` ` ` ` ` `if` `(cnt > ` `1` `) ` ` ` `{ ` ` ` ` ` `// If frequency in this range is > 1, ` ` ` `// then we must take this character, ` ` ` `// as it will give ` ` ` `// lexicographically smallest one ` ` ` `ok = ` `1` `; ` ` ` `c = i; ` ` ` `break` `; ` ` ` `} ` ` ` `} ` ` ` ` ` `// There is no character ` ` ` `// in range [L, R] such ` ` ` `// that it's frequency is > 1. ` ` ` `if` `(ok == ` `0` `) ` ` ` `{ ` ` ` `return` `-` `1` `; ` ` ` `} ` ` ` ` ` `// Return the character's value ` ` ` `return` `c; ` `} ` ` ` `// Function to find lexicographically smallest ` `// palindromic subsequence of even length ` `static` `void` `palindromicSubsequences(` `int` `Q[][], ` `int` `l) ` `{ ` ` ` `for` `(` `int` `i = ` `0` `; i < l; i++) ` ` ` `{ ` ` ` ` ` `// Find in the palindromic subsequences ` ` ` `int` `x = palindromicSubsequencesUtil( ` ` ` `Q[i][` `0` `], Q[i][` `1` `]); ` ` ` ` ` `// No such subsequence exists ` ` ` `if` `(x == -` `1` `) ` ` ` `{ ` ` ` `System.out.print(-` `1` `+ ` `"\n"` `); ` ` ` `} ` ` ` `else` ` ` `{ ` ` ` `char` `c = (` `char` `) (` `'a'` `+ x); ` ` ` `System.out.print((` `char` `) c + ` `""` `+ ` ` ` `(` `char` `) c + ` `"\n"` `); ` ` ` `} ` ` ` `} ` `} ` ` ` `// Driver Code ` `public` `static` `void` `main(String[] args) ` `{ ` ` ` `String str = ` `"dbdeke"` `; ` ` ` `int` `Q[][] = { { ` `0` `, ` `5` `}, ` ` ` `{ ` `1` `, ` `5` `}, ` ` ` `{ ` `1` `, ` `3` `} }; ` ` ` `int` `n = str.length(); ` ` ` `int` `l = Q.length; ` ` ` ` ` `// Function calls ` ` ` `precompute(str, n); ` ` ` ` ` `palindromicSubsequences(Q, l); ` `} ` `} ` ` ` `// This code is contributed by PrinciRaj1992 ` |

*chevron_right*

*filter_none*

**Output:**

dd ee -1

**Time Complexity:** O(26 * N + 26 * Q), where N is the length of string

Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.

## Recommended Posts:

- Length of longest Palindromic Subsequence of even length with no two adjacent characters same
- Check if an array has some palindromic subsequence of length at least 3
- Length of Smallest Subsequence such that sum of elements is greater than equal to K
- Longest Palindromic Subsequence | DP-12
- Count All Palindromic Subsequence in a given String
- Print Longest Palindromic Subsequence
- Longest Palindromic Subsequence of two distinct characters
- Find the lexicographically largest palindromic Subsequence of a String
- Number of palindromic subsequences of length k where k <= 3
- Check if all the palindromic sub-strings are of odd length
- Make the string lexicographically smallest and non palindromic by replacing exactly one character
- Count all Prime Length Palindromic Substrings
- Count of non-palindromic strings of length M using given N characters
- Length of longest palindromic sub-string : Recursion
- Number of strings of length N with no palindromic sub string
- Count of Palindromic substrings in an Index range
- Check if a string can be split into even length palindromic substrings
- Maximum length palindromic substring such that it starts and ends with given char
- Repeated subsequence of length 2 or more
- Maximum Sum Subsequence of length k

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.