给定一个数组, arr []由N个整数组成,任务是最小化使所有差值对偶数成对所需的数组元素的增量数量。
例子:
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.
输出:
1
时间复杂度: O(N)
辅助空间: O(1)