给定一个大小为N的数组arr[] ,任务是找到需要对数组元素执行的最小增量1,以便给定数组中偶数和奇数的计数变得相等。如果不可能,则打印“-1” 。
例子:
Input: arr[] = {1, 3, 4, 9}
Output: 1
Explanation:
Count of even and odd integers in the array are 1 and 3 respectively.
Increment arr[3] ( = 9) by 1 to make it 10(even).
So, as the count of even and odd integer are the same after the above steps. Hence, the minimum increment operations is 1.
Input: arr[] = {2, 2, 2, 2}
Output: 2
方法:解决给定问题的思路如下:
- 如果N是even ,则遍历数组并保留奇数和偶数整数的计数。偶数和奇数的计数的绝对差除以2给出了使偶数和奇数相等所需的最小增量运算。
- 如果N是奇数,则不可能使偶数和奇数相等,因此打印“-1” 。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to find min operations
// to make even and odd count equal
int minimumIncrement(int arr[], int N)
{
// Odd size will never make odd
// and even counts equal
if (N % 2 != 0) {
cout << "-1";
exit(0);
}
// Stores the count of even
// numbers in the array arr[]
int cntEven = 0;
// Stores count of odd numbers
// in the array arr[]
int cntOdd = 0;
// Traverse the array arr[]
for (int i = 0; i < N; i++) {
// If arr[i] is an
// even number
if (arr[i] % 2 == 0) {
// Update cntEven
cntEven += 1;
}
}
// Odd numbers in arr[]
cntOdd = N - cntEven;
// Return absolute difference
// divided by 2
return abs(cntEven - cntOdd) / 2;
}
// Driver Code
int main()
{
int arr[] = { 1, 3, 4, 9 };
int N = sizeof(arr) / sizeof(arr[0]);
// Function call
cout << minimumIncrement(arr, N);
return 0;
}
Java
// Java program for the above approach
import java.util.*;
import java.lang.*;
class GFG
{
// Function to find min operations
// to make even and odd count equal
static int minimumIncrement(int arr[], int N)
{
// Odd size will never make odd
// and even counts equal
if (N % 2 != 0)
{
System.out.println( "-1");
System.exit(0);
}
// Stores the count of even
// numbers in the array arr[]
int cntEven = 0;
// Stores count of odd numbers
// in the array arr[]
int cntOdd = 0;
// Traverse the array arr[]
for (int i = 0; i < N; i++)
{
// If arr[i] is an
// even number
if (arr[i] % 2 == 0)
{
// Update cntEven
cntEven += 1;
}
}
// Odd numbers in arr[]
cntOdd = N - cntEven;
// Return absolute difference
// divided by 2
return Math.abs(cntEven - cntOdd) / 2;
}
// Driver code
public static void main(String[] args)
{
int arr[] = { 1, 3, 4, 9 };
int N = arr.length;
// Function call
System.out.println(minimumIncrement(arr, N));
}
}
// This code is contributed by code_hunt.
Python3
# Python3 program for the above approach
# Function to find min operations
# to make even and odd count equal
def minimumIncrement(arr, N):
# Odd size will never make odd
# and even counts equal
if (N % 2 != 0):
print("-1")
return
# Stores the count of even
# numbers in the array arr[]
cntEven = 0
# Stores count of odd numbers
# in the array arr[]
cntOdd = 0
# Traverse the array arr[]
for i in range(N):
# If arr[i] is an
# even number
if (arr[i] % 2 == 0):
# Update cntEven
cntEven += 1
# Odd numbers in arr[]
cntOdd = N - cntEven
# Return absolute difference
# divided by 2
return abs(cntEven - cntOdd) // 2
# Driver Code
if __name__ == '__main__':
arr = [1, 3, 4, 9]
N = len(arr)
# Function call
print (minimumIncrement(arr, N))
# Thiss code is contributed by mohit kumar 29.
C#
// C# program to implement
// the above approach
using System;
class GFG
{
// Function to find min operations
// to make even and odd count equal
static int minimumIncrement(int[] arr, int N)
{
// Odd size will never make odd
// and even counts equal
if (N % 2 != 0)
{
Console.WriteLine( "-1");
Environment.Exit(0);
}
// Stores the count of even
// numbers in the array arr[]
int cntEven = 0;
// Stores count of odd numbers
// in the array arr[]
int cntOdd = 0;
// Traverse the array arr[]
for (int i = 0; i < N; i++)
{
// If arr[i] is an
// even number
if (arr[i] % 2 == 0)
{
// Update cntEven
cntEven += 1;
}
}
// Odd numbers in arr[]
cntOdd = N - cntEven;
// Return absolute difference
// divided by 2
return Math.Abs(cntEven - cntOdd) / 2;
}
// Driver Code
public static void Main()
{
int[] arr = { 1, 3, 4, 9 };
int N = arr.Length;
// Function call
Console.WriteLine(minimumIncrement(arr, N));
}
}
// This code is contributed by susmitakundugoaldanga.
Javascript
输出:
1
时间复杂度: O(N)
辅助空间: O(1)
如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live