Delete odd and even numbers at alternate step such that sum of remaining elements is minimized
Given an array arr[] of N elements. At any step, we can delete a number of a different parity from the just previous step, i.e., if, at the previous step, an odd number was deleted then delete an even number in the current step or vice versa.
It is allowed to start by deleting any number. Deletion is possible till we can delete numbers of different parity at every step. The task is to find the minimum possible sum of the elements left at the end.
Examples:
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.
Approach: The following ways can be followed to solve the above problem:
- Count the number of odd and even elements and store in vectors v1 and v2.
- Check if the number of odd and even elements are the same or differ by 1, then we can perform N steps, resulting in leftover sum as 0.
- If the size differs by more than 1, then only there will be leftover elements.
- In order to minimize the leftover sum of elements, we select the larger elements first.
- Hence, the sum of the X smaller elements will be the answer, where X is v2.size() – v1.size() – 1 or v1.size() – v2.size() – 1 depending on the count of even and odd elements.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int MinimizeleftOverSum( int a[], int n)
{
vector< int > 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 (v1.size() > v2.size()) {
sort(v1.begin(), v1.end());
sort(v2.begin(), v2.end());
int x = v1.size() - v2.size() - 1;
int sum = 0;
int i = 0;
while (i < x) {
sum += v1[i++];
}
return sum;
}
else if (v2.size() > v1.size()) {
sort(v1.begin(), v1.end());
sort(v2.begin(), v2.end());
int x = v2.size() - v1.size() - 1;
int sum = 0;
int i = 0;
while (i < x) {
sum += v2[i++];
}
return sum;
}
else
return 0;
}
int main()
{
int a[] = { 2, 2, 2, 2 };
int n = sizeof (a) / sizeof (a[0]);
cout << MinimizeleftOverSum(a, n);
return 0;
}
|
Java
import java.util.*;
class GFG
{
static int MinimizeleftOverSum( int a[], int n)
{
Vector<Integer> v1 = new Vector<Integer>(),
v2 = new Vector<Integer>();
for ( int i = 0 ; i < n; i++)
{
if (a[i] % 2 == 1 )
v1.add(a[i]);
else
v2.add(a[i]);
}
if (v1.size() > v2.size())
{
Collections.sort(v1);
Collections.sort(v2);
int x = v1.size() - v2.size() - 1 ;
int sum = 0 ;
int i = 0 ;
while (i < x)
{
sum += v1.get(i++);
}
return sum;
}
else if (v2.size() > v1.size())
{
Collections.sort(v1);
Collections.sort(v2);
int x = v2.size() - v1.size() - 1 ;
int sum = 0 ;
int i = 0 ;
while (i < x)
{
sum += v2.get(i++);
}
return sum;
}
else
return 0 ;
}
public static void main(String[] args)
{
int a[] = { 2 , 2 , 2 , 2 };
int n = a.length;
System.out.println(MinimizeleftOverSum(a, n));
}
}
|
Python3
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 ( len (v1) > len (v2)) :
v1.sort();
v2.sort();
x = len (v1) - len (v2) - 1 ;
sum = 0 ;
i = 0 ;
while (i < x) :
sum + = v1[i];
i + = 1
return sum ;
elif ( len (v2) > len (v1)) :
v1.sort();
v2.sort();
x = len (v2) - len (v1) - 1 ;
sum = 0 ;
i = 0 ;
while (i < x) :
sum + = v2[i];
i + = 1
return sum ;
else :
return 0 ;
if __name__ = = "__main__" :
a = [ 2 , 2 , 2 , 2 ];
n = len (a);
print (MinimizeleftOverSum(a, n));
|
C#
using System;
using System.Collections.Generic;
class GFG
{
static int MinimizeleftOverSum( int []a,
int n)
{
List< int > v1 = new List< int >(),
v2 = new List< int >();
for ( int i = 0; i < n; i++)
{
if (a[i] % 2 == 1)
v1.Add(a[i]);
else
v2.Add(a[i]);
}
if (v1.Count > v2.Count)
{
v1.Sort();
v2.Sort();
int x = v1.Count - v2.Count - 1;
int sum = 0;
int i = 0;
while (i < x)
{
sum += v1[i++];
}
return sum;
}
else if (v2.Count > v1.Count)
{
v1.Sort();
v2.Sort();
int x = v2.Count - v1.Count - 1;
int sum = 0;
int i = 0;
while (i < x)
{
sum += v2[i++];
}
return sum;
}
else
return 0;
}
public static void Main(String[] args)
{
int []a = { 2, 2, 2, 2 };
int n = a.Length;
Console.WriteLine(MinimizeleftOverSum(a, n));
}
}
|
Javascript
<script>
function MinimizeleftOverSum(a , n)
{
var v1 = [], v2 =[];
for (i = 0; i < n; i++)
{
if (a[i] % 2 == 1)
v1.push(a[i]);
else
v2.push(a[i]);
}
if (v1.length > v2.length)
{
v1.sort();
v2.sort();
var x = v1.length - v2.length - 1;
var sum = 0;
var i = 0;
while (i < x)
{
sum += v1[i++];
}
return sum;
}
else if (v2.length > v1.length)
{
v1.sort();
v2.sort();
var x = v2.length - v1.length - 1;
var sum = 0;
var i = 0;
while (i < x)
{
sum += v2[i++];
}
return sum;
}
else
return 0;
}
var a = [ 2, 2, 2, 2 ];
var n = a.length;
document.write(MinimizeleftOverSum(a, n));
</script>
|
Time Complexity: O(n * log n), as in the worst case we will be using an inbuilt sort function to sort an array of size n. Where N is the number of elements in the array.
Auxiliary Space: O(n), as we are using extra space for the array v1 and v2. Where N is the number of elements in the array.
Last Updated :
21 Jun, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...