给定的阵列ARR []大小N和整数d中,任务是使所有数组元素通过增加或减少由d数组元素的最小数目相等。如果不可能使所有数组元素相等,则打印-1 。
例子 :
Input: N = 4, d = 2, arr[ ] = {2, 4, 6, 8}
Output: 4
Explanation:
Incrementing arr[0] by D(=2) modifies arr[] to { 4, 4, 6, 8 }.
Decrementing arr[2] by D(=2) modifies arr[] to { 4, 4, 4, 8 }.
Decrementing arr[3] by D(=2) modifies arr[] to { 4, 4, 4, 6 }.
Decrementing arr[3] by D(=2) modifies arr[] to { 4, 4, 4, 4 }.
Input: N = 4, K = 3, arr[ ] = {2, 4, 5, 6}
Output: -1
方法:想法是使用Greedy Approach来解决这个问题。以下是步骤:
- 按升序对给定数组进行排序。
- 观察到只有那些数组元素可以相等,它们与相邻索引元素的差异可以被D完全整除,即(a[i + 1] – a[i]) % D == 0 。
- 如果任何差异不能被D整除,则打印-1,因为所有数组元素不能相等。
- 现在,在这里使用贪婪的方法。对于最小操作,请考虑mid 元素并尝试将所有其他元素更改为mid 。找出转换一个数字所需的操作次数,即abs(mid – a[i]) / D 。
- 最后,打印所需的最少操作次数。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to find minimum count of operations
// required to make all array elements equal by
// incrementing or decrementing array elements by d
void numOperation(int arr[], int N, int D)
{
// Sort the array
sort(arr, arr + N);
// Traverse the array
for (int i = 0; i < N - 1; i++) {
// If difference between two consecutive
// elements are not divisible by D
if ((arr[i + 1] - arr[i]) % D != 0) {
cout << "-1";
return;
}
}
// Store minimum count of operations required to
// make all array elements equal by incrementing
// or decrementing array elements by d
int count = 0;
// Stores middle element
// of the array
int mid = arr[N / 2];
// Traverse the array
for (int i = 0; i < N; i++) {
// Update count
count += abs(mid - arr[i]) / D;
}
cout << count;
}
// Driver Code
int main()
{
// Given N & D
int N = 4, D = 2;
// Given array arr[]
int arr[] = { 2, 4, 6, 8 };
// Function Call
numOperation(arr, N, D);
}
Java
// Java program for the above approach
import java.util.*;
class GFG{
// Function to find minimum count of operations
// required to make all array elements equal by
// incrementing or decrementing array elements by d
static void numOperation(int arr[], int N, int D)
{
// Sort the array
Arrays.sort(arr);
// Traverse the array
for (int i = 0; i < N - 1; i++) {
// If difference between two consecutive
// elements are not divisible by D
if ((arr[i + 1] - arr[i]) % D != 0) {
System.out.println("-1");
return;
}
}
// Store minimum count of operations required to
// make all array elements equal by incrementing
// or decrementing array elements by d
int count = 0;
// Stores middle element
// of the array
int mid = arr[N / 2];
// Traverse the array
for (int i = 0; i < N; i++) {
// Update count
count += Math.abs(mid - arr[i]) / D;
}
System.out.println(count);
}
// Driver code
public static void main(String[] args)
{
// Given N & D
int N = 4, D = 2;
// Given array arr[]
int arr[] = { 2, 4, 6, 8 };
// Function Call
numOperation(arr, N, D);
}
}
// This code is contributed by code_hunt.
Python3
# Python 3 program for the above approach
# Function to find minimum count of operations
# required to make all array elements equal by
# incrementing or decrementing array elements by d
def numOperation(arr, N, D):
# Sort the array
arr.sort()
# Traverse the array
for i in range(N - 1):
# If difference between two consecutive
# elements are not divisible by D
if ((arr[i + 1] - arr[i]) % D != 0):
print("-1")
return
# Store minimum count of operations required to
# make all array elements equal by incrementing
# or decrementing array elements by d
count = 0
# Stores middle element
# of the array
mid = arr[N // 2]
# Traverse the array
for i in range(N):
# Update count
count += abs(mid - arr[i]) // D
print(count)
# Driver Code
if __name__ == "__main__":
# Given N & D
N = 4
D = 2
# Given array arr[]
arr = [2, 4, 6, 8]
# Function Call
numOperation(arr, N, D)
# This code is contributed by chitranayal
C#
// C# program for the above approach
using System;
class GFG
{
// Function to find minimum count of operations
// required to make all array elements equal by
// incrementing or decrementing array elements by d
static void numOperation(int[] arr, int N, int D)
{
// Sort the array
Array.Sort(arr);
// Traverse the array
for (int i = 0; i < N - 1; i++)
{
// If difference between two consecutive
// elements are not divisible by D
if ((arr[i + 1] - arr[i]) % D != 0)
{
Console.WriteLine("-1");
return;
}
}
// Store minimum count of operations required to
// make all array elements equal by incrementing
// or decrementing array elements by d
int count = 0;
// Stores middle element
// of the array
int mid = arr[N / 2];
// Traverse the array
for (int i = 0; i < N; i++)
{
// Update count
count += Math.Abs(mid - arr[i]) / D;
}
Console.WriteLine(count);
}
// Driver code
static public void Main()
{
// Given N & D
int N = 4, D = 2;
// Given array arr[]
int[] arr = new int[] { 2, 4, 6, 8 };
// Function Call
numOperation(arr, N, D);
}
}
// This code is contributed by Dharanendra L V
Javascript
输出:
4
时间复杂度: O(N * Log(N))
辅助空间: O(1)
如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程和学生竞争性编程现场课程。