给定长度为N的数组arr[] ,任务是打印不能与总和为 2 的幂的任何其他数组元素成对的数组元素的数量。
例子:
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.
方法:
要解决上述问题,请按照以下步骤操作:
- 将所有数组元素的频率存储在 Map 中。
- 对于每个数组元素 a[i],迭代所有可能的和 p = {2 0 , 2 1 , …., 2 30 } 并检查p – a[i]是否存在于数组中。
- 需要满足以下两个条件之一:
- 让 s = p – a[i]。如果 s 在数组中出现不止一次,那么由 a[i] 和 p 组成的对是可能的。
- 如果 s 在数组中只出现一次,那么对于可能的对, s 必须与 a[i] 不同。
- 如果以上两个条件都不满足,则不可能有由 a[i] 组成的对和 p。
- 如果当前 a[i] 的任何 p 都不满足上述两个条件,则增加计数,因为 a[i] 不能与任何其他数组元素形成 2 的幂的和。
- 打印count的最终值。
下面是上述方法的实现:
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
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 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;
}
Java
// Java 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
import java.util.*;
class GFG{
// Function to calculate and return
// the count of elements
static int powerOfTwo(int a[], int n)
{
// Stores the frequencies
// of every array element
HashMap mp = new HashMap();
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);
}
}
// Stores the count
// of removals
int count = 0;
for(int i = 0; i < n; i++)
{
boolean 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 Math.pow(2, j) - a[i]
int s = (1 << j) - a[i];
// Check if s is present
// in the array
if (mp.containsKey(s) &&
// If frequency of s
// exceeds 1
(mp.get(s) > 1 ||
// If s has frequency 1
// but is different from
// a[i]
mp.get(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
public static void main(String[] args)
{
int a[] = { 6, 2, 11 };
int n = a.length;
System.out.print(powerOfTwo(a, n));
}
}
// This code is contributed by Amit Katiyar
Python3
# Python3 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
from collections import defaultdict
# Function to calculate
# and return the
# count of elements
def powerOfTwo(a, n):
# Stores the frequencies
# of every array element
mp = defaultdict (int)
for i in range (n):
mp[a[i]] += 1
# Stores the count
# of removals
count = 0
for i in range (n):
f = False
# For every element, check if
# it can form a sum equal to
# any power of 2 with any other
# element
for j in range (31):
# Store pow(2, j) - a[i]
s = (1 << j) - a[i]
# Check if s is present
# in the array
if (s in mp
# If frequency of s
# exceeds 1
and (mp[s] > 1
# If s has frequency 1
# but is different from
# a[i]
or mp[s] == 1 and
s != a[i])):
# Pair possible
f = True
# If no pair possible for
# the current element
if (f == False):
count += 1
# Return the answer
return count
# Driver Code
if __name__ == "__main__":
a = [6, 2, 11]
n = len(a)
print(powerOfTwo(a, n))
# This code is contributed by Chitranayal
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
using System;
using System.Collections.Generic;
class GFG{
// Function to calculate and return
// the count of elements
static int powerOfTwo(int []a, int n)
{
// Stores the frequencies
// of every array element
Dictionary mp = new Dictionary();
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);
}
}
// 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 Math.Pow(2, j) - a[i]
int s = (1 << j) - a[i];
// Check if s is present
// in the array
if (mp.ContainsKey(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
public static void Main(String[] args)
{
int []a = { 6, 2, 11 };
int n = a.Length;
Console.Write(powerOfTwo(a, n));
}
}
// This code is contributed by Amit Katiyar
Javascript
输出:
1
时间复杂度: O(N*log(N))
辅助空间: O(N)
如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live