📌  相关文章
📜  通过删除所有出现的任何数组元素可能的最小和

📅  最后修改于: 2021-04-17 17:30:02             🧑  作者: Mango

给定一个由N个整数组成的数组arr [] ,任务是通过删除所有出现的任何单个数组元素来找到数组的最小可能和。

例子:

方法:解决此问题的想法是首先找到数组中每个元素的频率以及数组的总和。然后,对于每个唯一元素,通过找到数组元素和与乘积及其频率之间的差来找到最小和。

请按照以下步骤解决问题:

  • 初始化一个映射,例如mp ,以存储数组元素的频率,并初始化一个变量minSum ,以存储在删除所有出现的任何数组元素之后获得的最小和。
  • 遍历数组arr []以计算每个数组元素的频率并将其存储在Map中,并计算所有数组元素总和并将其总和存储
  • 遍历地图,并对每个键值对执行以下操作:
    • 总和中减去元素的乘积及其出现次数,并存储以minSum为单位的最小总和
  • 返回minSum作为获得的最小总和。

下面是上述方法的实现:

C++
// C++ program for the above approach
 
#include 
using namespace std;
 
// Function to find minimum sum after deletion
int minSum(int A[], int N)
{
    // Stores frequency of
    // array elements
    map mp;
 
    int sum = 0;
 
    // Traverse the array
    for (int i = 0; i < N; i++) {
 
        // Calculate sum
        sum += A[i];
 
        // Update frequency of
        // the current element
        mp[A[i]]++;
    }
 
    // Stores the minimum
    // sum required
    int minSum = INT_MAX;
 
    // Traverse map
    for (auto it : mp) {
 
        // Find the minimum sum obtained
        minSum = min(
            minSum, sum - (it.first * it.second));
    }
 
    // Return minimum sum
    return minSum;
}
 
// Driver code
int main()
{
    // Input array
    int arr[] = { 4, 5, 6, 6 };
 
    // Size of array
    int N = sizeof(arr) / sizeof(arr[0]);
 
    cout << minSum(arr, N) << "\n";
}


Java
// Java program for the above approach
import java.util.*;
class GFG
{
 
// Function to find minimum sum after deletion
static int minSum(int A[], int N)
{
   
    // Stores frequency of
    // array elements
    HashMap mp = new HashMap();
    int sum = 0;
 
    // Traverse the array
    for (int i = 0; i < N; i++)
    {
 
        // Calculate sum
        sum += A[i];
 
        // Update frequency of
        // the current element
        if(mp.containsKey(A[i]))
        {
            mp.put(A[i], mp.get(A[i]) + 1);
        }
        else
        {
            mp.put(A[i], 1);
        }
    }
 
    // Stores the minimum
    // sum required
    int minSum = Integer.MAX_VALUE;
 
    // Traverse map
    for (Map.Entry it : mp.entrySet())
    {
 
        // Find the minimum sum obtained
        minSum = Math.min(
            minSum, sum - (it.getKey() * it.getValue()));
    }
 
    // Return minimum sum
    return minSum;
}
 
// Driver code
public static void main(String[] args)
{
   
    // Input array
    int arr[] = { 4, 5, 6, 6 };
 
    // Size of array
    int N = arr.length;
    System.out.print(minSum(arr, N)+ "\n");
}
}
 
// This code is contributed by 29AjayKumar


Python3
# Python program for the above approach
 
# Function to find minimum sum after deletion
def minSum(A, N):
   
    # Stores frequency of
    # array elements
    mp = {}
    sum = 0
 
    # Traverse the array
    for i in range(N):
 
        # Calculate sum
        sum += A[i]
 
        # Update frequency of
        # the current element
        if A[i] in mp:
            mp[A[i]] += 1
        else:
            mp[A[i]] = 1
 
    # Stores the minimum
    # sum required
    minSum = float('inf')
 
    # Traverse map
    for it in mp:
 
        # Find the minimum sum obtained
        minSum = min(minSum, sum - (it * mp[it]))
     
    # Return minimum sum
    return minSum
 
# Driver code
# Input array
arr = [ 4, 5, 6, 6 ]
 
# Size of array
N = len(arr)
print(minSum(arr, N))
 
# This code is contributed by rohitsingh07052.


C#
// C# program for the above approach
using System;
using System.Collections.Generic;
public class GFG
{
 
  // Function to find minimum sum after deletion
  static int minSum(int []A, int N)
  {
 
    // Stores frequency of
    // array elements
    Dictionary mp = new Dictionary();
    int sum = 0;
 
    // Traverse the array
    for (int i = 0; i < N; i++)
    {
 
      // Calculate sum
      sum += A[i];
 
      // Update frequency of
      // the current element
      if(mp.ContainsKey(A[i]))
      {
        mp[A[i]] = mp[A[i]] + 1;
      }
      else
      {
        mp.Add(A[i], 1);
      }
    }
 
    // Stores the minimum
    // sum required
    int minSum = int.MaxValue;
 
    // Traverse map
    foreach (KeyValuePair it in mp)
    {
 
      // Find the minimum sum obtained
      minSum = Math.Min(
        minSum, sum - (it.Key * it.Value));
    }
 
    // Return minimum sum
    return minSum;
  }
 
  // Driver code
  public static void Main(String[] args)
  {
 
    // Input array
    int []arr = { 4, 5, 6, 6 };
 
    // Size of array
    int N = arr.Length;
    Console.Write(minSum(arr, N)+ "\n");
  }
}
 
// This code is contributed by 29AjayKumar


输出:
9

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