📌  相关文章
📜  从较大的元素减去较小的元素后,数组元素的最小总和

📅  最后修改于: 2021-06-26 16:26:57             🧑  作者: Mango

给定数组arr ,任务是在应用以下操作后找到数组元素的最小和:
对于数组中的任何一对,如果a [i]> a [j],a [i] = a [i] – a [j]

例子:

方法:在这里观察到,在每次操作之后,所有元素的GCD将保持不变。因此,最后,在应用了给定的操作之后,每个元素都将等于数组中所有元素的gcd。
因此,最终答案将是(n * gcd)

下面是上述方法的实现:

C++
// CPP program to Find the minimum sum
// of given array after applying given operation.
#include 
using namespace std;
 
// Function to Find the minimum sum
// of given array after applying given operation.
int MinSum(int a[], int n)
{
    // to store final gcd value
    int gcd = a[0];
 
    // get gcd of the whole array
    for (int i = 1; i < n; i++)
        gcd = __gcd(a[i], gcd);
 
    return n * gcd;
}
 
// Driver code
int main()
{
 
    int a[] = { 20, 14, 6, 8, 15 };
 
    int n = sizeof(a) / sizeof(a[0]);
 
    // function call
    cout << MinSum(a, n);
 
    return 0;
}


Java
// Java program to Find the minimum sum
// of given array after applying given operation.
 
import java.io.*;
 
class GFG {
    
// Recursive function to return gcd of a and b
static int __gcd(int a, int b)
{
    // Everything divides 0 
    if (a == 0)
       return b;
    if (b == 0)
       return a;
    
    // base case
    if (a == b)
        return a;
    
    // a is greater
    if (a > b)
        return __gcd(a-b, b);
    return __gcd(a, b-a);
}
// Function to Find the minimum sum
// of given array after applying given operation.
static int MinSum(int []a, int n)
{
    // to store final gcd value
    int gcd = a[0];
 
    // get gcd of the whole array
    for (int i = 1; i < n; i++)
        gcd = __gcd(a[i], gcd);
 
    return n * gcd;
}
 
// Driver code
 
    public static void main (String[] args) {
            int a[] = { 20, 14, 6, 8, 15 };
 
    int n = a.length;
 
    // function call
    System.out.println(MinSum(a, n));
    }
}
// This code is contributed by anuj_67..


Python3
# Python3 program to Find the minimum
# sum of given array after applying
# given operation.
import math
 
# Function to Find the minimum sum
# of given array after applying
# given operation.
def MinSum(a, n):
 
    # to store final gcd value
    gcd = a[0]
 
    # get gcd of the whole array
    for i in range(1, n):
        gcd = math.gcd(a[i], gcd)
 
    return n * gcd
 
# Driver code
if __name__ == "__main__":
 
    a = [20, 14, 6, 8, 15 ]
 
    n = len(a)
 
    # function call
    print(MinSum(a, n))
 
# This code is contributed by ita_c


C#
// C# program to Find the minimum sum
// of given array after applying given operation.
 
using System;
class GFG {
    
    // Recursive function to return gcd of a and b
    static int __gcd(int a, int b)
    {
        // Everything divides 0 
        if (a == 0)
           return b;
        if (b == 0)
           return a;
        
        // base case
        if (a == b)
            return a;
        
        // a is greater
        if (a > b)
            return __gcd(a-b, b);
        return __gcd(a, b-a);
    }
     
    // Function to Find the minimum sum
    // of given array after applying given operation.
    static int MinSum(int []a, int n)
    {
        // to store final gcd value
        int gcd = a[0];
     
        // get gcd of the whole array
        for (int i = 1; i < n; i++)
            gcd = __gcd(a[i], gcd);
     
        return n * gcd;
    }
     
     
    // Driver Program to test above function
    static void Main()
    {
        int []a = { 20, 14, 6, 8, 15 };
        int n = a.Length;
        Console.WriteLine(MinSum(a, n));
    }
     
    // This code is contributed by Ryuga.
}


PHP


Javascript


输出:
5

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