📌  相关文章
📜  使所有数组元素与给定数组的总和相等的最小增量,在精确删除一次后

📅  最后修改于: 2021-10-27 03:20:59             🧑  作者: Mango

给定一个大小为N的数组arr[]和一个整数K ,任务是通过删除一个数组元素并增加所有其他数组元素的值来检查是否可以使所有数组元素相等,以便数组元素的总和保持原样。如果发现是真的,则打印“YES” 。否则,打印“NO”

例子 :

方法:该问题可以使用贪心技术解决。这个想法是删除最大的数组元素并增加其他元素的值,使总和保持不变。请按照以下步骤解决问题:

  • 初始化一个变量,比如secMax ,以存储数组的第二大元素。
  • 初始化一个变量,比如totalSum ,以存储数组元素的总和。
  • 遍历数组,计算数组元素之和,找到第二大数组元素。
  • 如果totalSum < (N – 1) * secMaxtotalSum % (N – 1) != 0 ,则打印“NO”
  • 否则,打印“YES”

下面是上述方法的实现

C++
// C++ program to implement
// the above approach
 
#include 
using namespace std;
 
// Function to check if an array of
// equal elements with sum equal to
// the given array can be obtained or not
bool CheckAllarrayEqual(int arr[], int N)
{
    // Base case
    if (N == 1) {
        return true;
    }
 
    // Stores sum of
    // array elements
    int totalSum = arr[0];
 
    // Stores second largest
    // array element
    int secMax = INT_MIN;
 
    // Stores the largest
    // array element
    int Max = arr[0];
 
    // Traverse the array
    for (int i = 1; i < N; i++) {
 
        if (arr[i] >= Max) {
 
            // Update secMax
            secMax = Max;
 
            // Update Max
            Max = arr[i];
        }
        else if (arr[i] > secMax) {
 
            // Update secMax
            secMax = arr[i];
        }
 
        // Update totalSum
        totalSum += arr[i];
    }
 
    // If totalSum is less than
    // secMax * (N - 1))
    if ((secMax * (N - 1)) > totalSum) {
 
        return false;
    }
 
    // If totalSum is not
    // divisible by (N - 1)
    if (totalSum % (N - 1)) {
 
        return false;
    }
 
    return true;
}
 
// Driver Code
int main()
{
    int arr[] = { 6, 2, 2, 2 };
    int N = sizeof(arr) / sizeof(arr[0]);
 
    if (CheckAllarrayEqual(arr, N)) {
        cout << "YES";
    }
    else {
        cout << "NO";
    }
}


Java
// Java program to implement
// the above approach
import java.util.*;
    
class GFG{
 
// Function to check if an array of
// equal elements with sum equal to
// the given array can be obtained or not
static boolean CheckAllarrayEqual(int[] arr,
                                  int N)
{
     
    // Base case
    if (N == 1)
    {
        return true;
    }
   
    // Stores sum of
    // array elements
    int totalSum = arr[0];
   
    // Stores second largest
    // array element
    int secMax = Integer.MIN_VALUE;
   
    // Stores the largest
    // array element
    int Max = arr[0];
   
    // Traverse the array
    for(int i = 1; i < N; i++)
    {
        if (arr[i] >= Max)
        {
             
            // Update secMax
            secMax = Max;
             
            // Update Max
            Max = arr[i];
        }
        else if (arr[i] > secMax)
        {
             
            // Update secMax
            secMax = arr[i];
        }
         
        // Update totalSum
        totalSum += arr[i];
    }
   
    // If totalSum is less than
    // secMax * (N - 1))
    if ((secMax * (N - 1)) > totalSum)
    {
        return false;
    }
   
    // If totalSum is not
    // divisible by (N - 1)
    if (totalSum % (N - 1) != 0)
    {
        return false;
    }
   
    return true;
}
    
// Driver Code
public static void main(String[] args)
{
    int[] arr = { 6, 2, 2, 2 };
    int N = arr.length;
   
    if (CheckAllarrayEqual(arr, N))
    {
        System.out.print("YES");
    }
    else
    {
        System.out.print("NO");
    }
}
}
 
// This code is contributed by susmitakundugoaldanga


Python3
# Python3 program to implement
# the above approach
 
# Function to check if an array of
# equal elements with sum equal to
# the given array can be obtained or not
def CheckAllarrayEqual(arr, N):
     
    # Base case
    if (N == 1):
        return True
 
    # Stores sum of
    # array elements
    totalSum = arr[0]
 
    # Stores second largest
    # array element
    secMax = -10**19
 
    # Stores the largest
    # array element
    Max = arr[0]
 
    # Traverse the array
    for i in range(1,N):
 
        if (arr[i] >= Max):
 
            # Update secMax
            secMax = Max
 
            # Update Max
            Max = arr[i]
        elif (arr[i] > secMax):
 
            # Update secMax
            secMax = arr[i]
 
        # Update totalSum
        totalSum += arr[i]
 
    # If totalSum is less than
    # secMax * (N - 1))
    if ((secMax * (N - 1)) > totalSum):
 
        return False
 
    # If totalSum is not
    # divisible by (N - 1)
    if (totalSum % (N - 1)):
        return False
    return True
 
# Driver Code
if __name__ == '__main__':
    arr=[6, 2, 2, 2]
    N = len(arr)
 
    if (CheckAllarrayEqual(arr, N)):
        print("YES")
    else:
        print("NO")
 
        # This code is contributed by mohit kumar 29


C#
// C# program to implement
// the above approach 
using System;
class GFG
{
  
// Function to check if an array of
// equal elements with sum equal to
// the given array can be obtained or not
static bool CheckAllarrayEqual(int[] arr, int N)
{
    // Base case
    if (N == 1) {
        return true;
    }
  
    // Stores sum of
    // array elements
    int totalSum = arr[0];
  
    // Stores second largest
    // array element
    int secMax = Int32.MinValue;
  
    // Stores the largest
    // array element
    int Max = arr[0];
  
    // Traverse the array
    for (int i = 1; i < N; i++) {
  
        if (arr[i] >= Max) {
  
            // Update secMax
            secMax = Max;
  
            // Update Max
            Max = arr[i];
        }
        else if (arr[i] > secMax) {
  
            // Update secMax
            secMax = arr[i];
        }
  
        // Update totalSum
        totalSum += arr[i];
    }
  
    // If totalSum is less than
    // secMax * (N - 1))
    if ((secMax * (N - 1)) > totalSum) {
  
        return false;
    }
  
    // If totalSum is not
    // divisible by (N - 1)
    if (totalSum % (N - 1) != 0) {
  
        return false;
    }
  
    return true;
}
  
// Driver Code
public static void Main()
{
    int[] arr = { 6, 2, 2, 2 };
    int N = arr.Length;
  
    if (CheckAllarrayEqual(arr, N)) {
        Console.Write("YES");
    }
    else {
        Console.Write("NO");
    }
}
}
 
// This code is contributed by sanjoy_62


Javascript


输出:
YES

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

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程