📌  相关文章
📜  从数组元素中减去k的倍数后的最小和

📅  最后修改于: 2021-10-26 05:27:38             🧑  作者: Mango

给定一个整数K 和一个整数数组arr ,任务是找到数组中所有元素通过减去倍数减少后的最小可能总和K 从每个元素(结果必须是正数,并且数组的每个元素在此减少后必须相等)。如果数组不能减少,则打印-1 .请注意,在数组的最终状态下,元素可能会或可能不会减少。
例子:

方法:首先,需要对数组进行排序,因为可以使用贪心方法解决问题。

  • 对数组进行排序,如果arr[0] < 0,则打印-1,因为每个元素都需要 ≥ 0。
  • 如果K == 0则没有元素可以进一步减少。因此,为了得到答案,数组中的每个元素都必须相等。所以元素的总和是n * arr[0] else print -1
  • 现在对于其余元素,运行从1 到 n的循环并检查((arr[i] – arr[0]) % K) == 0arr[i] 是否可以减少为arr[0]
  • 如果任何元素的上述条件失败,则打印-1
  • 否则,如果k == 1则答案为n即每个元素都将减少到1
  • 否则答案是n * (a[0] % k)

下面是上述方法的实现:

C++
// C++ program of the above approach
#include 
using namespace std;
 
// function to calculate minimum sum after transformation
int min_sum(int n, int k, int a[])
{
 
    sort(a, a + n);
 
    if (a[0] < 0)
        return -1;
     
 
    // no element can be reduced further
    if (k == 0) {
 
        // if all the elements of the array
        // are identical
        if (a[0] == a[n - 1])
            return (n * a[0]);
        else
            return -1;
    }
    else {
        int f = 0;
        for (int i = 1; i < n; i++) {
 
            int p = a[i] - a[0];
 
            // check if a[i] can be reduced to a[0]
            if (p % k == 0)
                continue;
            else {
                f = 1;
                break;
            }
        }
 
        // one of the elements cannot be reduced
        // to be equal to the other elements
        if (f)
            return -1;
        else {
 
            // if k = 1 then all elements can be reduced to 1
            if (k == 1)
                return n;
            else
                return (n * (a[0] % k));
        }
    }
}
 
// Driver code
int main()
{
    int arr[] = { 2, 3, 4, 5 };
    int K = 1;
    int N = sizeof(arr) / sizeof(arr[0]);
    cout << min_sum(N, K, arr);
 
    return 0;
}


Java
// Java program of the above approach
 
import java.io.*;
import java.util.*;
class GFG {
     
// function to calculate minimum sum after transformation
static int min_sum(int n, int k, int a[])
{
 
    Arrays.sort(a);
 
    if (a[0] < 0)
        return -1;
     
 
    // no element can be reduced further
    if (k == 0) {
 
        // if all the elements of the array
        // are identical
        if (a[0] == a[n - 1])
            return (n * a[0]);
        else
            return -1;
    }
    else {
        int f = 0;
        for (int i = 1; i < n; i++) {
 
            int p = a[i] - a[0];
 
            // check if a[i] can be reduced to a[0]
            if (p % k == 0)
                continue;
            else {
                f = 1;
                break;
            }
        }
 
        // one of the elements cannot be reduced
        // to be equal to the other elements
        if (f>0)
            return -1;
        else {
 
            // if k = 1 then all elements can be reduced to 1
            if (k == 1)
                return n;
            else
                return (n * (a[0] % k));
        }
    }
}
 
// Driver code
 
 
    public static void main (String[] args) {
            int arr[] = { 2, 3, 4, 5 };
    int K = 1;
    int N = arr.length;
    System.out.println(min_sum(N, K, arr));
    }
}
// This code is contributed by shs..


Python3
# Python 3 program of the above approach
 
# function to calculate minimum sum
# after transformation
def min_sum(n, k, a):
    a.sort(reverse = False)
 
    if (a[0] < 0):
        return -1
     
    # no element can be reduced further
    if (k == 0):
         
        # if all the elements of the
        # array are identical
        if (a[0] == a[n - 1]):
            return (n * a[0])
        else:
            return -1
     
    else:
        f = 0
        for i in range(1, n, 1):
            p = a[i] - a[0]
 
            # check if a[i] can be
            # reduced to a[0]
            if (p % k == 0):
                continue
            else:
                f = 1
                break
         
        # one of the elements cannot be reduced
        # to be equal to the other elements
        if (f):
            return -1
        else:
             
            # if k = 1 then all elements
            # can be reduced to 1
            if (k == 1):
                return n
            else:
                return (n * (a[0] % k))
     
# Driver code
if __name__ == '__main__':
    arr = [2, 3, 4, 5]
    K = 1
    N = len(arr)
    print(min_sum(N, K, arr))
 
# This code is contributed by
# Surendra_Gangwar


C#
// C# program of the above approach
using System;
 
class GFG
{
     
// function to calculate minimum
// sum after transformation
static int min_sum(int n, int k, int[] a)
{
 
    Array.Sort(a);
 
    if (a[0] < 0)
        return -1;
 
    // no element can be reduced further
    if (k == 0)
    {
 
        // if all the elements of the array
        // are identical
        if (a[0] == a[n - 1])
            return (n * a[0]);
        else
            return -1;
    }
    else
    {
        int f = 0;
        for (int i = 1; i < n; i++)
        {
            int p = a[i] - a[0];
 
            // check if a[i] can be
            // reduced to a[0]
            if (p % k == 0)
                continue;
            else
            {
                f = 1;
                break;
            }
        }
 
        // one of the elements cannot be reduced
        // to be equal to the other elements
        if (f > 0)
            return -1;
        else
        {
 
            // if k = 1 then all elements can
            // be reduced to 1
            if (k == 1)
                return n;
            else
                return (n * (a[0] % k));
        }
    }
}
 
// Driver code
public static void Main ()
{
    int[] arr = new int[] { 2, 3, 4, 5 };
    int K = 1;
    int N = arr.Length;
    Console.WriteLine(min_sum(N, K, arr));
}
}
 
// This code is contributed by mits


PHP


Javascript


输出:
4

时间复杂度:O(n Log n)
进一步优化:
我们可以在 O(n) 时间内找到最小元素,而不是对数组进行排序。我们也可以在 O(n) 时间内检查所有元素是否相同。

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