📌  相关文章
📜  根据给定条件最小化数组的总和

📅  最后修改于: 2021-06-25 19:48:58             🧑  作者: Mango

给定整数数组A。任务是使用以下规则使数组元素的总和最小化:
选择两个索引ij以及任意整数x ,以使xA [i]的除数,然后将它们更改为A [i] = A [i] / xA [j] = A [j] * X。
例子:

方法:如果将任何数字除以x,则最好将x与数组中存在的最小数字相乘。
这个想法是获得数组的最小值,找到特定元素的除数,并不断检查总和减少了多少。
下面是上述方法的实现:

C++
// C++ implementation
#include 
using namespace std;
 
// Function to return the minimum sum
void findMin(int arr[], int n)
{
    int sum = 0;
    for (int i = 0; i < n; i++)
        sum += arr[i];
 
    // sort the array to find the
    // minimum element
    sort(arr, arr + n);
 
    int min = arr[0];
    int max = 0;
 
    for (int i = n - 1; i >= 1; i--) {
        int num = arr[i];
        int total = num + min;
        int j;
 
        // finding the number to
        // divide
        for (j = 2; j <= num; j++) {
            if (num % j == 0) {
                int d = j;
                int now = (num / d)
                          + (min * d);
 
                // Checking to what
                // instance the sum
                // has decreased
                int reduce = total - now;
 
                // getting the max
                // difference
                if (reduce > max)
                    max = reduce;
            }
        }
    }
    cout << (sum - max);
}
 
// Driver Code
int main()
{
    int arr[] = { 1, 2, 3, 4, 5 };
    int n = sizeof(arr) / sizeof(arr[0]);
    findMin(arr, n);
}


Java
// Java implementation of the above approach
import java.util.*;
 
class GFG
{
     
    // Function to return the minimum sum
    static void findMin(int arr[], int n)
    {
        int sum = 0;
        for (int i = 0; i < n; i++)
            sum += arr[i];
     
        // sort the array to find the
        // minimum element
        Arrays.sort(arr);
     
        int min = arr[0];
        int max = 0;
     
        for (int i = n - 1; i >= 1; i--)
        {
            int num = arr[i];
            int total = num + min;
            int j;
     
            // finding the number to
            // divide
            for (j = 2; j <= num; j++)
            {
                if (num % j == 0)
                {
                    int d = j;
                    int now = (num / d) +
                              (min * d);
     
                    // Checking to what
                    // instance the sum
                    // has decreased
                    int reduce = total - now;
     
                    // getting the max
                    // difference
                    if (reduce > max)
                        max = reduce;
                }
            }
        }
        System.out.println(sum - max);
    }
     
    // Driver Code
    public static void main (String[] args)
    {
        int arr[] = { 1, 2, 3, 4, 5 };
        int n = arr.length;
        findMin(arr, n);
    }
}
 
// This code is contributed by AnkitRai01


Python3
# Function to return the minimum sum
def findMin(arr, n):
    sum = 0
    for i in range(0, n):
        sum = sum + arr[i]
 
    # sort the array to find the
    # minimum element
    arr.sort()
 
    min = arr[0]
    max = 0
 
    for i in range(n - 1, 0, -1):
        num = arr[i]
        total = num + min
 
        # finding the number to
        # divide
        for j in range(2, num + 1):
            if(num % j == 0):
                d = j
                now = (num // d) + (min * d)
 
                # Checking to what
                # instance the sum
                # has decreased
                reduce = total - now
 
                # getting the max
                # difference
                if(reduce > max):
                    max = reduce
 
    print(sum - max)
 
# Driver Code
arr = [1, 2, 3, 4, 5 ]
n = len(arr)
findMin(arr, n)
 
# This code is contributed by Sanjit_Prasad


C#
// C# implementation of the above approach
using System;
     
class GFG
{
     
    // Function to return the minimum sum
    static void findMin(int []arr, int n)
    {
        int sum = 0;
        for (int i = 0; i < n; i++)
            sum += arr[i];
     
        // sort the array to find the
        // minimum element
        Array.Sort(arr);
     
        int min = arr[0];
        int max = 0;
     
        for (int i = n - 1; i >= 1; i--)
        {
            int num = arr[i];
            int total = num + min;
            int j;
     
            // finding the number to
            // divide
            for (j = 2; j <= num; j++)
            {
                if (num % j == 0)
                {
                    int d = j;
                    int now = (num / d) +
                              (min * d);
     
                    // Checking to what
                    // instance the sum
                    // has decreased
                    int reduce = total - now;
     
                    // getting the max
                    // difference
                    if (reduce > max)
                        max = reduce;
                }
            }
        }
        Console.WriteLine(sum - max);
    }
     
    // Driver Code
    public static void Main (String[] args)
    {
        int []arr = { 1, 2, 3, 4, 5 };
        int n = arr.Length;
        findMin(arr, n);
    }
}
 
// This code is contributed by PrinciRaj1992


Javascript


输出:
14

如果您希望与行业专家一起参加现场课程,请参阅《 Geeks现场课程》和《 Geeks现场课程美国》。