📌  相关文章
📜  使所有数组元素相等所需的最小D增量或减量

📅  最后修改于: 2021-04-24 21:48:42             🧑  作者: Mango

给定的阵列ARR []大小N和整数d中,任务是使所有数组元素通过增加或减少由d数组元素的最小数目相等。如果不可能使所有数组元素相等,则打印-1

例子 :

方法:想法是使用贪婪方法解决此问题。步骤如下:

  1. 以升序对给定数组进行排序。
  2. 观察到只有那些数组元素可以相等,它们与相邻索引元素的差可以完全被D整除,即(a [i + 1] – a [i])%D == 0
  3. 如果任何差异都不能被D整除,则打印-1,因为不能使所有数组元素相等。
  4. 现在,在这里使用贪婪的方法。为了使操作最少,请考虑使用mid元素,然后尝试将所有其他元素更改为mid 。找到一个数字转换所需要的操作数,即abs(mid – a [i])/ D。
  5. 最后,打印所需的最少操作数。

下面是上述方法的实现:

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


输出:
4

时间复杂度: O(N * Log(N))
辅助空间: O(1)