📌  相关文章
📜  通过重复镜像操作获得给定Array所需的最小元素数

📅  最后修改于: 2021-05-06 22:39:16             🧑  作者: Mango

给定一个由N个整数组成的数组arr [] ,任务是找到最小可能长度的数组K [] ,以便在对K []执行多次镜像操作之后,可以获得给定的数组arr []

例子:

天真的方法:
解决该问题的最简单方法是从给定大小小于N / 2的给定数组生成所有可能的子数组,并针对每个子数组检查执行镜像操作是否给出数组arr [] 。打印满足条件的最小长度子数组。如果没有找到满意的子数组,则打印No。
时间复杂度: O(N 3 )
辅助空间: O(N)
高效方法:
可以使用分而治之技术进一步优化上述方法。请按照以下步骤解决问题:

  • 初始化变量K = N ,然后检查长度为KA []的前缀是否是回文。
  • 如果长度K的前缀是回文,则将K除以2并执行上述检查。
  • 如果前缀不是回文,则答案为K的当前值。
  • K> 0时继续检查,直到K为奇数为止。
  • 如果K奇数,则打印K的当前值。

下面是上述方法的实现:

C++
// C++ Program to implement
// the above approach
#include 
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


输出:
2

时间复杂度: O(N * log N)
辅助空间: O(N)