给定一个大小为N的数组arr[] ,任务是通过选择任意两个不同的索引来打印使所有数组元素相等所需的最小移动次数,然后在第一个选定索引处增加元素并在另一个索引处减少元素在每次移动中选择索引1 。如果不可能使所有数组元素相等,则打印“ -1 ”。
例子:
Input: arr[] = {5, 4, 1, 10}
Output: 5
Explanation:
One of the possible way to perform operation is:
Operation 1: Select the indices 1 and 3 and then increment arr[1] by 1 and decrement arr[3] by 1. Thereafter, the array modifies to {5, 5, 1, 9}.
Operation 2: Select the indices 2 and 3 and then increment arr[2] by 1 and decrement arr[3] by 1. Thereafter, the array modifies to {5, 5, 2, 8}.
Operation 3: Select the indices 2 and 3 and then increment arr[2] by 1 and decrement arr[3] by 1. Thereafter, the array modifies to {5, 5, 3, 7}.
Operation 4: Select the indices 2 and 3 and then increment arr[2] by 1 and decrement arr[3] by 1. Thereafter, the array modifies to {5, 5, 4, 6}.
Operation 5: Select the indices 2 and 3 and then increment arr[2] by 1 and decrement arr[3] by 1. Thereafter, the array modifies to {5, 5, 5, 5}.
Therefore, the total number of move needed is 5. Also, it is the minimum possible moves needed.
Input: arr[] = {1, 4}
Output: -1
方法:根据以下观察可以解决给定的问题:
- It can be observed that in one move the sum of the array remains the same therefore, if the sum of the array is not divisible N then it is impossible to make all the array elements equal.
- Otherwise, each array element will be equal to the sum of the array divided by N.
- Therefore, the idea is to use the two pointer technique to find the minimum count of moves needed to make all the array elements equal to the sum/N.
请按照以下步骤解决问题:
- 初始化一个变量,比如ans为0 ,以存储所需移动的计数。
- 找到数组的总和并将其存储在一个变量中,比如sum 。
- 现在,如果总和不能被N整除,则打印“ -1 ”。否则,将总和更新为sum = sum/N。
- 按升序对数组进行排序。
- 初始化变量,比如i为0和j为N – 1以迭代数组。
- 迭代直到i小于 j 并执行以下步骤:
- 如果将arr[i]增加到sum小于将arr[j]减少到sum ,则将sum – arr[i] 添加到ans ,然后更新arr[i]和arr[j] ,然后将i增加1 。
- 否则,将arr[j] – sum添加到ans ,并更新arr[i]和arr[j] ,然后将j减1。
- 最后,完成上述步骤后,打印ans中存储的值。
下面是上述方法的实现:
C++
// C++ Program for the above approach
#include
#include
using namespace std;
// Function to find the minimum
// operations to make the array
// elements equal
int find(int arr[], int N)
{
// Stores the sum of the
// array
int Sum = 0;
for (int i = 0; i < N; i++) {
Sum += arr[i];
}
if (Sum % N) {
return -1;
}
// update sum
Sum /= N;
// sort array
sort(arr, arr + N);
// Store the minimum
// needed moves
int ans = 0;
int i = 0, j = N - 1;
// Iterate until i
// is less than j
while (i < j) {
if (Sum - arr[i] < arr[j] - Sum) {
// Increment ans by
// Sum-arr[i]
ans += (Sum - arr[i]);
// update
arr[i] += (Sum - arr[i]);
arr[j] -= (Sum - arr[i]);
// Increment i by 1
i++;
}
else {
// Increment ans by
//arr[j]-Sum
ans += (arr[j] - Sum);
// Update
arr[i] += (arr[j] - Sum);
arr[j] -= (arr[j] - Sum);
// Decrement j by 1
j--;
}
}
// Return the value in ans
return ans;
}
// Driver code
int main()
{
// Given input
int arr[] = { 5, 4, 1, 10 };
int N = sizeof(arr) / sizeof(int);
// Function call
cout << find(arr, N);
return 0;
}
// This code is contributed by Parth Manchanda
Java
import java.util.Arrays;
// Java Program for the above approach
class GFG {
// Function to find the minimum
// operations to make the array
// elements equal
public static int find(int arr[], int N)
{
// Stores the sum of the
// array
int Sum = 0;
for (int i = 0; i < N; i++) {
Sum += arr[i];
}
if (Sum % N > 0) {
return -1;
}
// update sum
Sum /= N;
// sort array
Arrays.sort(arr);
// Store the minimum
// needed moves
int ans = 0;
int i = 0, j = N - 1;
// Iterate until i
// is less than j
while (i < j) {
if (Sum - arr[i] < arr[j] - Sum) {
// Increment ans by
// Sum-arr[i]
ans += (Sum - arr[i]);
// update
arr[i] += (Sum - arr[i]);
arr[j] -= (Sum - arr[i]);
// Increment i by 1
i++;
} else {
// Increment ans by
// arr[j]-Sum
ans += (arr[j] - Sum);
// Update
arr[i] += (arr[j] - Sum);
arr[j] -= (arr[j] - Sum);
// Decrement j by 1
j--;
}
}
// Return the value in ans
return ans;
}
// Driver code
public static void main(String args[]) {
// Given input
int arr[] = { 5, 4, 1, 10 };
int N = arr.length;
// Function call
System.out.println(find(arr, N));
}
}
// This code is contributed by gfgking
Python3
# Python program for the above approach
# Function to find the minimum
# operations to make the array
# elements equal
def find(arr, N):
# Stores the sum of the
# array
Sum = sum(arr)
# If sum is not divisible
# by N
if Sum % N:
return -1
else:
# Update sum
Sum //= N
# Sort the array
arr.sort()
# Store the minimum
# needed moves
ans = 0
i = 0
j = N-1
# Iterate until i
# is less than j
while i < j:
# If the Sum-arr[i]
# is less than the
# arr[j]-sum
if Sum-arr[i] < arr[j]-Sum:
# Increment ans by
# Sum-arr[i]
ans += Sum-arr[i]
# Update
arr[i] += Sum-arr[i]
arr[j] -= Sum-arr[i]
# Increment i by 1
i += 1
# Otherwise,
else:
# Increment ans by
# arr[j]-Sum
ans += arr[j]-Sum
# Update
arr[i] += arr[j]-Sum
arr[j] -= arr[j]-Sum
# Decrement j by 1
j -= 1
# Return the value in ans
return ans
# Driver Code
if __name__ == '__main__':
# Given Input
arr = [5, 4, 1, 10]
N = len(arr)
# Function Call
print(find(arr, N))
C#
// C# program for the above approach
using System;
class GFG{
// Function to find the minimum
// operations to make the array
// elements equal
public static int find(int[] arr, int N)
{
// Stores the sum of the
// array
int Sum = 0;
int i = 0;
for(i = 0; i < N; i++)
{
Sum += arr[i];
}
if (Sum % N > 0)
{
return -1;
}
// update sum
Sum /= N;
// sort array
Array.Sort(arr);
// Store the minimum
// needed moves
int ans = 0;
i = 0;
int j = N - 1;
// Iterate until i
// is less than j
while (i < j)
{
if (Sum - arr[i] < arr[j] - Sum)
{
// Increment ans by
// Sum-arr[i]
ans += (Sum - arr[i]);
// update
arr[i] += (Sum - arr[i]);
arr[j] -= (Sum - arr[i]);
// Increment i by 1
i++;
}
else
{
// Increment ans by
// arr[j]-Sum
ans += (arr[j] - Sum);
// Update
arr[i] += (arr[j] - Sum);
arr[j] -= (arr[j] - Sum);
// Decrement j by 1
j--;
}
}
// Return the value in ans
return ans;
}
// Driver code
static public void Main()
{
// Given input
int[] arr = { 5, 4, 1, 10 };
int N = arr.Length;
// Function call
Console.WriteLine(find(arr, N));
}
}
// This code is contributed by target_2
Javascript
5
时间复杂度: O(N*log(N))
辅助空间: O(1)
如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程和学生竞争性编程现场课程。