Given an array arr[] consisting of N integers, the task is to find the array K[] of minimum possible length such that after performing multiple mirror operations on K[], the given array arr[] can be obtained.
Mirror Operation: Appending all the array elements to the original array in reverse order.
Illustration:
arr[] = {1, 2, 3}
After 1st mirror operation, arr[] modifies to {1, 2, 3, 3, 2, 1}
After 2nd mirror operation, arr[] modifies to {1, 2, 3, 3, 2, 1, 1, 2, 3, 3, 2, 1}
Examples:
Input: N = 6, arr[] = { 1, 2, 3, 3, 2, 1 }
Output: 3
Explanation:
Subarrays {1, 2, 3} and {3, 2, 1} are mirror images of each other.
Single mirror operation on {1, 2, 3} obtains the given array.
Therefore, the minimum number of elements required is 3.
Input: N = 8, arr[] = { 1, 2, 2, 1, 1, 2, 2, 1 }
Output: 2
Explanation:
Subarrays {1, 2, 2, 1} and {1, 2, 2, 1} are mirror images of each other.
Subarray {1, 2} and {2, 1} are mirror images of each other.
{1, 2} -> {1, 2, 2, 1} -> {1, 2, 2, 1, 1, 2, 2, 1}
Therefore, the minimum number of elements required is 2.
Naive Approach:
The simplest approach to solve the problem is to generate all the possible subarrays from the given array of size less than equal to N/2 and, for each subarray, check if performing mirror operation gives the array arr[] or not. Print the minimum length subarray satisfying the condition. If no subarray is found to be satisfying, print No.
Time Complexity: O(N3)
Auxiliary Space: O(N)
Efficient Approach:
The above approach can be further optimized using Divide and Conquer technique. Follow the steps below to solve the problem:
- Initialize a variable K = N and then, check whether the prefix of A[] of length K is a palindrome or not.
- If the prefix of length K is a palindrome then divide K by 2 and perform the above checking.
- If the prefix is not a palindrome then the answer is the current value of K.
- Keep checking while K > 0 until K is odd.
- If K is odd, then print the current value of K.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int minimumrequired( int A[], int N)
{
int K = N;
int ans;
while (K > 0) {
if (K % 2 == 1) {
ans = K;
break ;
}
bool ispalindrome = 1;
for ( int i = 0; i < K / 2; i++) {
if (A[i] != A[K - 1 - i])
ispalindrome = 0;
}
if (ispalindrome) {
ans = K / 2;
K /= 2;
}
else {
ans = K;
break ;
}
}
return ans;
}
int main()
{
int a[] = { 1, 2, 2, 1, 1, 2, 2, 1 };
int N = sizeof a / sizeof a[0];
cout << minimumrequired(a, N);
return 0;
}
|
Java
class GFG{
static int minimumrequired( int A[], int N)
{
int K = N;
int ans= 0 ;
while (K > 0 )
{
if (K % 2 == 1 )
{
ans = K;
break ;
}
int ispalindrome = 1 ;
for ( int i = 0 ; i < K / 2 ; i++)
{
if (A[i] != A[K - 1 - i])
ispalindrome = 0 ;
}
if (ispalindrome == 1 )
{
ans = K / 2 ;
K /= 2 ;
}
else
{
ans = K;
break ;
}
}
return ans;
}
public static void main(String[] args)
{
int a[] = { 1 , 2 , 2 , 1 , 1 , 2 , 2 , 1 };
int N = a.length;
System.out.println(minimumrequired(a, N));
}
}
|
Python3
def minimumrequired(A, N):
K = N
while (K > 0 ):
if (K % 2 ) = = 1 :
ans = K
break
ispalindrome = 1
for i in range ( 0 , K / / 2 ):
if (A[i] ! = A[K - 1 - i]):
ispalindrome = 0
if (ispalindrome = = 1 ):
ans = K / / 2
K = K / / 2
else :
ans = K
break
return ans
A = [ 1 , 2 , 2 , 1 , 1 , 2 , 2 , 1 ]
N = len (A)
print (minimumrequired(A, N))
|
C#
using System;
class GFG{
static int minimumrequired( int [] A, int N)
{
int K = N;
int ans = 0;
while (K > 0)
{
if (K % 2 == 1)
{
ans = K;
break ;
}
int ispalindrome = 1;
for ( int i = 0; i < K / 2; i++)
{
if (A[i] != A[K - 1 - i])
ispalindrome = 0;
}
if (ispalindrome == 1)
{
ans = K / 2;
K /= 2;
}
else
{
ans = K;
break ;
}
}
return ans;
}
public static void Main(String[] args)
{
int [] a = { 1, 2, 2, 1, 1, 2, 2, 1 };
int N = a.Length;
Console.WriteLine(minimumrequired(a, N));
}
}
|
Javascript
<script>
function minimumrequired(A, N)
{
let K = N;
let ans;
while (K > 0) {
if (K % 2 == 1) {
ans = K;
break ;
}
let ispalindrome = true ;
for (let i = 0; i < parseInt(K / 2, 10); i++) {
if (A[i] != A[K - 1 - i])
ispalindrome = false ;
}
if (ispalindrome) {
ans = parseInt(K / 2, 10);
K = parseInt(K / 2, 10);
}
else {
ans = K;
break ;
}
}
return ans;
}
let a = [ 1, 2, 2, 1, 1, 2, 2, 1 ];
let N = a.length;
document.write(minimumrequired(a, N));
</script>
|
Time Complexity: O(N*log N)
Auxiliary Space: O(N)
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!
Last Updated :
01 Apr, 2021
Like Article
Save Article