# Count of elements which cannot form any pair whose sum is power of 2

Given an array **arr[]** of length **N**, the task is to print the number of array elements which cannot form a pair with any other array element whose sum is a power of two.

**Examples:**

Input:arr[] = {6, 2, 11}

Output:1

Explanation:

Since 6 and 2 can form a pair with sum 8 (= 2^{3}). So only 11 has to be removed as it does not form a sum which is a power of 2.

Input:arr[] = [1, 1, 1, 1023],

Output:0

Explanation:

The given array elements can be split into following two pairs:

(1, 1) with sum 2(= 2^{1})

(1, 1023) with sum 1024(= 2^{10})

Hence, no need to remove any element.

**Approach:**

To solve the problem mentioned above, follow the steps below:

- Store the frequencies of all array elements in a Map.
- For every array element a[i], iterate over all possible sums p = {2
^{0}, 2^{1}, …., 2^{30}} and check if**p – a[i]**is present in the array or not. - Either of the following two conditions need to be satisfied:
- Let s = p – a[i]. If s is present in the array more than once, then a pair consisting of a[i] with sum p is possible.
- If s is present only once in the array, then s has to be different from a[i] for a possible pair.
- If none of the above two conditions are satisfied, no pair consisting of a[i] is possible with sum p.

- If the above two conditions does not satisfy for any p for the current a[i], then increase
**count**as a[i] cannot form a sum which is a power of 2 with any other array element. - Print the final value of
**count**.

Below is the implementation of the above approach:

## C++

`// C++ Program to count of ` `// array elements which do ` `// not form a pair with sum ` `// equal to a power of 2 ` `// with any other array element ` ` ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to calculate ` `// and return the ` `// count of elements ` ` ` `int` `powerOfTwo(` `int` `a[], ` `int` `n) ` `{ ` ` ` `// Stores the frequencies ` ` ` `// of every array element ` ` ` ` ` `map<` `int` `, ` `int` `> mp; ` ` ` ` ` `for` `(` `int` `i = 0; i < n; i++) ` ` ` `mp[a[i]]++; ` ` ` ` ` `// Stores the count ` ` ` `// of removals ` ` ` ` ` `int` `count = 0; ` ` ` ` ` `for` `(` `int` `i = 0; i < n; i++) { ` ` ` `bool` `f = ` `false` `; ` ` ` ` ` `// For every element, check if ` ` ` `// it can form a sum equal to ` ` ` `// any power of 2 with any other ` ` ` `// element ` ` ` ` ` `for` `(` `int` `j = 0; j < 31; j++) { ` ` ` ` ` `// Store pow(2, j) - a[i] ` ` ` `int` `s = (1 << j) - a[i]; ` ` ` ` ` `// Check if s is present ` ` ` `// in the array ` ` ` `if` `(mp.count(s) ` ` ` ` ` `// If frequency of s ` ` ` `// exceeds 1 ` ` ` `&& (mp[s] > 1 ` ` ` ` ` `// If s has frequency 1 ` ` ` `// but is different from ` ` ` `// a[i] ` ` ` `|| mp[s] == 1 && s != a[i])) ` ` ` ` ` `// Pair possible ` ` ` `f = ` `true` `; ` ` ` `} ` ` ` ` ` `// If no pair possible for ` ` ` `// the current element ` ` ` ` ` `if` `(f == ` `false` `) ` ` ` `count++; ` ` ` `} ` ` ` ` ` `// Return the answer ` ` ` `return` `count; ` `} ` ` ` `// Driver Code ` `int` `main() ` `{ ` ` ` `int` `a[] = { 6, 2, 11 }; ` ` ` `int` `n = ` `sizeof` `(a) / ` `sizeof` `(a[0]); ` ` ` `cout << powerOfTwo(a, n); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

**Output:**

1

**Time Complexity:** O(N)

**Auxiliary Space:** O(N)

Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.

## Recommended Posts:

- Count pair of strings whose concatenation of substrings form a palindrome
- Count of elements which are power of 2 in a given range subarray for Q queries
- Count of elements which is product of a pair or an element square
- Count of elements to be multiplied with integers to make each pair of Array a perfect square
- Count of sub-arrays whose elements can be re-arranged to form palindromes
- Count of elements which form a loop in an Array according to given constraints
- Find the sum of power of bit count raised to the power B
- Pair of integers having difference of their fifth power as X
- Length of array pair formed where one contains all distinct elements and other all same elements
- Count subarrays such that remainder after dividing sum of elements by K gives count of elements
- Array containing power of 2 whose XOR and Sum of elements equals X
- Count of pairs whose bitwise AND is a power of 2
- Count of even and odd power pairs in an Array
- Count of numbers which can be made power of 2 by given operation
- Count pairs in array such that one element is power of another
- Count unordered pairs (i,j) such that product of a[i] and a[j] is power of two
- Elements of Array which can be expressed as power of prime numbers
- Elements of Array which can be expressed as power of some integer to given exponent K
- Smallest power of 2 which is greater than or equal to sum of array elements
- Count pairs in Array whose product is a Kth power of any positive integer

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.