给定一个整数和一个整数数组 ,任务是在减去数组的所有元素后,通过减去的倍数找到最小的可能总和。 从每个元素开始计算(结果必须为正,并且在减少之后,数组的每个元素必须相等)。如果无法缩小数组,则打印 。注意,在数组的最终状态下元素可以减少也可以不减少。
例子:
Input: arr[] = {2, 3, 4, 5}, K = 1
Output: 4
Subtract 1 form 2, arr[] = {1, 3, 4, 5}
Subtract 2 from 3, arr[] = {1, 1, 4, 5}
Subtract 3 from 4, arr[] = {1, 1, 1, 5}
Subtract 4 from 5 to make arr[] = {1, 1, 1, 1}, thus giving minimum possible sum as 4.
Input: arr[] = {5, 6, 7}, K = 2
Output: -1
方法:首先,需要对数组进行排序,因为可以使用贪婪方法解决问题。
- 对数组进行排序,如果arr [0] <0,则打印-1,因为每个元素都必须≥0。
- 如果K == 0,则没有元素可以进一步减少。因此,为了获得答案,数组的每个元素必须相等。因此元素的总和为n * arr [0],否则为-1 。
- 现在,对于其余元素,运行从1到n的循环,并检查((arr [i] – arr [0])%K)== 0是否可以将arr [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
输出:
4
时间复杂度:O(n Log n)
进一步优化:
不用对数组进行排序,我们可以在O(n)的时间内找到最小的元素。我们可以检查所有元素在O(n)时间内是否相同。