Minimum count of elements required to obtain the given Array by repeated mirror operations
Last Updated :
01 Apr, 2021
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++
// C++ Program to implement
// the above approach
#include <bits/stdc++.h>
using namespace std;
// Function to find minimum number
// of elements required to form A[]
// by performing mirroring operation
int minimumrequired(int A[], int N)
{
// Initialize K
int K = N;
int ans;
while (K > 0) {
// Odd length array
// cannot be formed by
// mirror operation
if (K % 2 == 1) {
ans = K;
break;
}
bool ispalindrome = 1;
// Check if prefix of
// length K is palindrome
for (int i = 0; i < K / 2; i++) {
// Check if not a palindrome
if (A[i] != A[K - 1 - i])
ispalindrome = 0;
}
// If found to be palindrome
if (ispalindrome) {
ans = K / 2;
K /= 2;
}
// Otherwise
else {
ans = K;
break;
}
}
// Return the final answer
return ans;
}
// Driver Code
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
// Java Program to implement
// the above approach
class GFG{
// Function to find minimum number
// of elements required to form A[]
// by performing mirroring operation
static int minimumrequired(int A[], int N)
{
// Initialize K
int K = N;
int ans=0;
while (K > 0)
{
// Odd length array
// cannot be formed by
// mirror operation
if (K % 2 == 1)
{
ans = K;
break;
}
int ispalindrome = 1;
// Check if prefix of
// length K is palindrome
for (int i = 0; i < K / 2; i++)
{
// Check if not a palindrome
if (A[i] != A[K - 1 - i])
ispalindrome = 0;
}
// If found to be palindrome
if (ispalindrome == 1)
{
ans = K / 2;
K /= 2;
}
// Otherwise
else
{
ans = K;
break;
}
}
// Return the final answer
return ans;
}
// Driver Code
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));
}
}
// This code is contributed by rock_cool
Python3
# Python3 program to implement
# the above approach
# Function to find minimum number
# of elements required to form A[]
# by performing mirroring operation
def minimumrequired(A, N):
# Initialize K
K = N
while (K > 0):
# Odd length array
# cannot be formed by
# mirror operation
if (K % 2) == 1:
ans = K
break
ispalindrome = 1
# Check if prefix of
# length K is palindrome
for i in range(0, K // 2):
# Check if not a palindrome
if (A[i] != A[K - 1 - i]):
ispalindrome = 0
# If found to be palindrome
if (ispalindrome == 1):
ans = K // 2
K = K // 2
# Otherwise
else:
ans = K
break
# Return the final answer
return ans
# Driver code
A = [ 1, 2, 2, 1, 1, 2, 2, 1 ]
N = len(A)
print(minimumrequired(A, N))
# This code is contributed by VirusBuddah_
C#
// C# program to implement
// the above approach
using System;
class GFG{
// Function to find minimum number
// of elements required to form []A
// by performing mirroring operation
static int minimumrequired(int[] A, int N)
{
// Initialize K
int K = N;
int ans = 0;
while (K > 0)
{
// Odd length array
// cannot be formed by
// mirror operation
if (K % 2 == 1)
{
ans = K;
break;
}
int ispalindrome = 1;
// Check if prefix of
// length K is palindrome
for(int i = 0; i < K / 2; i++)
{
// Check if not a palindrome
if (A[i] != A[K - 1 - i])
ispalindrome = 0;
}
// If found to be palindrome
if (ispalindrome == 1)
{
ans = K / 2;
K /= 2;
}
// Otherwise
else
{
ans = K;
break;
}
}
// Return the readonly answer
return ans;
}
// Driver Code
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));
}
}
// This code is contributed by amal kumar choubey
JavaScript
<script>
// Javascript Program to implement
// the above approach
// Function to find minimum number
// of elements required to form A[]
// by performing mirroring operation
function minimumrequired(A, N)
{
// Initialize K
let K = N;
let ans;
while (K > 0) {
// Odd length array
// cannot be formed by
// mirror operation
if (K % 2 == 1) {
ans = K;
break;
}
let ispalindrome = true;
// Check if prefix of
// length K is palindrome
for (let i = 0; i < parseInt(K / 2, 10); i++) {
// Check if not a palindrome
if (A[i] != A[K - 1 - i])
ispalindrome = false;
}
// If found to be palindrome
if (ispalindrome) {
ans = parseInt(K / 2, 10);
K = parseInt(K / 2, 10);
}
// Otherwise
else {
ans = K;
break;
}
}
// Return the final answer
return ans;
}
let a = [ 1, 2, 2, 1, 1, 2, 2, 1 ];
let N = a.length;
document.write(minimumrequired(a, N));
// This code is contributed by divyeshrabadiya07.
</script>
Time Complexity: O(N*log N)
Auxiliary Space: O(N)
Similar Reads
Minimum number of given operations required to reduce the array to 0 element
Given an array arr[] of N integers. The task is to find the minimum number of given operations required to reduce the array to 0 elements. In a single operation, any element can be chosen from the array and all of its multiples get removed including itself.Examples: Input: arr[] = {2, 4, 6, 3, 4, 6,
6 min read
Minimum number of steps required to obtain the given Array by the given operations
Given an array arr[] of N positive integers, the task is to find the minimum number of operations required of the following types to obtain the array arr[] from an array of zeroes only. Select any index i and increment all the elements at the indices [i, N - 1] by 1.Select any index i and decrease a
12 min read
Minimum number of operations required to make all the elements positive
Given an array A[] of length N. You can apply the below operation: Choose an index i such that (i+1) element exists. After that choose any number X (positive or negative). Then subtract X from the ith element and add it into (i+1)th element.The task is to find the minimum number of operations requir
9 min read
Minimum operations required to modify the array such that parity of adjacent elements is different
Given an array A[], the task is to find the minimum number of operations required to convert the array into B[] such that for every index in B (except the last) parity(b[i]) != parity(b[i + 1]) where parity(x) = x % 3. Below is the operation to be performed: Any element from the set {1, 2} can be ad
9 min read
Find the minimum number of operations required to make all array elements equal
Given an array arr[] of size N. The task is to make all the array elements equal by applying the below operations minimum number of times: Choose a pair of indices (i, j) such that |i - j| = 1 (indices i and j are adjacent) and set arr[i] = arr[i] + |arr[i] - arr[j]|Choose a pair of indices (i, j) s
6 min read
Find the minimum value of K required to remove all elements from the array in at most M operations
Given array A[] of size N and integer M, Perform the following operation until the array becomes empty. Choose K and the pick first K elements of array A[]. In one operation subtract all these chosen K elements by 1 if any element becomes zero that element will be deleted and it will be replaced by
13 min read
Find the minimum operations required to make Array elements Divisible by 3
mathGiven an array A[] of size N, count the minimum number of operations required such that all the elements of the array are divisible by 3. In one operation, we can take any two elements from the array, remove them, and append their sum at the end of the array. Note: If we can't make all elements
6 min read
Make the array elements equal by performing given operations minimum number of times
Given an array arr[] of size N, the task is to make all the array elements equal by performing following operations minimum number of times: Increase all array elements of any suffix array by 1.Decrease all the elements of any suffix array by 1.Replace any array element y another. Examples: Input: a
7 min read
Minimize the non-zero elements in the Array by given operation
Given an array arr[] of length N, the task is to minimize the count of the number of non-zero elements by adding the value of the current element to any of its adjacent element and subtracting from the current element at most once.Examples: Input: arr[] = { 1, 0, 1, 0, 0, 1 } Output: 2 Explanation:
5 min read
Minimum increment/decrement operations required on Array to satisfy given conditions
Given an array arr[] of size N, the task is to find the minimum number of increment or decrement operations required at any index i such that for each i (1 ? i < N) if the sum of elements at index from 1 to i is positive then the sum of elements from 1 to i + 1 must be negative or vice versa. Not
11 min read