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 that 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 (= 23). 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(= 21)
(1, 1023) with sum 1024(= 210)
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 = {20, 21, …., 230} and check if p – a[i] is present in the array or not.
- Either of the following two conditions needs 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 do not satisfy for any p for the current a[i], then increase the 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++
#include <bits/stdc++.h>
using namespace std;
int powerOfTwo( int a[], int n)
{
map< int , int > mp;
for ( int i = 0; i < n; i++)
mp[a[i]]++;
int count = 0;
for ( int i = 0; i < n; i++) {
bool f = false ;
for ( int j = 0; j < 31; j++) {
int s = (1 << j) - a[i];
if (mp.count(s)
&& (mp[s] > 1
|| mp[s] == 1 && s != a[i]))
f = true ;
}
if (f == false )
count++;
}
return count;
}
int main()
{
int a[] = { 6, 2, 11 };
int n = sizeof (a) / sizeof (a[0]);
cout << powerOfTwo(a, n);
return 0;
}
|
Java
import java.util.*;
class GFG{
static int powerOfTwo( int a[], int n)
{
HashMap<Integer,
Integer> mp = new HashMap<Integer,
Integer>();
for ( int i = 0 ; i < n; i++)
{
if (mp.containsKey(a[i]))
{
mp.put(a[i], mp.get(a[i]) + 1 );
}
else
{
mp.put(a[i], 1 );
}
}
int count = 0 ;
for ( int i = 0 ; i < n; i++)
{
boolean f = false ;
for ( int j = 0 ; j < 31 ; j++)
{
int s = ( 1 << j) - a[i];
if (mp.containsKey(s) &&
(mp.get(s) > 1 ||
mp.get(s) == 1 && s != a[i]))
f = true ;
}
if (f == false )
count++;
}
return count;
}
public static void main(String[] args)
{
int a[] = { 6 , 2 , 11 };
int n = a.length;
System.out.print(powerOfTwo(a, n));
}
}
|
Python3
from collections import defaultdict
def powerOfTwo(a, n):
mp = defaultdict ( int )
for i in range (n):
mp[a[i]] + = 1
count = 0
for i in range (n):
f = False
for j in range ( 31 ):
s = ( 1 << j) - a[i]
if (s in mp
and (mp[s] > 1
or mp[s] = = 1 and
s ! = a[i])):
f = True
if (f = = False ):
count + = 1
return count
if __name__ = = "__main__" :
a = [ 6 , 2 , 11 ]
n = len (a)
print (powerOfTwo(a, n))
|
C#
using System;
using System.Collections.Generic;
class GFG{
static int powerOfTwo( int []a, int n)
{
Dictionary< int ,
int > mp = new Dictionary< int ,
int >();
for ( int i = 0; i < n; i++)
{
if (mp.ContainsKey(a[i]))
{
mp[a[i]] = mp[a[i]] + 1;
}
else
{
mp.Add(a[i], 1);
}
}
int count = 0;
for ( int i = 0; i < n; i++)
{
bool f = false ;
for ( int j = 0; j < 31; j++)
{
int s = (1 << j) - a[i];
if (mp.ContainsKey(s) &&
(mp[s] > 1 ||
mp[s] == 1 && s != a[i]))
f = true ;
}
if (f == false )
count++;
}
return count;
}
public static void Main(String[] args)
{
int []a = { 6, 2, 11 };
int n = a.Length;
Console.Write(powerOfTwo(a, n));
}
}
|
Javascript
<script>
function powerOfTwo(a, n)
{
let mp = new Map();
for (let i = 0; i < n; i++)
{
if (mp.has(a[i]))
{
mp.set(a[i], mp.get(a[i]) + 1);
}
else
{
mp.set(a[i], 1);
}
}
let count = 0;
for (let i = 0; i < n; i++)
{
let f = false ;
for (let j = 0; j < 31; j++)
{
let s = (1 << j) - a[i];
if (mp.has(s) &&
(mp.get(s) > 1 ||
mp.get(s) == 1 && s != a[i]))
f = true ;
}
if (f == false )
count++;
}
return count;
}
let a = [ 6, 2, 11 ];
let n = a.length;
document.write(powerOfTwo(a, n));
</script>
|
Time Complexity: O(N*log(N))
Auxiliary Space: O(N)
Last Updated :
24 May, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...