给定一个由N 个整数组成的数组arr[] ,任务是最小化数组元素的增量数量,以使数组元素的所有差异对相等。
例子:
Input: arr[] = {4, 1, 2}
Output: 1
Explanation:
Operation 1: Increment arr[1] by 1. The array arr[] modifies to {4, 2, 2}.
All pairs: (4, 2) → difference = 2
(4, 2) → difference = 2
(2, 2) → difference = 0
Now, the pairwise differences between array elements is even. Hence, the answer is 1.
Input: arr[] = {2, 4}
Output: 0
Explanation: Differences between all pairs of array elements is already even. Therefore, the answer is 0.
方法:给定的问题可以通过观察以下事实来解决,即要使所有数组元素对之间的差异均匀,两个元素必须具有相同的奇偶校验。因此,想法是将所有数组元素转换为偶数或奇数。最小增量数等于偶数和奇数数组元素的最小值。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to find the minimum increments
// required to difference between all
// pairs of array elements even
void minimumOperations(int arr[], int N)
{
// Store the count of odd
// and even numbers
int oddCnt = 0, evenCnt = 0;
// Traverse the array
for (int i = 0; i < N; i++) {
if (arr[i] % 2 == 0) {
// Increment evenCnt by 1
evenCnt++;
}
else {
// Increment eveCnt by 1
oddCnt++;
}
}
// Print the minimum of oddCnt
// and eveCnt
cout << min(oddCnt, evenCnt);
}
// Driver Code
int main()
{
int arr[] = { 4, 1, 2 };
int N = sizeof(arr) / sizeof(arr[0]);
minimumOperations(arr, N);
return 0;
}
Java
// Java program for the above approach
public class GFG
{
// Function to find the minimum increments
// required to difference between all
// pairs of array elements even
static void minimumOperations(int[] arr, int N)
{
// Store the count of odd
// and even numbers
int oddCnt = 0, evenCnt = 0;
// Traverse the array
for (int i = 0; i < N; i++)
{
if (arr[i] % 2 == 0)
{
// Increment evenCnt by 1
evenCnt++;
}
else
{
// Increment oddCnt by 1
oddCnt++;
}
}
// Print the minimum of oddCnt
// and eveCnt
System.out.print(Math.min(oddCnt, evenCnt));
}
// Driver code
public static void main(String args[])
{
int[] arr = { 4, 1, 2 };
int N = arr.length;
minimumOperations(arr, N);
}
}
// This code is contributed by AnkThon
Python3
# Python program for the above approach
# Function to find the minimum increments
# required to difference between all
# pairs of array elements even
def minimumOperations(arr, N) :
# Store the count of odd
# and even numbers
oddCnt = 0
evenCnt = 0
# Traverse the array
for i in range(N):
if (arr[i] % 2 == 0) :
# Increment evenCnt by 1
evenCnt += 1
else :
# Increment eveCnt by 1
oddCnt += 1
# Prthe minimum of oddCnt
# and eveCnt
print(min(oddCnt, evenCnt))
# Driver Code
arr = [ 4, 1, 2 ]
N = len(arr)
minimumOperations(arr, N)
# This code is contributed by sanjoy_62.
C#
// C# program for the above approach
using System;
using System.Collections.Generic;
class GFG
{
// Function to find the minimum increments
// required to difference between all
// pairs of array elements even
static void minimumOperations(int[] arr, int N)
{
// Store the count of odd
// and even numbers
int oddCnt = 0, evenCnt = 0;
// Traverse the array
for (int i = 0; i < N; i++)
{
if (arr[i] % 2 == 0)
{
// Increment evenCnt by 1
evenCnt++;
}
else
{
// Increment eveCnt by 1
oddCnt++;
}
}
// Print the minimum of oddCnt
// and eveCnt
Console.Write(Math.Min(oddCnt, evenCnt));
}
// Driver code
static void Main()
{
int[] arr = { 4, 1, 2 };
int N = arr.Length;
minimumOperations(arr, N);
}
}
// This code is contributed by divyeshrabadiya07.
Javascript
1
时间复杂度: O(N)
辅助空间: O(1)
如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live