给定一个大小为N的数组arr [] ,该数组由正整数和整数K组成,任务是找到使数组的所有元素相等所需的最小步数,以便在每一步中,数组中的一个值可以由K选择并减1。如果数组不能相等,则打印-1。
例子:
Input: arr[] = {12, 9, 15}, K = 3
Output: 3
Explanation:
Initially: {12, 9, 15}
After decreasing K from 15 at position 3: [12, 9, 12]
After decreasing K from 12 at position 1: [9, 9, 12]
After decreasing K from 12 at position 3: [9, 9, 9]
Input: arr[] = {10, 9}, K = 2
Output: -1
Explanation:
It is impossible to equalize all elements
方法:想法是保持最小价值的元素不受影响,并计算其他元素为达到该最小值而进行的减量操作的次数。可以按照以下步骤计算结果:
- 在数组中找到最小元素minx 。
- 找到最小值后,将保持变量减量并将其初始化为0。
- 然后,对所有元素运行循环,将(arr [i] -minx)/ K添加到减量变量。
- 如果遇到任何arr [i],使得arr [i] -minx无法被K整除,则返回-1,因为它不能减小到最小值。
下面是上述方法的实现:
C++
// C++ implementation of the above approach
#include
using namespace std;
#define lli long long int
lli solve(lli arr[], lli n, lli k)
{
lli i, minx = INT_MAX;
// Finding the minimum element
for (i = 0; i < n; i++) {
minx = min(minx, arr[i]);
}
lli decrements = 0;
// Loop over all the elements
// and find the difference
for (i = 0; i < n; i++) {
if ((arr[i] - minx) % k != 0) {
return -1;
}
else {
decrements += ((arr[i] - minx) / k);
}
}
// Solution found and returned
return decrements;
}
// Driver code
int main()
{
lli n, k;
n = 3;
k = 3;
lli arr[n] = { 12, 9, 15 };
cout << solve(arr, n, k);
}
Java
// Java implementation of the above approach
class GFG
{
static int INT_MAX = Integer.MAX_VALUE ;
static int solve(int arr[], int n, int k)
{
int minx = INT_MAX;
int i;
// Finding the minimum element
for (i = 0; i < n; i++)
{
minx = Math.min(minx, arr[i]);
}
int decrements = 0;
// Loop over all the elements
// and find the difference
for (i = 0; i < n; i++)
{
if ((arr[i] - minx) % k != 0)
{
return -1;
}
else
{
decrements += ((arr[i] - minx) / k);
}
}
// Solution found and returned
return decrements;
}
// Driver code
public static void main (String[] args)
{
int n, k;
n = 3;
k = 3;
int arr[] = { 12, 9, 15 };
System.out.println(solve(arr, n, k));
}
}
// This code is contributed by AnkitRai01
Python3
# Python3 implementation of the above approach
import sys
def solve(arr, n, k) :
minx = sys.maxsize;
# Finding the minimum element
for i in range(n) :
minx = min(minx, arr[i]);
decrements = 0;
# Loop over all the elements
# and find the difference
for i in range(n) :
if ((arr[i] - minx) % k != 0) :
return -1;
else :
decrements += ((arr[i] - minx) // k);
# Solution found and returned
return decrements;
# Driver code
if __name__ == "__main__" :
n = 3;
k = 3;
arr = [ 12, 9, 15 ];
print(solve(arr, n, k));
# This code is contributed by AnkitRai01
C#
// C# implementation of the above approach
using System;
class GFG
{
static int INT_MAX = int.MaxValue ;
static int solve(int []arr, int n, int k)
{
int minx = INT_MAX;
int i;
// Finding the minimum element
for (i = 0; i < n; i++)
{
minx = Math.Min(minx, arr[i]);
}
int decrements = 0;
// Loop over all the elements
// and find the difference
for (i = 0; i < n; i++)
{
if ((arr[i] - minx) % k != 0)
{
return -1;
}
else
{
decrements += ((arr[i] - minx) / k);
}
}
// Solution found and returned
return decrements;
}
// Driver code
public static void Main()
{
int n, k;
n = 3;
k = 3;
int []arr = { 12, 9, 15 };
Console.WriteLine(solve(arr, n, k));
}
}
// This code is contributed by AnkitRai01
输出:
3
时间复杂度: O(N)