Subarray of length K whose concatenation forms a palindrome
Last Updated :
01 Oct, 2021
Given an array arr[], consisting of N integers in the range [0, 9], the task is to find a subarray of length K from which we can generate a number which is a Palindrome Number. If no such subarray exists, print -1.
Note: The elements in the array are in the range of 0 to 10.
Examples:
Input: arr[] = {1, 5, 3, 2, 3, 5, 4}, K = 5
Output: 5, 3, 2, 3, 5
Explanation:
Number generated by concatenating all elements of the subarray, i.e. 53235, is a palindrome.
Input: arr[] = {2, 3, 5, 1, 3}, K = 4
Output: -1
Naive Approach: The simplest approach to solve the problem is to generate all subarrays of length K and for each subarray, concatenate all the elements from the subarray and check if the number formed is a Palindrome Number or not.
Time Complexity: O(N3)
Auxiliary Space: O(K)
Efficient Approach: The problem can be solved using the Window-Sliding technique. Follow the steps below to solve the problem:
- Make a palindrome function to check if the given subarray (Window-Sliding) is palindrome or not.
- Iterate over the array, and for each subarray call the palindrome function.
- If found to be true, return the starting index of that subarray, and print the array from starting index to the next k index.
- If no such subarray found which is a palindrome, print -1.
Below is the implementation of the above approach:
C++
// C++ program for the above approach
#include <bits/stdc++.h>
using namespace std;
// Function to check if a number
// is Palindrome or not
// here i is the starting index
// and j is the last index of the subarray
bool palindrome(vector<int> a, int i, int j)
{
while(i<j)
{
// If the integer at i is not equal to j
// then the subarray is not palindrome
if(a[i] != a[j])
return false;
// Otherwise
i++;
j--;
}
// all a[i] is equal to a[j]
// then the subarray is palindrome
return true;
}
// Function to find a subarray whose
// concatenation forms a palindrome
// and return its starting index
int findSubArray(vector<int> arr, int k)
{
int n= sizeof(arr)/sizeof(arr[0]);
// Iterating over subarray of length k
// and checking if that subarray is palindrome
for(int i=0; i<=n-k; i++){
if(palindrome(arr, i, i+k-1))
return i;
}
// If no subarray is palindrome
return -1;
}
// Driver Code
int main()
{
vector<int> arr = { 2, 3, 5, 1, 3 };
int k = 4;
int ans = findSubArray(arr, k);
if (ans == -1)
cout << -1 << "\n";
else {
for (int i = ans; i < ans + k;
i++)
cout << arr[i] << " ";
cout << "\n";
}
return 0;
}
// This code is contributed by Prafulla Shekhar
Java
// Java program for the above approach
import java.io.*;
class GFG
{
// Function to check if a number
// is Palindrome or not
// here i is the starting index
// and j is the last index of the subarray
public static boolean palindrome(int[] a, int i, int j)
{
while(i<j)
{
// If the integer at i is not equal to j
// then the subarray is not palindrome
if(a[i] != a[j])
return false;
// Otherwise
i++;
j--;
}
// all a[i] is equal to a[j]
// then the subarray is palindrome
return true;
}
// Function to find a subarray whose
// concatenation forms a palindrome
// and return its starting index
static int findSubArray(int []arr, int k)
{
int n= arr.length;
// Iterating over subarray of length k
// and checking if that subarray is palindrome
for(int i=0; i<=n-k; i++){
if(palindrome(arr, i, i+k-1))
return i;
}
// If no subarray is palindrome
return -1;
}
// Driver code
public static void main (String[] args)
{
int []arr = { 2, 3, 5, 1, 3 };
int k = 4;
int ans = findSubArray(arr, k);
if (ans == -1)
System.out.print(-1 + "\n");
else
{
for(int i = ans; i < ans + k; i++)
System.out.print(arr[i] + " ");
System.out.print("\n");
}
}
}
// This code is contributed by Prafulla Shekhar
Python3
# Python3 program for the above approach
# Function to check if a number
# is Palindrome or not here i is
# the starting index and j is the
# last index of the subarray
def palindrome(a, i, j):
while(i < j):
# If the integer at i is not equal to j
# then the subarray is not palindrome
if (a[i] != a[j]):
return False
# Otherwise
i += 1
j -= 1
# all a[i] is equal to a[j]
# then the subarray is palindrome
return True
# Function to find a subarray whose
# concatenation forms a palindrome
# and return its starting index
def findSubArray(arr, k):
n = len(arr)
# Iterating over subarray of length k
# and checking if that subarray is palindrome
for i in range(n - k + 1):
if (palindrome(arr, i, i + k - 1)):
return i
return -1
# Driver code
arr = [ 2, 3, 5, 1, 3 ]
k = 4
ans = findSubArray(arr, k)
if (ans == -1):
print(-1)
else:
for i in range(ans,ans + k):
print(arr[i], end = " ")
# This code is contributed by avanitrachhadiya2155
C#
// C# program for the above approach
using System;
class GFG{
// Function to check if a number
// is Palindrome or not here i is
// the starting index and j is the
// last index of the subarray
public static bool palindrome(int[] a, int i,
int j)
{
while (i < j)
{
// If the integer at i is not equal to j
// then the subarray is not palindrome
if (a[i] != a[j])
return false;
// Otherwise
i++;
j--;
}
// All a[i] is equal to a[j]
// then the subarray is palindrome
return true;
}
// Function to find a subarray whose
// concatenation forms a palindrome
// and return its starting index
static int findSubArray(int[] arr, int k)
{
int n = arr.Length;
// Iterating over subarray of length k
// and checking if that subarray is palindrome
for(int i = 0; i <= n - k; i++)
{
if (palindrome(arr, i, i + k - 1))
return i;
}
// If no subarray is palindrome
return -1;
}
// Driver code
public static void Main(String[] args)
{
int[] arr = { 2, 3, 5, 1, 3 };
int k = 4;
int ans = findSubArray(arr, k);
if (ans == -1)
Console.Write(-1 + "\n");
else
{
for(int i = ans; i < ans + k; i++)
Console.Write(arr[i] + " ");
Console.Write("\n");
}
}
}
// This code is contributed by aashish1995
JavaScript
<script>
// Javascript program for
// the above approach
// Function to check if a number
// is Palindrome or not
// here i is the starting index
// and j is the last index of the subarray
function palindrome(a, i, j)
{
while(i<j)
{
// If the integer at i is not equal to j
// then the subarray is not palindrome
if(a[i] != a[j])
return false;
// Otherwise
i++;
j--;
}
// all a[i] is equal to a[j]
// then the subarray is palindrome
return true;
}
// Function to find a subarray whose
// concatenation forms a palindrome
// and return its starting index
function findSubArray(arr, k)
{
let n= arr.length;
// Iterating over subarray of length k
// and checking if that subarray is palindrome
for(let i=0; i<=n-k; i++){
if(palindrome(arr, i, i+k-1))
return i;
}
// If no subarray is palindrome
return -1;
}
// Driver Code
let arr = [ 2, 3, 5, 1, 3 ];
let k = 4;
let ans = findSubArray(arr, k);
if (ans == -1)
document.write(-1 + "\n");
else
{
for(let i = ans; i < ans + k; i++)
document.write(arr[i] + " ");
document.write("<br/>");
}
</script>
Time Complexity: O(N)
Auxiliary Space: O(1)
Similar Reads
Count pair of strings whose concatenation of substrings form a palindrome Given an array of strings arr[], the task is to count the pair of strings whose concatenation of substrings form a palindrome.Examples: Input: arr[] = {"gfg", "gfg"} Output: 1 Explanation: One possible way of choosing s1 and s2 is s1 = "gf", s2 = "g" such that s1 + s2 i.e "gfg" is a palindrome.Input
5 min read
Count of pairs of strings whose concatenation forms a palindromic string Given an array A[ ] consisting of N strings, the task is to count the number of pairs of possible strings that on merging forms a Palindromic String or can be rearranged to form a Palindromic String. Example : Input: N = 6, A[ ] = {aab, abcac, dffe, ed, aa, aade}Output: 6Explanation: All possible pa
9 min read
Check if sum of any subarray is Palindrome or not Given an array arr[] of size N. the task is to check whether there exists any subarray of size atleast 2 such that its sum is palindrome. If such a subarray exists, then print YES. Otherwise, print NO.Examples: Input: arr[] = {10, 6, 7, 9, 12} Output: Yes Explanation: The subarray [6, 7, 9] with sum
8 min read
Longest palindrome formed by concatenating and reordering strings of equal length Given an array arr[] consisting of N strings of equal length M, the task is to create the longest palindrome by concatenating the strings. Reordering and discarding some strings from the given set of strings can also be done.Examples: Input: N = 3, arr[] = { "tab", "one", "bat" }, M = 3 Output: tabb
9 min read
Palindrome check for concatenation of Prefixes and Suffixes Given an integer N and an array of strings arr[], the task is to check whether the concatenation of prefixes and suffixes of all strings forms a palindrome or not. Examples: Input: N = 4, arr[] = {"bcd", "cd", "a", "d", "abc", "ab"}Output: NoExplanation: "a", "ab" and "abc" are prefixes. "d", "cd" a
8 min read
Count of three non-overlapping sub-strings which on concatenation forms a palindrome Given a string str, the task is to count the number of ways a palindromic substring could be formed by the concatenation of three sub-strings x, y and z of the string str such that all of them are non-overlapping i.e. sub-string y occurs after substring x and sub-string z occurs after sub-string y.E
7 min read
Subarray of length K having concatenation of its elements divisible by X Given an array arr[] consisting of N positive integers, the task is to find a subarray of length K such that concatenation of each element of the subarray is divisible by X. If no such subarray exists, then print "-1". If more than one such subarray exists, print any one of them. Examples: Input: ar
9 min read
Longest palindromic string formed by concatenation of prefix and suffix of a string Given string str, the task is to find the longest palindromic substring formed by the concatenation of the prefix and suffix of the given string str. Examples: Input: str = "rombobinnimor" Output: rominnimor Explanation: The concatenation of string "rombob"(prefix) and "mor"(suffix) is "rombobmor" w
10 min read
Longest substring whose characters can be rearranged to form a Palindrome Given a string S of length N which only contains lowercase alphabets. Find the length of the longest substring of S such that the characters in it can be rearranged to form a palindrome. Examples:Input: S = âaabeâOutput: 3Explanation:The substring âaabâ can be rearranged to form "aba", which is a pa
15+ min read
Longest Palindrome in a String formed by concatenating its prefix and suffix Given a string str consisting of lowercase English letters, the task is to find the longest palindromic string T which satisfies the following condition: T = p + m + s where p and s are the prefix and the suffix of the given string str respectively and the string m is either the prefix or suffix of
13 min read