Subarray of length K whose concatenation forms a palindrome
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++
#include <bits/stdc++.h>
using namespace std;
bool palindrome(vector< int > a, int i, int j)
{
while (i<j)
{
if (a[i] != a[j])
return false ;
i++;
j--;
}
return true ;
}
int findSubArray(vector< int > arr, int k)
{
int n= sizeof (arr)/ sizeof (arr[0]);
for ( int i=0; i<=n-k; i++){
if (palindrome(arr, i, i+k-1))
return i;
}
return -1;
}
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;
}
|
Java
import java.io.*;
class GFG
{
public static boolean palindrome( int [] a, int i, int j)
{
while (i<j)
{
if (a[i] != a[j])
return false ;
i++;
j--;
}
return true ;
}
static int findSubArray( int []arr, int k)
{
int n= arr.length;
for ( int i= 0 ; i<=n-k; i++){
if (palindrome(arr, i, i+k- 1 ))
return i;
}
return - 1 ;
}
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" );
}
}
}
|
Python3
def palindrome(a, i, j):
while (i < j):
if (a[i] ! = a[j]):
return False
i + = 1
j - = 1
return True
def findSubArray(arr, k):
n = len (arr)
for i in range (n - k + 1 ):
if (palindrome(arr, i, i + k - 1 )):
return i
return - 1
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 = " " )
|
C#
using System;
class GFG{
public static bool palindrome( int [] a, int i,
int j)
{
while (i < j)
{
if (a[i] != a[j])
return false ;
i++;
j--;
}
return true ;
}
static int findSubArray( int [] arr, int k)
{
int n = arr.Length;
for ( int i = 0; i <= n - k; i++)
{
if (palindrome(arr, i, i + k - 1))
return i;
}
return -1;
}
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" );
}
}
}
|
Javascript
<script>
function palindrome(a, i, j)
{
while (i<j)
{
if (a[i] != a[j])
return false ;
i++;
j--;
}
return true ;
}
function findSubArray(arr, k)
{
let n= arr.length;
for (let i=0; i<=n-k; i++){
if (palindrome(arr, i, i+k-1))
return i;
}
return -1;
}
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)
Last Updated :
01 Oct, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...