📌  相关文章
📜  通过用它们的总和替换相邻对,使所有数组元素相等

📅  最后修改于: 2021-09-06 06:31:14             🧑  作者: Mango

给定一个由N 个整数组成的数组arr[] ,任务是用它们的和替换最小数量的相邻元素对,以使所有数组元素相等。打印所需的最少数量的此类操作。

例子:

方法:可以使用前缀和数组技术解决给定的问题。请按照以下步骤解决给定的问题:

  • 初始化一个变量,比如count ,以存储可以为任何给定和值获得的子数组的最大计数。
  • 初始化一个辅助数组prefix[] ,大小为N ,并将给定数组arr[]的前缀和存储在其中。
  • 遍历数组prefix[]并对每个元素prefix[i]执行以下步骤:
    • 检查给定的数组arr[]是否可以分为 sum prefix[i] 的子数组。如果发现为true ,则将此类子数组的计数存储在变量中,例如ans
    • count的值更新为countans的最大值。
  • 完成上述步骤后,打印(N – count)的值作为所需的最小操作次数。

下面是上述方法的实现:

C++
// C++ program for the above approach
#include 
using namespace std;
 
// Function to count the minimum number
// of pairs of adjacent elements required
// to be replaced by their sum to make all
// array elements equal
int minSteps(vector a, int n)
{
 
    // Stores the prefix sum of the array
    vector prefix_sum(n);
    prefix_sum[0] = a[0];
 
    // Calculate the prefix sum array
    for (int i = 1; i < n; i++)
        prefix_sum[i] += prefix_sum[i - 1] + a[i];
 
    // Stores the maximum number of subarrays
    // into which the array can be split
    int mx = -1;
 
    // Iterate over all possible sums
    for (int subgroupsum :prefix_sum)
    {
 
        int sum = 0;
        int i = 0;
        int grp_count = 0;
 
        // Traverse the array
        while (i < n)
        {
            sum += a[i];
 
            // If the sum is equal to
            // the current prefix sum
            if (sum == subgroupsum)
            {
                // Increment count
                // of groups by 1
                grp_count += 1;
                sum = 0;
              }
           
            // Otherwise discard
            // this subgroup sum
            else if(sum > subgroupsum)
            {
 
                grp_count = -1;
                break;
              }
            i += 1;
          }
 
        // Update the maximum
        // this of subarrays
        if (grp_count > mx)
            mx = grp_count;
      }
 
    // Return the minimum
    // number of operations
    return n - mx;
}
 
// Driver Code
int main()
{
  vector A = {1, 2, 3, 2, 1, 3};
  int N = A.size();
 
  // Function Call
  cout << minSteps(A, N);
 
  return 0;
}
 
// This code is contributed by mohit kumar 29.


Java
// Java program for the above approach
import java.util.*;
 
class GFG{
 
// Function to count the minimum number
// of pairs of adjacent elements required
// to be replaced by their sum to make all
// array elements equal
static int minSteps(ArrayList a, int n)
{
     
    // Stores the prefix sum of the array
    int []prefix_sum = new int[n];
    prefix_sum[0] = a.get(0);
 
    // Calculate the prefix sum array
    for(int i = 1; i < n; i++)
        prefix_sum[i] += prefix_sum[i - 1] + a.get(i);
 
    // Stores the maximum number of subarrays
    // into which the array can be split
    int mx = -1;
 
    // Iterate over all possible sums
    for(int subgroupsum : prefix_sum)
    {
        int sum = 0;
        int i = 0;
        int grp_count = 0;
 
        // Traverse the array
        while (i < n)
        {
            sum += a.get(i);
 
            // If the sum is equal to
            // the current prefix sum
            if (sum == subgroupsum)
            {
                 
                // Increment count
                // of groups by 1
                grp_count += 1;
                sum = 0;
            }
             
            // Otherwise discard
            // this subgroup sum
            else if(sum > subgroupsum)
            {
                grp_count = -1;
                break;
            }
            i += 1;
        }
         
        // Update the maximum
        // this of subarrays
        if (grp_count > mx)
            mx = grp_count;
    }
 
    // Return the minimum
    // number of operations
    return n - mx;
}
 
// Driver Code
public static void main(String[] args)
{  
   ArrayListA = new ArrayList();
   A.add(1);
   A.add(2);
   A.add(3);
   A.add(2);
   A.add(1);
   A.add(3);
 
  int N = A.size();
 
  // Function Call
  System.out.print(minSteps(A, N));
}
}
 
// This code is contributed by sanjoy_62


Python3
# Python3 program for the above approach
 
# Function to count the minimum number
# of pairs of adjacent elements required
# to be replaced by their sum to make all
# arrat elements equal
def minSteps(a, n):
     
    # Stores the prefix sum of the array   
    prefix_sum = a[:]
     
    # Calculate the prefix sum array
    for i in range(1, n):
        prefix_sum[i] += prefix_sum[i-1]
 
    # Stores the maximum number of subarrays
    # into which the array can be split
    mx = -1
 
    # Iterate over all possible sums
    for subgroupsum in prefix_sum:
 
        sum = 0
        i = 0
        grp_count = 0
 
        # Traverse the array
        while i < n:
            sum += a[i]
 
            # If the sum is equal to
            # the current prefix sum
            if sum == subgroupsum:
 
                # Increment count
                # of groups by 1
                grp_count += 1
                sum = 0
                 
            # Otherwise discard
            # this subgroup sum
            elif sum > subgroupsum:
 
                grp_count = -1
                break
                 
            i += 1
 
        # Update the maximum
        # this of subarrays      
        if grp_count > mx:
            mx = grp_count
 
    # Return the minimum
    # number of operations   
    return n - mx
 
 
# Driver Code
if __name__ == '__main__':
   
    A = [1, 2, 3, 2, 1, 3]
    N = len(A)
     
    # Function Call
    print(minSteps(A, N))


C#
// C# program for the above approach
using System;
using System.Collections.Generic;
class GFG
{
    
   // Function to count the minimum number
// of pairs of adjacent elements required
// to be replaced by their sum to make all
// array elements equal
static int minSteps(List a, int n)
{
 
    // Stores the prefix sum of the array
    int []prefix_sum = new int[n];
    prefix_sum[0] = a[0];
 
    // Calculate the prefix sum array
    for (int i = 1; i < n; i++)
        prefix_sum[i] += prefix_sum[i - 1] + a[i];
 
    // Stores the maximum number of subarrays
    // into which the array can be split
    int mx = -1;
 
    // Iterate over all possible sums
    foreach (int subgroupsum in prefix_sum)
    {
 
        int sum = 0;
        int i = 0;
        int grp_count = 0;
 
        // Traverse the array
        while (i < n)
        {
            sum += a[i];
 
            // If the sum is equal to
            // the current prefix sum
            if (sum == subgroupsum)
            {
                // Increment count
                // of groups by 1
                grp_count += 1;
                sum = 0;
              }
           
            // Otherwise discard
            // this subgroup sum
            else if(sum > subgroupsum)
            {
 
                grp_count = -1;
                break;
              }
            i += 1;
          }
 
        // Update the maximum
        // this of subarrays
        if (grp_count > mx)
            mx = grp_count;
      }
 
    // Return the minimum
    // number of operations
    return n - mx;
}
 
// Driver Code
public static void Main()
{
  List A = new List(){1, 2, 3, 2, 1, 3};
  int N = A.Count;
 
  // Function Call
  Console.Write(minSteps(A, N));
}
}
 
// This code is contributed by SURENDRA_GANGWAR.


Javascript


输出:
2

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

如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live