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

📅  最后修改于: 2021-09-04 07:52:39             🧑  作者: Mango

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

例子:

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

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

  • 初始化一个映射,比如mp ,以存储数组元素的频率和一个变量,比如minSum ,以存储在删除任何数组元素的所有出现后获得的最小总和。
  • 遍历数组arr[] ,统计每个数组元素出现的频率并存入一个Map中,计算出所有数组元素总和存入sum中。
  • 遍历地图,对每个键值对,执行以下操作:
    • 总和中减去元素及其出现次数的乘积,并将获得的最小总和存储在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)

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