# 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 8.

**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 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++

`// C++ implementation of the approach ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to find the minimized sum ` `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 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; ` `} ` |

*chevron_right*

*filter_none*

## 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 ` |

*chevron_right*

*filter_none*

**Output:**

6

## Recommended Posts:

- Average of remaining elements after removing K largest and K smallest elements from array
- Minimum number of elements to be removed such that the sum of the remaining elements is equal to k
- Choose k array elements such that difference of maximum and minimum is minimized
- Element equal to the sum of all the remaining elements
- Check if the array has an element which is equal to sum of all the remaining elements
- Check if the array has an element which is equal to product of remaining elements
- Alternate Fibonacci Numbers
- Sum of Fibonacci Numbers with alternate negatives
- Choose X such that (A xor X) + (B xor X) is minimized
- Find a point such that sum of the Manhattan distances is minimized
- Area of plot remaining at the end
- Reach A and B by multiplying them with K and K^2 at every step
- Minimum step to reach one
- Minimum steps in which N can be obtained using addition or subtraction at every step
- Steps to reduce N to zero by subtracting its most significant digit at every step

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.