📌  相关文章
📜  通过重复用不相等的相邻数组元素对替换它们的总和,以最小化数组长度

📅  最后修改于: 2021-04-23 21:45:50             🧑  作者: Mango

给定一个整数数组arr [],任务是通过用其和重复替换两个不相等的相邻数组元素,以最小化给定数组的长度。一旦将阵列减小到其最小可能长度,即阵列中没有剩余的不相等对,请打印所需的操作计数。

例子:

天真的方法:解决问题的最简单方法是遍历给定的数组,对于每个相邻的不相等对,用其和替换该对。最后,如果数组中不存在不相等的对,则打印数组的长度。
时间复杂度: O(N 2 )
辅助空间:O(N)

高效的方法:可以基于以下观察来优化上述方法:

  • 如果数组的所有元素均相等,则无法执行任何操作。因此,打印N ,即数组的初始长度,作为数组的最小可缩减长度
  • 否则,数组的最小长度将始终为1

因此,要解决该问题,只需遍历数组并检查所有数组元素是否相等。如果发现是正确的,则将N打印为必需的答案。否则,打印1
下面是上述方法的实现:

C++
// C++ program for 
// the above approach
#include 
using namespace std;
  
// Function that returns the minimum
// length of the array after merging
// unequal adjacent elements
int minLength(int A[], int N)
{
  
    // Stores the first element
    // and its frequency
    int elem = A[0], count = 1;
  
    // Traverse the array
    for (int i = 1; i < N; i++) {
        if (A[i] == elem) {
            count++;
        }
        else {
            break;
        }
    }
  
    // If all elements are equal
    if (count == N)
  
        // No merge-pair operations
        // can be performed
        return N;
  
    // Otherwise
    else
        return 1;
}
  
// Driver Code
int main()
{
    // Given array
    int arr[] = { 2, 1, 3, 1 };
  
    // Length of the array
    int N = sizeof(arr) / sizeof(arr[0]);
  
    // Function Call
    cout << minLength(arr, N) << endl;
  
    return 0;
}


Java
// Java program for 
// the above approach
class GFG{
  
// Function that returns the minimum
// length of the array 
// after merging unequal 
// adjacent elements  
static int minLength(int A[], 
                     int N)
{
  // Stores the first element
  // and its frequency
  int elem = A[0], count = 1;
  
  // Traverse the array
  for (int i = 1; i < N; i++) 
  {
    if (A[i] == elem) 
    {
      count++;
    }
    else 
    {
      break;
    }
  }
  
  // If all elements are equal
  if (count == N)
  
    // No merge-pair operations
    // can be performed
    return N;
  
  // Otherwise
  else
    return 1;
}
  
// Driver Code
public static void main(String[] args)
{
  // Given array
  int arr[] = {2, 1, 3, 1};
  
  // Length of the array
  int N = arr.length;
  
  // Function Call
  System.out.print(minLength(arr, N) + "\n");
}
}
  
// This code is contributed by Rajput-Ji


Python3
# Python3 program for the above approach
  
# Function that returns the minimum
# length of the array after merging
# unequal adjacent elements
def minLength(A, N):
  
    # Stores the first element
    # and its frequency
    elem = A[0]
    count = 1
  
    # Traverse the array
    for i in range(1, N):
        if (A[i] == elem):
            count += 1
        else:
            break
      
    # If all elements are equal
    if (count == N):
  
        # No merge-pair operations
        # can be performed
        return N
  
    # Otherwise
    else:
        return 1
  
# Driver Code
  
# Given array
arr = [ 2, 1, 3, 1 ]
  
# Length of the array
N = len(arr) 
  
# Function call
print(minLength(arr, N))
  
# This code is contributed by code_hunt


C#
// C# program for 
// the above approach
using System;
class GFG{
  
// Function that returns the minimum
// length of the array 
// after merging unequal 
// adjacent elements  
static int minLength(int []A, 
                     int N)
{
  // Stores the first element
  // and its frequency
  int elem = A[0], count = 1;
  
  // Traverse the array
  for (int i = 1; i < N; i++) 
  {
    if (A[i] == elem) 
    {
      count++;
    }
    else 
    {
      break;
    }
  }
  
  // If all elements are equal
  if (count == N)
  
    // No merge-pair operations
    // can be performed
    return N;
  
  // Otherwise
  else
    return 1;
}
  
// Driver Code
public static void Main(String[] args)
{
  // Given array
  int []arr = {2, 1, 3, 1};
  
  // Length of the array
  int N = arr.Length;
  
  // Function Call
  Console.Write(minLength(arr, N) + "\n");
}
}
  
// This code is contributed by Rajput-Ji


输出:
1

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