给定一个数组arr [] ,该数组包含范围[ 0,9]中的N个整数,任务是找到一个长度为K的子数组,我们可以从中生成一个回文数。如果不存在这样的子数组,则打印-1 。
注意:数组中的元素在0到10的范围内。
例子:
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
天真的方法:解决该问题的最简单方法是生成所有长度为K的子数组,并为每个子数组连接该子数组中的所有元素,并检查形成的数字是否为回文数。
时间复杂度: O(N 3 )
辅助空间: O(K)
高效的方法:使用窗口滑动技术可以解决该问题。请按照以下步骤解决问题:
- 生成并存储通过将数组的前K个元素串联而形成的数字,并存储在变量palin_num中。
- 检查它是否是回文。如果发现是真的,则打印palin_num作为答案。
- 否则,迭代数组,并为每个剩余元素追加当前数组元素,并删除palin_num当前值的第一个元素。现在,检查它是否是回文数。
- 如果无法生成这样的数字(回文),请打印-1。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to check if a number
// is Palindrome or not
bool checkPalindrome(int n)
{
int t = n, palin_num = 0;
// Reversing a number
while (t > 0) {
// Append the last digit
// of the number
palin_num
= palin_num * 10
+ t % 10;
// Remove the last digit
t /= 10;
}
// If the number
// is a palindrome
if (palin_num == n) {
return true;
}
// Otherwise
return false;
}
// Function to find a subarray whose
// concatenation forms a palindrome
// and return its starting index
int findSubArray(vector arr, int k)
{
int i, num = 0;
// Concatenate first k elements
for (i = 0; i < k; i++) {
num = num * 10 + arr[i];
}
// Check if the first K-length
// subarray forms a palindrome
if (checkPalindrome(num)) {
return 0;
}
// Traverse the array
for (int j = i; j < arr.size(); j++) {
// Append the last element of current
// K-length subarray and remove the
// first element of previous subarray
num = (num % (int)pow(
10, k - 1))
* 10
+ arr[j];
// Check if the conctenation
// forms a palindrome
if (checkPalindrome(num)) {
return j - k + 1;
}
}
return -1;
}
// Driver Code
int main()
{
vector 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
// Java program for the above approach
import java.util.*;
class GFG{
// Function to check if a number
// is Palindrome or not
static boolean checkPalindrome(int n)
{
int t = n, palin_num = 0;
// Reversing a number
while (t > 0)
{
// Append the last digit
// of the number
palin_num = palin_num * 10 +
t % 10;
// Remove the last digit
t /= 10;
}
// If the number
// is a palindrome
if (palin_num == n)
{
return true;
}
// Otherwise
return false;
}
// Function to find a subarray whose
// concatenation forms a palindrome
// and return its starting index
static int findSubArray(int []arr, int k)
{
int i, num = 0;
// Concatenate first k elements
for(i = 0; i < k; i++)
{
num = num * 10 + arr[i];
}
// Check if the first K-length
// subarray forms a palindrome
if (checkPalindrome(num))
{
return 0;
}
// Traverse the array
for(int j = i; j < arr.length; j++)
{
// Append the last element of current
// K-length subarray and remove the
// first element of previous subarray
num = (num % (int)Math.pow(
10, k - 1)) * 10 + arr[j];
// Check if the conctenation
// forms a palindrome
if (checkPalindrome(num))
{
return j - k + 1;
}
}
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 Amit Katiyar
Python3
# Python3 program for the above approach
# Function to check if a number
# is Palindrome or not
def checkPalindrome(n):
t = n
palin_num = 0
# Reversing a number
while (t > 0):
# Append the last digit
# of the number
palin_num = palin_num * 10 + t % 10
# Remove the last digit
t //= 10
# If the number
# is a palindrome
if (palin_num == n):
return True
# Otherwise
return False
# Function to find a subarray whose
# concatenation forms a palindrome
# and return its starting index
def findSubArray(arr, k):
num = 0
# Concatenate first k elements
i = 0
while i < k:
num = num * 10 + arr[i]
i += 1
# Check if the first K-length
# subarray forms a palindrome
if (checkPalindrome(num)):
return 0
# Traverse the array
for j in range(i, len(arr)):
# Append the last element of current
# K-length subarray and remove the
# first element of previous subarray
num = (num % pow(10, k - 1)) * 10 + arr[j]
# Check if the conctenation
# forms a palindrome
if (checkPalindrome(num)):
return j - k + 1
return -1
# Driver Code
if __name__ == '__main__':
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 mohit kumar 29
C#
// C# program for the
// above approach
using System;
class GFG{
// Function to check if a number
// is Palindrome or not
static bool checkPalindrome(int n)
{
int t = n, palin_num = 0;
// Reversing a number
while (t > 0)
{
// Append the last digit
// of the number
palin_num = palin_num * 10 +
t % 10;
// Remove the last digit
t /= 10;
}
// If the number
// is a palindrome
if (palin_num == n)
{
return true;
}
// Otherwise
return false;
}
// Function to find a subarray whose
// concatenation forms a palindrome
// and return its starting index
static int findSubArray(int[] arr,
int k)
{
int i, num = 0;
// Concatenate first k elements
for (i = 0; i < k; i++)
{
num = num * 10 + arr[i];
}
// Check if the first K-length
// subarray forms a palindrome
if (checkPalindrome(num))
{
return 0;
}
// Traverse the array
for (int j = i; j < arr.Length; j++)
{
// Append the last element of current
// K-length subarray and remove the
// first element of previous subarray
num = (num % (int)Math.Pow(10,
k - 1)) * 10 + arr[j];
// Check if the conctenation
// forms a palindrome
if (checkPalindrome(num))
{
return j - k + 1;
}
}
return -1;
}
// Driver Code
public static void Main()
{
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 Chitranayal
输出
-1
时间复杂度: O(N 2 )
辅助空间: O(1)