给定整数数组arr [] ,任务是通过执行以下两个操作来找到使数组元素相等的最小步骤数–
- 用0的成本从元素中减去2
- 用1成本从元素中添加或减去1
例子:
Input: arr[] = {4, 3, 2, 1}
Output: 2
Explanation:
As in the above example all the array elements can be equal to the 4 or 3 with 2 as minimum cost
Cost of Making Array Elements equal to 4
Index 0: As index 0 is already equal to 4, the cost is 0
Index 1: 4 – 3 = 1, Adding 1 to the element will cost 1
Index 2: 4 – 2 = 2, Adding 2 to the element will cost 0
Index 3: 4 – 1 = 3, Add 2 and 1 once in the array element, which will cost 1, because 2 can be added any number of times with cost 0.
Total Cost = 1 + 1 = 2
Input: arr[] = {3, 2, 1}
Output: 1
Explanation:
As in the above example all the array elements can be made equal to 3 or 1 with 1 as minimum cost
Cost of Making Array Elements equal to 3
Index 0: As index 0 is already equal to 3, the cost is 0
Index 1: 3 – 2 = 1, Adding 1 to the element will cost 1
Index 2: 3 – 1 = 2, Adding 2 to the element will cost 0
Total Cost = 1
方法:这个想法是利用将2加到数组的任何元素上将花费0的事实。另外,第二个观察结果是,将2加到任何奇数上都会得到一个奇数,而类似地将2加到一个偶数上也会得到这个结果。仅以偶数为单位。因此,任何元素都可以以零成本等于最接近的整数,但具有相同的属性,即如果它的初始值是奇数,则它将保持为奇数,或者即使它的初始值是偶数,也将保留为零。因此,要找到最小成本,我们可以找到数组中奇数或偶数元素的最小计数。
算法:
- 查找数组中奇数或偶数元素的计数(例如count )。
- 在count和(length – count)之间找到最小值,其中length是数组的长度。
- 最小值将是使所有数组元素均等的期望成本。
举例说明:
Given Array be - {4, 2, 3, 1}
Count of Odd Elements - 2 ({3, 1})
Count of Even Elements - 2 ({4, 2})
Hence, the minimum cost required is 2 and
the array elements can be made equal
to any integer of even or odd value
下面是上述方法的实现:
C++
// C++ implementation to make
// array elements equal with
// minimum cost
#include
using namespace std;
// Function to find the minimum
// cost required to make array
// elements equal
void makearrayequal(int arr[], int n)
{
int x = 0;
// Loop to find the
// count of odd elements
for (int i = 0; i < n; i++) {
x += arr[i] & 1;
}
cout << min(x, n - x) << endl;
}
// Driver Code
int main()
{
int arr[] = { 4, 3, 2, 1 };
int n = sizeof(arr) / sizeof(arr[0]);
makearrayequal(arr, n);
return 0;
}
Java
// Java implementation to make
// array elements equal with
// minimum cost
class GFG {
// Function to find the minimum
// cost required to make array
// elements equal
static void makearrayequal(int arr[], int n)
{
int x = 0;
// Loop to find the
// count of odd elements
for (int i = 0; i < n; i++) {
x += (arr[i] & 1);
}
System.out.println(Math.min(x, n - x));
}
// Driver Code
public static void main (String[] args)
{
int arr[] = { 4, 3, 2, 1 };
int n = arr.length;
makearrayequal(arr, n);
}
}
// This code is contributed by Yash_R
Python3
# Python3 implementation to make
# array elements equal with
# minimum cost
# Function to find the minimum
# cost required to make array
# elements equal
def makearrayequal(arr, n) :
x = 0;
# Loop to find the
# count of odd elements
for i in range(n) :
x += arr[i] & 1;
print(min(x, n - x));
# Driver Code
if __name__ == "__main__" :
arr = [ 4, 3, 2, 1 ];
n = len(arr);
makearrayequal(arr, n);
# This code is contributed by Yash_R
C#
// C# implementation to make
// array elements equal with
// minimum cost
using System;
class GFG {
// Function to find the minimum
// cost required to make array
// elements equal
static void makearrayequal(int []arr, int n)
{
int x = 0;
// Loop to find the
// count of odd elements
for (int i = 0; i < n; i++) {
x += (arr[i] & 1);
}
Console.WriteLine(Math.Min(x, n - x));
}
// Driver Code
public static void Main (string[] args)
{
int []arr = { 4, 3, 2, 1 };
int n = arr.Length;
makearrayequal(arr, n);
}
}
// This code is contributed by Yash_R
Javascript
2
性能分析:
- 时间复杂度: O(N)。
- 辅助空间: O(1)