给定大小为N的数组arr [] ,任务是检查给定数组中的任何子数组是否可以通过将其少于一半的元素(即floor [length / 2])替换为该元素的任何其他元素而成为回文子数组。
例子:
Input: arr[] = {2, 7, 4, 6, 7, 8}
Output: Yes
Explanation: Among all subarrays of this array, subarray {7, 4, 6, 7} requires only 1 operation to make it a palindrome i.e. replace arr[3] by 4 or arr[4] by 6, which is less than floor(4/2) ( = 2).
Input: arr[] = {3, 7, 19, 6}
Output: No
天真的方法:解决此问题的最简单方法是生成给定数组的所有子数组,对于每个子数组,检查使该子数组成为回文所需的替换次数是否小于floor(subarray的长度/ 2) 。
时间复杂度: O(N 3 )
辅助空间: O(1)
高效的方法:可以基于以下观察来优化上述方法:
If the array arr[] contains duplicate elements, then it is always possible to choose a subarray from initial occurrence to the next occurrence of that element. This subarray will require less than floor(length/2) operations as first and last element of subarray is already equal.
请按照以下步骤解决问题:
- 初始化一个Map,用于存储每个数组元素的频率。
- 遍历所有数组元素并计数每个数组元素的频率,然后将其插入到Map中。
- 检查是否发现任何数组元素的频率大于1。如果发现为真,则打印“是” 。否则,打印“否” 。
以下是此方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// A Utility Function to check if a subarray
// can be palindromic by replacing less than
// half of the elements present in it
bool isConsistingSubarrayUtil(int arr[], int n)
{
// Stores frequency of array elements
map mp;
// Traverse the array
for (int i = 0; i < n; ++i) {
// Update frequency of
// each array element
mp[arr[i]]++;
}
// Iterator over the Map
map::iterator it;
for (it = mp.begin(); it != mp.end(); ++it) {
// If frequency of any element exceeds 1
if (it->second > 1) {
return true;
}
}
// If no repetition is found
return false;
}
// Function to check and print if any subarray
// can be made palindromic by replacing less
// than half of its elements
void isConsistingSubarray(int arr[], int N)
{
if (isConsistingSubarrayUtil(arr, N)) {
cout << "Yes" << endl;
}
else {
cout << "No" << endl;
}
}
// Driver Code
int main()
{
// Given array arr[]
int arr[] = { 1, 2, 3, 4, 5, 1 };
// Size of array
int N = sizeof(arr) / sizeof(arr[0]);
// Function Call
isConsistingSubarray(arr, N);
return 0;
}
Java
// Java program for the above approach
import java.util.*;
class GFG{
// A Utility Function to check if a subarray
// can be palindromic by replacing less than
// half of the elements present in it
static boolean isConsistingSubarrayUtil(int arr[],
int n)
{
// Stores frequency of array elements
TreeMap mp = new TreeMap();
// Traverse the array
for(int i = 0; i < n; ++i)
{
// Update frequency of
// each array element
mp.put(arr[i],
mp.getOrDefault(arr[i], 0) + 1);
}
for(Map.Entry it : mp.entrySet())
{
// If frequency of any element exceeds 1
if (it.getValue() > 1)
{
return true;
}
}
// If no repetition is found
return false;
}
// Function to check and print if any subarray
// can be made palindromic by replacing less
// than half of its elements
static void isConsistingSubarray(int arr[], int N)
{
if (isConsistingSubarrayUtil(arr, N))
{
System.out.println("Yes");
}
else
{
System.out.println("No");
}
}
// Driver Code
public static void main(String args[])
{
// Given array arr[]
int arr[] = { 1, 2, 3, 4, 5, 1 };
// Size of array
int N = arr.length;
// Function Call
isConsistingSubarray(arr, N);
}
}
// This code is contributed by susmitakundugoaldanga
Python3
# Python3 program for the above approach
# A Utility Function to check if a subarray
# can be palindromic by replacing less than
# half of the elements present in it
def isConsistingSubarrayUtil(arr, n) :
# Stores frequency of array elements
mp = {};
# Traverse the array
for i in range(n) :
# Update frequency of
# each array element
if arr[i] in mp :
mp[arr[i]] += 1;
else :
mp[arr[i]] = 1;
# Iterator over the Map
for it in mp :
# If frequency of any element exceeds 1
if (mp[it] > 1) :
return True;
# If no repetition is found
return False;
# Function to check and print if any subarray
# can be made palindromic by replacing less
# than half of its elements
def isConsistingSubarray(arr, N) :
if (isConsistingSubarrayUtil(arr, N)) :
print("Yes");
else :
print("No");
# Driver Code
if __name__ == "__main__" :
# Given array arr[]
arr = [ 1, 2, 3, 4, 5, 1 ];
# Size of array
N = len(arr);
# Function Call
isConsistingSubarray(arr, N);
# This code is contributed by AnkThon
C#
// C# program for the above approach
using System;
using System.Collections.Generic;
using System.Linq;
class GFG{
// A Utility Function to check if a subarray
// can be palindromic by replacing less than
// half of the elements present in it
static bool isConsistingSubarrayUtil(int[] arr,
int n)
{
// Stores frequency of array elements
Dictionary mp = new Dictionary();
// Traverse the array
for(int i = 0; i < n; ++i)
{
// Update frequency of
// each array element
if (mp.ContainsKey(arr[i]) == true)
mp[arr[i]] += 1;
else
mp[arr[i]] = 1;
}
var val = mp.Keys.ToList();
foreach(var key in val)
{
// If frequency of any element exceeds 1
if (mp[key] > 1)
{
return true;
}
}
// If no repetition is found
return false;
}
// Function to check and print if any subarray
// can be made palindromic by replacing less
// than half of its elements
static void isConsistingSubarray(int[] arr, int N)
{
if (isConsistingSubarrayUtil(arr, N))
{
Console.Write("Yes");
}
else
{
Console.Write("No");
}
}
// Driver Code
public static void Main()
{
// Given array arr[]
int[] arr = { 1, 2, 3, 4, 5, 1 };
// Size of array
int N = arr.Length;
// Function Call
isConsistingSubarray(arr, N);
}
}
// This code is contributed by sanjoy62
Yes
时间复杂度: O(N)
辅助空间: O(N)