给定一个由N个整数组成的数组arr [] ,任务是用它们的和替换最小数量的相邻元素对,以使所有数组元素相等。打印所需最少数量的此类操作。
例子:
Input: arr[] = {1, 2, 3}
Output: 1
Explanation: Replace arr[0] and arr[1] by their sum. Therefore, the array modifies to {3, 3}.
After completing the above operations, all the array elements become equal.
Therefore, the number of operations required is 1.
Input: arr[] = {4, 4, 4}
Output: 0
方法:可以使用前缀求和数组技术解决给定的问题。请按照以下步骤解决给定的问题:
- 初始化一个变量,例如count ,以存储可以为任何给定的总和值获得的子数组的最大数量。
- 初始化大小为N的辅助数组prefix [] ,并在其中存储给定数组arr []的前缀和。
- 遍历数组prefix [] ,对于每个元素prefix [i] ,执行以下步骤:
- 检查给定的数组arr []是否可以划分为总和为[i]的子数组。如果发现为真,则将此类子数组的计数存储在变量ans中。
- 将count的值更新为count和ans的最大值。
- 完成上述步骤后,将(N –计数)的值打印为所需的最少操作数。
下面是上述方法的实现:
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.
输出:
2
时间复杂度: O(N 2 )
辅助空间: O(N)