给定一个包含 N 个整数的数组。任务是消除最小数量的元素,使得在结果数组中任意两个相邻值的总和为奇数。
例子:
Input: arr[] = {1, 2, 3}
Output: 0
Sum of all adjacent elements is already odd.
Input: arr[] = {1, 3, 5, 4, 2}
Output: 3
Eliminate 3, 5 and 2 so that in the resulting array the sum of any two adjacent values is odd.
方法两个数之和是奇数,如果其中一个是奇数,另一个是偶数。这意味着对于每对具有相同奇偶性的连续数字,消除其中一个,无论哪个都无关紧要。所以下面的贪心算法是有效的:
- 按顺序遍历所有元素。
- 如果当前数字与前一个数字具有相同的奇偶校验,则消除它,否则不要。
下面是上述方法的实现:
C++
// C++ implementation of the above approach
#include
using namespace std;
// Returns the minimum number of eliminations
int min_elimination(int n, int arr [])
{
int count = 0;
// Stores the previous element
int prev_val = arr[0];
// Stores the new value
for (int i = 1; i < n; i++)
{
int curr_val = arr[i];
// Check if the previous and current
// values are of same parity
if (curr_val % 2 == prev_val % 2)
count++;
// Previous value is now the current value
prev_val = curr_val;
}
// Return the counter variable
return count;
}
// Driver code
int main()
{
int arr [] = { 1, 2, 3, 7, 9 };
int n = sizeof(arr)/sizeof(arr[0]);
cout << min_elimination(n, arr);
return 0;
}
// This code is contributed by ihritik
Java
// Java implementation of the above approach
class GFG {
// Returns the minimum number of eliminations
static int min_elimination(int n, int[] arr)
{
int count = 0;
// Stores the previous element
int prev_val = arr[0];
// Stores the new value
for (int i = 1; i < n; i++) {
int curr_val = arr[i];
// Check if the previous and current
// values are of same parity
if (curr_val % 2 == prev_val % 2)
count++;
// Previous value is now the current value
prev_val = curr_val;
}
// Return the counter variable
return count;
}
// Driver code
public static void main(String[] args)
{
int[] arr = new int[] { 1, 2, 3, 7, 9 };
int n = arr.length;
System.out.println(min_elimination(n, arr));
}
}
Python3
# Python3 implementation of the above approach
# Returns the minimum number of eliminations
def min_elimination(n, arr):
count = 0
# Stores the previous element
prev_val = arr[0]
# Stores the new value
for i in range (1, n):
curr_val = arr[i];
# Check if the previous and current
# values are of same parity
if (curr_val % 2 == prev_val % 2):
count = count + 1
# Previous value is now the current value
prev_val = curr_val
# Return the counter variable
return count
# Driver code
arr = [ 1, 2, 3, 7, 9 ]
n = len(arr)
print(min_elimination(n, arr));
# This code is contributed by ihritik
C#
// C# implementation of the above approach
using System;
class GFG
{
// Returns the minimum number of eliminations
static int min_elimination(int n, int[] arr)
{
int count = 0;
// Stores the previous element
int prev_val = arr[0];
// Stores the new value
for (int i = 1; i < n; i++)
{
int curr_val = arr[i];
// Check if the previous and current
// values are of same parity
if (curr_val % 2 == prev_val % 2)
count++;
// Previous value is now the current value
prev_val = curr_val;
}
// Return the counter variable
return count;
}
// Driver code
public static void Main()
{
int[] arr = new int[] { 1, 2, 3, 7, 9 };
int n = arr.Length;
Console.WriteLine(min_elimination(n, arr));
}
}
// This code is contributed by ihritik
PHP
Javascript
输出:
2
时间复杂度: O(N)
辅助空间: O(1)
如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程和学生竞争性编程现场课程。