用和重复替换偶数对后的最小数组大小
给定一个大小为 N 的数组。从序列中选择一对随机元素,使它们的和为偶数,从序列中删除这两个元素并将它们的和插入序列中,以最小化数组的长度。最后,打印数组的最小可能大小。
例子 :
Input : 88 98 1 7 3
Output : 2
By following above rules --[88, 98, 1, 10]
[98, 88, 1]---[186, 1]--we cannot move further since
186 + 1 = 187, which is not even. So, size = 2.
Input : 7 4 3 2 6
Output : 1
delete 7 and 3, insert 10---[10, 4, 2, 6]
repeating the process of deleting and inserting---[14, 8]--[22]
size of array becomes 1.
方法:
如果两个数字都是偶数或它们都是奇数,则一对数字可以加起来为偶数。因此,我们只需要计算给定数组中存在的奇数。答案可以是 2 或 1(仅此而已),具体取决于条件。如果数组中的总赔率是奇数,则打印 2,否则打印 1。
C++
// CPP Program to find minimum size of array
// after repeatedly replacing even sum pair
// with the sum
#include
using namespace std;
// function to count and check
// total number of odds
bool check(int a[], int n)
{
int i, c = 0;
// loop to count total no. of
// odd numbers in the array
if (n == 1)
return false;
for (i = 0; i < n; i++)
if (a[i] & 1)
c++;
if (c & 1)
return true;
return false;
}
// Driver program to test
// above function
int main()
{
int n, a[] = { 7, 4, 3, 2, 6 };
n = sizeof(a) / sizeof(a[0]);
// if in total there are
// odd number of odds
if (check(a, n))
cout << 2;
else
cout << 1;
return 0;
}
Java
// Java Program to find minimum size of array
// after repeatedly replacing even sum pair
// with the sum
import java.io.*;
import java.math.*;
class GFG {
// function to count and check
// total number of odds
static boolean check(int a[], int n)
{
int i, c = 0;
// loop to count total no. of
// odd numbers in the array
if (n == 1)
return false;
for (i = 0; i < n; i++)
if ((a[i] & 1)==1)
c++;
if ((c & 1) == 1)
return true;
return false;
}
// Driver program to test
// above function
public static void main(String args[])
{
int n, a[] = { 7, 4, 3, 2, 6 };
n = a.length;
// if in total there are
// odd number of odds
if (check(a, n))
System.out.println("2");
else
System.out.println("1");
}
}
// This code is contributed by Nikita Tiwari.
Python 3
# Python 3 Program to illustrate
# the above approach
# function to count and check
# total number of odds
def check(a):
c = 0
# if length is 1, then we return
# false in order to get output 1
if len(a) == 1:
return False
# loop to count number of odds
for i in a:
if i & 1:
c += 1
# if c is odd
if c & 1:
return True
return False
# Driver program to test above function
a = [7, 4, 3, 2, 6]
# if in total there are odd
# number of odd numbers
if(check(a)):
print(2)
else:
print(1)
C#
// C# Program to find minimum size of array
// after repeatedly replacing even sum pair
// with the sum
using System;
class GFG {
// function to count and check
// total number of odds
static bool check(int []a, int n)
{
int i, c = 0;
// loop to count total no. of
// odd numbers in the array
if (n == 1)
return false;
for (i = 0; i < n; i++)
if ((a[i] & 1)==1)
c++;
if ((c & 1) == 1)
return true;
return false;
}
// Driver program
public static void Main()
{
int n;
int []a = { 7, 4, 3, 2, 6 };
n = a.Length;
// if in total there are
// odd number of odds
if (check(a, n))
Console.WriteLine("2");
else
Console.WriteLine("1");
}
}
// This code is contributed by vt_m.
PHP
Javascript
输出 :
1
时间复杂度O(n)
空间复杂度O(1)。