给定N个元素的数组arr [] 。在任何步骤,我们都可以删除与前一个步骤不同的奇偶校验数字,即,如果在上一步中删除了奇数,则在当前步骤中删除偶数,反之亦然。
允许通过删除任何数字来开始。删除是可能的,直到我们可以在每个步骤删除不同奇偶校验的数字为止。任务是找到末尾剩余元素的最小和。
例子:
Input: arr[] = {1, 5, 7, 8, 2}
Output: 0
Delete elements in the order 1, 2, 5, 8 and finally 7.
There are multiple ways of deletion,
resulting in the same minimized sum.
Input: arr[] = {2, 2, 2, 2}
Output: 6
Delete 2 in first step.
Cannot delete any number, since there are no odd numbers left.
Hence, the leftover elements sum is 6.
方法:可以通过以下方法解决上述问题:
- 计算奇数和偶数元素的数量,并将其存储在向量v1和v2中。
- 检查奇数和偶数元素的数量是否相同或相差1 ,那么我们可以执行N步,结果剩余总和为0。
- 如果大小相差大于1,则只有剩余元素。
- 为了最小化元素的剩余总和,我们首先选择较大的元素。
- 因此,X个较小元素的总和将成为答案,其中X是v2.size()– v1.size()– 1或v1.size()– v2.size()– 1(取决于偶数)和奇数元素。
下面是上述方法的实现:
C++
// C++ implementation of the approach
#include
using namespace std;
// Function to find the minimized sum
int MinimizeleftOverSum(int a[], int n)
{
vector v1, v2;
for (int i = 0; i < n; i++) {
if (a[i] % 2)
v1.push_back(a[i]);
else
v2.push_back(a[i]);
}
// If more odd elements
if (v1.size() > v2.size()) {
// Sort the elements
sort(v1.begin(), v1.end());
sort(v2.begin(), v2.end());
// Left-over elements
int x = v1.size() - v2.size() - 1;
int sum = 0;
int i = 0;
// Find the sum of leftover elements
while (i < x) {
sum += v1[i++];
}
// Return the sum
return sum;
}
// If more even elements
else if (v2.size() > v1.size()) {
// Sort the elements
sort(v1.begin(), v1.end());
sort(v2.begin(), v2.end());
// Left-over elements
int x = v2.size() - v1.size() - 1;
int sum = 0;
int i = 0;
// Find the sum of leftover elements
while (i < x) {
sum += v2[i++];
}
// Return the sum
return sum;
}
// If same elements
else
return 0;
}
// Driver code
int main()
{
int a[] = { 2, 2, 2, 2 };
int n = sizeof(a) / sizeof(a[0]);
cout << MinimizeleftOverSum(a, n);
return 0;
}
Java
// Java implementation of the approach
import java.util.*;
class GFG
{
// Function to find the minimized sum
static int MinimizeleftOverSum(int a[], int n)
{
Vector v1 = new Vector(),
v2 = new Vector();
for (int i = 0; i < n; i++)
{
if (a[i] % 2 == 1)
v1.add(a[i]);
else
v2.add(a[i]);
}
// If more odd elements
if (v1.size() > v2.size())
{
// Sort the elements
Collections.sort(v1);
Collections.sort(v2);
// Left-over elements
int x = v1.size() - v2.size() - 1;
int sum = 0;
int i = 0;
// Find the sum of leftover elements
while (i < x)
{
sum += v1.get(i++);
}
// Return the sum
return sum;
}
// If more even elements
else if (v2.size() > v1.size())
{
// Sort the elements
Collections.sort(v1);
Collections.sort(v2);
// Left-over elements
int x = v2.size() - v1.size() - 1;
int sum = 0;
int i = 0;
// Find the sum of leftover elements
while (i < x)
{
sum += v2.get(i++);
}
// Return the sum
return sum;
}
// If same elements
else
return 0;
}
// Driver code
public static void main(String[] args)
{
int a[] = { 2, 2, 2, 2 };
int n = a.length;
System.out.println(MinimizeleftOverSum(a, n));
}
}
// This code is contributed by Princi Singh
Python3
# Python3 implementation of the approach
# Function to find the minimized sum
def MinimizeleftOverSum(a, n) :
v1, v2 = [], [];
for i in range(n) :
if (a[i] % 2) :
v1.append(a[i]);
else :
v2.append(a[i]);
# If more odd elements
if (len(v1) > len(v2)) :
# Sort the elements
v1.sort();
v2.sort();
# Left-over elements
x = len(v1) - len(v2) - 1;
sum = 0;
i = 0;
# Find the sum of leftover elements
while (i < x) :
sum += v1[i];
i += 1
# Return the sum
return sum;
# If more even elements
elif (len(v2) > len(v1)) :
# Sort the elements
v1.sort();
v2.sort();
# Left-over elements
x = len(v2) - len(v1) - 1;
sum = 0;
i = 0;
# Find the sum of leftover elements
while (i < x) :
sum += v2[i];
i += 1
# Return the sum
return sum;
# If same elements
else :
return 0;
# Driver code
if __name__ == "__main__" :
a = [ 2, 2, 2, 2 ];
n = len(a);
print(MinimizeleftOverSum(a, n));
# This code is contributed by Ryuga
C#
// C# implementation of the approach
using System;
using System.Collections.Generic;
class GFG
{
// Function to find the minimized sum
static int MinimizeleftOverSum(int []a,
int n)
{
List v1 = new List(),
v2 = new List();
for (int i = 0; i < n; i++)
{
if (a[i] % 2 == 1)
v1.Add(a[i]);
else
v2.Add(a[i]);
}
// If more odd elements
if (v1.Count > v2.Count)
{
// Sort the elements
v1.Sort();
v2.Sort();
// Left-over elements
int x = v1.Count - v2.Count - 1;
int sum = 0;
int i = 0;
// Find the sum of leftover elements
while (i < x)
{
sum += v1[i++];
}
// Return the sum
return sum;
}
// If more even elements
else if (v2.Count > v1.Count)
{
// Sort the elements
v1.Sort();
v2.Sort();
// Left-over elements
int x = v2.Count - v1.Count - 1;
int sum = 0;
int i = 0;
// Find the sum of leftover elements
while (i < x)
{
sum += v2[i++];
}
// Return the sum
return sum;
}
// If same elements
else
return 0;
}
// Driver code
public static void Main(String[] args)
{
int []a = { 2, 2, 2, 2 };
int n = a.Length;
Console.WriteLine(MinimizeleftOverSum(a, n));
}
}
// This code is contributed by PrinciRaj1992
输出:
6