📌  相关文章
📜  通过从两端删除相似的子数组来最小化数组的长度

📅  最后修改于: 2021-04-17 14:42:03             🧑  作者: Mango

给定大小为N的数组arr [] ,任务是通过从包含相同单个元素的数组的开头和结尾重复删除子数组,以最小化给定数组的长度。

例子:

方法:想法是使用两指针技术解决问题。请按照以下步骤解决问题:

  1. 初始化两个指针front = 0back = N – 1以同时从两端遍历数组。
  2. 遍历数组arr []直到front
    • 如果两个元素都不相同,则中断循环。
    • 否则,递增指针和递减指针,直到它们指向与当前元素不同的元素为止。
  3. 将两个指针位置之间的差异打印为数组的最小长度。

下面是上述方法的实现:

C++
// C++ program for
// the above approach
 
#include 
using namespace std;
 
// Function to minimize length of
// the array by removing similar
// subarrays from both ends of the array
void findMinLength(int arr[], int N)
{
    // Initialize two pointers
    int front = 0, back = N - 1;
 
    while (front < back) {
 
        // Stores the current integer
        int x = arr[front];
 
        // Check if the elements at
        // both ends are same or not
        if (arr[front] != arr[back])
            break;
 
        // Move the front pointer
        while (arr[front] == x
               && front <= back)
            front++;
 
        // Move the rear pointer
        while (arr[back] == x
               && front <= back)
            back--;
    }
 
    // Print the minimized length of the array
    cout << back - front + 1 << endl;
}
 
// Driver Code
int main()
{
    // Input
    int arr[] = { 1, 1, 2, 3, 3, 1, 2, 2, 1 };
    int N = sizeof(arr) / sizeof(arr[0]);
 
    // Function call to find the
    // minimized length of the array
    findMinLength(arr, N);
 
    return 0;
}


Java
// Java program for the above approach
import java.util.*;
class GFG
{
 
  // Function to minimize length of
  // the array by removing similar
  // subarrays from both ends of the array
  static void findMinLength(int arr[], int N)
  {
    // Initialize two pointers
    int front = 0, back = N - 1;
    while (front < back) {
 
      // Stores the current integer
      int x = arr[front];
 
      // Check if the elements at
      // both ends are same or not
      if (arr[front] != arr[back])
        break;
 
      // Move the front pointer
      while (arr[front] == x
             && front <= back)
        front++;
 
      // Move the rear pointer
      while (arr[back] == x
             && front <= back)
        back--;
    }
 
    // Print the minimized length of the array
    System.out.println( back - front + 1 );
  }
 
 
  // Driver Code
  public static void main(String[] args)
  {
 
    // Input
    int arr[] = { 1, 1, 2, 3, 3, 1, 2, 2, 1 };
    int N = arr.length;
 
    // Function call to find the
    // minimized length of the array
    findMinLength(arr, N);
  }
}
 
// This code is contributed by sanjoy_62.


Python3
# Python program for
# the above approach
 
# Function to minimize length of
# the array by removing similar
# subarrays from both ends of the array
def findMinLength(arr, N):
   
    # Initialize two pointers
    front = 0
    back = N - 1
    while (front < back):
       
        # Stores the current integer
        x = arr[front]
         
        # Check if the elements at
        # both ends are same or not
        if arr[front] != arr[back]:
            break
             
            # Move the front pointer
        while (arr[front] == x and front <= back):
            front += 1
             
            # Move the rear pointer
        while (arr[back] == x and front <= back):
            back -= 1
 
    # Print the minimized length of the array
    print(back - front + 1)
 
# Driver Code
# Input
arr = [1, 1, 2, 3, 3, 1, 2, 2, 1]
N = len(arr)
 
# Function call to find the
# minimized length of the array
findMinLength(arr, N)
 
# This code is contributed by sudhanshugupta2019a.


C#
// C# program for the above approach
using System;
public class GFG
{
 
  // Function to minimize length of
  // the array by removing similar
  // subarrays from both ends of the array
  static void findMinLength(int []arr, int N)
  {
 
    // Initialize two pointers
    int front = 0, back = N - 1;
    while (front < back)
    {
 
      // Stores the current integer
      int x = arr[front];
 
      // Check if the elements at
      // both ends are same or not
      if (arr[front] != arr[back])
        break;
 
      // Move the front pointer
      while (arr[front] == x
             && front <= back)
        front++;
 
      // Move the rear pointer
      while (arr[back] == x
             && front <= back)
        back--;
    }
 
    // Print the minimized length of the array
    Console.WriteLine( back - front + 1 );
  }
 
  // Driver Code
  public static void Main(String[] args)
  {
 
    // Input
    int []arr = { 1, 1, 2, 3, 3, 1, 2, 2, 1 };
    int N = arr.Length;
 
    // Function call to find the
    // minimized length of the array
    findMinLength(arr, N);
  }
}
 
// This code is contributed by shikhasingrajput


Javascript


输出:
3

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