给定长度为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]组成的对是不可能的。
- 如果对于电流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
输出:
1
时间复杂度: O(N * log(N))
辅助空间: O(N)