Check if any subarray can be made palindromic by replacing less than half of its elements
Last Updated :
28 Dec, 2022
Given an array arr[] of size N, the task is to check if any subarray from the given array can be made a palindrome by replacing less than half of its elements (i.e. floor[length/2]) by any other element of the subarray.
Examples:
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
Naive Approach: The simplest approach to solve this problem is to generate all subarrays of the given array and for each subarray, check if the number of replacements required to make that subarray a palindrome is less than floor(length of subarray / 2).
Time Complexity: O(N3)
Auxiliary Space: O(1)
Efficient Approach: The above approach can be optimized based on the following observations:
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.
Follow the steps below to solve the problem:
Below is the implementation of this approach:
C++
#include <bits/stdc++.h>
using namespace std;
bool isConsistingSubarrayUtil( int arr[], int n)
{
map< int , int > mp;
for ( int i = 0; i < n; ++i) {
mp[arr[i]]++;
}
map< int , int >::iterator it;
for (it = mp.begin(); it != mp.end(); ++it) {
if (it->second > 1) {
return true ;
}
}
return false ;
}
void isConsistingSubarray( int arr[], int N)
{
if (isConsistingSubarrayUtil(arr, N)) {
cout << "Yes" << endl;
}
else {
cout << "No" << endl;
}
}
int main()
{
int arr[] = { 1, 2, 3, 4, 5, 1 };
int N = sizeof (arr) / sizeof (arr[0]);
isConsistingSubarray(arr, N);
return 0;
}
|
Java
import java.util.*;
class GFG{
static boolean isConsistingSubarrayUtil( int arr[],
int n)
{
TreeMap<Integer,
Integer> mp = new TreeMap<Integer,
Integer>();
for ( int i = 0 ; i < n; ++i)
{
mp.put(arr[i],
mp.getOrDefault(arr[i], 0 ) + 1 );
}
for (Map.Entry<Integer,
Integer> it : mp.entrySet())
{
if (it.getValue() > 1 )
{
return true ;
}
}
return false ;
}
static void isConsistingSubarray( int arr[], int N)
{
if (isConsistingSubarrayUtil(arr, N))
{
System.out.println( "Yes" );
}
else
{
System.out.println( "No" );
}
}
public static void main(String args[])
{
int arr[] = { 1 , 2 , 3 , 4 , 5 , 1 };
int N = arr.length;
isConsistingSubarray(arr, N);
}
}
|
Python3
def isConsistingSubarrayUtil(arr, n) :
mp = {};
for i in range (n) :
if arr[i] in mp :
mp[arr[i]] + = 1 ;
else :
mp[arr[i]] = 1 ;
for it in mp :
if (mp[it] > 1 ) :
return True ;
return False ;
def isConsistingSubarray(arr, N) :
if (isConsistingSubarrayUtil(arr, N)) :
print ( "Yes" );
else :
print ( "No" );
if __name__ = = "__main__" :
arr = [ 1 , 2 , 3 , 4 , 5 , 1 ];
N = len (arr);
isConsistingSubarray(arr, N);
|
C#
using System;
using System.Collections.Generic;
using System.Linq;
class GFG{
static bool isConsistingSubarrayUtil( int [] arr,
int n)
{
Dictionary< int ,
int > mp = new Dictionary< int ,
int >();
for ( int i = 0; i < n; ++i)
{
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 (mp[key] > 1)
{
return true ;
}
}
return false ;
}
static void isConsistingSubarray( int [] arr, int N)
{
if (isConsistingSubarrayUtil(arr, N))
{
Console.Write( "Yes" );
}
else
{
Console.Write( "No" );
}
}
public static void Main()
{
int [] arr = { 1, 2, 3, 4, 5, 1 };
int N = arr.Length;
isConsistingSubarray(arr, N);
}
}
|
Javascript
<script>
function isConsistingSubarrayUtil(arr, n)
{
var mp = new Map();
for ( var i = 0; i < n; ++i) {
if (mp.has(arr[i]))
{
mp.set(arr[i], mp.get(arr[i])+1);
}
else
{
mp.set(arr[i], 1);
}
}
var ans = false ;
mp.forEach((value, key) => {
if (value > 1) {
ans = true ;
}
});
if (ans)
return true ;
return false ;
}
function isConsistingSubarray(arr, N)
{
if (isConsistingSubarrayUtil(arr, N)) {
document.write( "Yes" );
}
else {
document.write( "No" );
}
}
var arr = [1, 2, 3, 4, 5, 1];
var N = arr.length;
isConsistingSubarray(arr, N);
</script>
|
Time Complexity: O(N*log(N))
Auxiliary Space: O(N)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...