# Minimum number of elements that should be removed to make the array good

Given an array **arr[]**, the task is to find the minimum number of elements that must be removed to make the array good. A sequence a_{1}, a_{2} … a_{n} is called good if for each element **a _{i}**, there exists an element

**a**(i not equals to j) such that

_{j}**a**is a

_{i}+ a_{j}**power of two**i.e. 2

^{d}for some non-negative integer d.

**Examples:**

Input:arr[] = {4, 7, 1, 5, 4, 9}

Output:1

Remove 5 from the array to make the array good.

Input:arr[] = {1, 3, 1, 1}

Output:0

**Approach:** We should delete only such **a _{i}** for which there is no

**a**(i not equals to j) such that

_{j}**a**is a power of 2.

_{i}+ a_{j}For each value let’s find the number of its occurrences in the array. We can use the map data-structure.

Now we can easily check that **a _{i}** doesn’t have a pair

**a**. Let’s iterate over all possible sums,

_{j}**S = 2**and for each

^{0}, 2^{1}, …, 2^{30}**S**calculate

**S – a[i]**whether it exists in the map.

Below is the implementation of the above approach :

## C++

`// C++ implementation of the approach ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to return the minimum number ` `// of elements that must be removed ` `// to make the given array good ` `int` `minimumRemoval(` `int` `n, ` `int` `a[]) ` `{ ` ` ` ` ` `map<` `int` `, ` `int` `> c; ` ` ` ` ` `// Count frequency of each element ` ` ` `for` `(` `int` `i = 0; i < n; i++) ` ` ` `c[a[i]]++; ` ` ` ` ` `int` `ans = 0; ` ` ` ` ` `// For each element check if there ` ` ` `// exists another element that makes ` ` ` `// a valid pair ` ` ` `for` `(` `int` `i = 0; i < n; i++) { ` ` ` `bool` `ok = ` `false` `; ` ` ` `for` `(` `int` `j = 0; j < 31; j++) { ` ` ` `int` `x = (1 << j) - a[i]; ` ` ` `if` `(c.count(x) && (c[x] > 1 ` ` ` `|| (c[x] == 1 && x != a[i]))) { ` ` ` `ok = ` `true` `; ` ` ` `break` `; ` ` ` `} ` ` ` `} ` ` ` ` ` `// If does not exist then ` ` ` `// increment answer ` ` ` `if` `(!ok) ` ` ` `ans++; ` ` ` `} ` ` ` ` ` `return` `ans; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `int` `a[] = { 4, 7, 1, 5, 4, 9 }; ` ` ` `int` `n = ` `sizeof` `(a) / ` `sizeof` `(a[0]); ` ` ` `cout << minimumRemoval(n, a); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 implementation of the approach ` ` ` `# Function to return the minimum number ` `# of elements that must be removed ` `# to make the given array good ` `def` `minimumRemoval(n, a) : ` ` ` ` ` `c ` `=` `dict` `.fromkeys(a, ` `0` `); ` ` ` ` ` `# Count frequency of each element ` ` ` `for` `i ` `in` `range` `(n) : ` ` ` `c[a[i]] ` `+` `=` `1` `; ` ` ` ` ` `ans ` `=` `0` `; ` ` ` ` ` `# For each element check if there ` ` ` `# exists another element that makes ` ` ` `# a valid pair ` ` ` `for` `i ` `in` `range` `(n) : ` ` ` `ok ` `=` `False` `; ` ` ` `for` `j ` `in` `range` `(` `31` `) : ` ` ` ` ` `x ` `=` `(` `1` `<< j) ` `-` `a[i]; ` ` ` `if` `(x ` `in` `c ` `and` `(c[x] > ` `1` `or` ` ` `(c[x] ` `=` `=` `1` `and` `x !` `=` `a[i]))) : ` ` ` ` ` `ok ` `=` `True` `; ` ` ` `break` `; ` ` ` ` ` `# If does not exist then ` ` ` `# increment answer ` ` ` `if` `(` `not` `ok) : ` ` ` `ans ` `+` `=` `1` `; ` ` ` ` ` `return` `ans; ` ` ` `# Driver Code ` `if` `__name__ ` `=` `=` `"__main__"` `: ` ` ` ` ` `a ` `=` `[ ` `4` `, ` `7` `, ` `1` `, ` `5` `, ` `4` `, ` `9` `]; ` ` ` `n ` `=` `len` `(a) ; ` ` ` ` ` `print` `(minimumRemoval(n, a)); ` ` ` `# This code is contributed by Ryuga ` |

*chevron_right*

*filter_none*

**Output:**

1

## Recommended Posts:

- Minimum number of elements to be removed to make XOR maximum
- Minimum number of elements to be removed so that pairwise consecutive elements are same
- Minimum number of elements to be replaced to make the given array a Fibonacci Sequence
- Minimum gcd operations to make all array elements one
- Make array elements equal in Minimum Steps
- Minimum operations required to make all the elements distinct in an array
- Minimum operations required to make all the array elements equal
- Minimum Increment / decrement to make array elements equal
- Minimum elements to be removed such that sum of adjacent elements is always odd
- Number of digits to be removed to make a number divisible by 3
- Print digit's position to be removed to make a number divisible by 6
- Minimum number of changes required to make the given array an AP
- Find minimum number of merge operations to make an array palindrome
- Find the number of operations required to make all array elements Equal
- Minimum number of increment/decrement operations such that array contains all elements from 1 to N

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.