给定一个整数x和一个数组arr [],每个元素的幂为2。该任务是从数组中找到2的最小整数次幂,相加后得出x 。如果不可能用给定的数组元素表示x ,则打印-1 。
例子:
Input: arr[] = {2, 4, 8, 2, 4}, x = 14
Output: 3
14 can be written as 8 + 4 + 2
Input: arr[] = {2, 4, 8, 2, 4}, x = 5
Output: -1
5 cannot be represented as the sum any elements from the given array.
方法:对于2的乘方,让我们计算给定数组中元素的数量,其值等于此值。我们称它为cnt 。显然,我们可以贪婪地获得值x(因为元素的所有较少值都是元素的所有较大值的除数)。
现在让我们将2的所有幂从30迭代到0 。 deg为当前度。我们可以取min(x / 2 deg ,cnt deg )个元素,其值等于2 deg 。任其治愈。将cur加到答案中,并从x减去2度* cur 。重复该过程,直到无法再减小x为止。如果在遍历所有幂之后,x仍然不为零,则打印-1 。否则,打印答案。
下面是上述方法的实现:
C++
// C++ implementation of the approach
#include
using namespace std;
// Function to return the minimum number
// of given integer powers of 2 required
// to represent a number as sum of these powers
int power_of_two(int n, int a[], int x)
{
// To store the count of powers of two
vector cnt(31);
for (int i = 0; i < n; ++i) {
// __builtin_ctz(a[i]) returns the count
// of trailing 0s in a[i]
++cnt[__builtin_ctz(a[i])];
}
int ans = 0;
for (int i = 30; i >= 0 && x > 0; --i) {
// If current power is available
// in the array and can be used
int need = min(x >> i, cnt[i]);
// Update the answer
ans += need;
// Reduce the number
x -= (1 << i) * need;
}
// If the original number is not reduced to 0
// It cannot be represented as the sum
// of the given powers of 2
if (x > 0)
ans = -1;
return ans;
}
// Driver code
int main()
{
int arr[] = { 2, 2, 4, 4, 8 }, x = 6;
int n = sizeof(arr) / sizeof(arr[0]);
cout << power_of_two(n, arr, x);
return 0;
}
Java
// Java implementation of the approach
import java.util.*;
class GFG
{
// __builtin_ctz(a[i]) returns the count
// of trailing 0s in a[i]
static int __builtin_ctz(int a)
{
int count = 0;
for(int i = 0; i < 40; i++)
if(((a >> i) & 1) == 0)
{
count++;
}
else
break;
return count;
}
// Function to return the minimum number
// of given integer powers of 2 required
// to represent a number as sum of these powers
static int power_of_two(int n, int a[], int x)
{
// To store the count of powers of two
Vector cnt = new Vector();
for (int i = 0; i < 31; ++i)
cnt.add(0);
for (int i = 0; i < n; ++i)
{
// __builtin_ctz(a[i]) returns the count
// of trailing 0s in a[i]
cnt.set(__builtin_ctz(a[i]),
(cnt.get(__builtin_ctz(a[i]))==null) ?
1 : cnt.get(__builtin_ctz(a[i]))+1);
}
int ans = 0;
for (int i = 30; i >= 0 && x > 0; --i)
{
// If current power is available
// in the array and can be used
int need = Math.min(x >> i, cnt.get(i));
// Update the answer
ans += need;
// Reduce the number
x -= (1 << i) * need;
}
// If the original number is not reduced to 0
// It cannot be represented as the sum
// of the given powers of 2
if (x > 0)
ans = -1;
return ans;
}
// Driver code
public static void main(String args[])
{
int arr[] = { 2, 2, 4, 4, 8 }, x = 6;
int n = arr.length;
System.out.println(power_of_two(n, arr, x));
}
}
// This code is contributed by Arnab Kundu
python
# Python3 implementation of the approach
# Function to return the minimum number
# of given eger powers of 2 required
# to represent a number as sum of these powers
def power_of_two( n, a, x):
# To store the count of powers of two
cnt=[0 for i in range(31)]
for i in range(n):
# __builtin_ctz(a[i]) returns the count
# of trailing 0s in a[i]
count = 0
xx = a[i]
while ((xx & 1) == 0):
xx = xx >> 1
count += 1
cnt[count]+=1
ans = 0
for i in range(30,-1,-1):
if x<=0:
continue
# If current power is available
# in the array and can be used
need = min(x >> i, cnt[i])
# Update the answer
ans += need
# Reduce the number
x -= (1 << i) * need
# If the original number is not reduced to 0
# It cannot be represented as the sum
# of the given powers of 2
if (x > 0):
ans = -1
return ans
# Driver code
arr=[2, 2, 4, 4, 8 ]
x = 6
n = len(arr)
print(power_of_two(n, arr, x))
# This code is contributed by mohit kumar 29
C#
// C# implementation of the approach
using System;
class GFG
{
// __builtin_ctz(a[i]) returns the count
// of trailing 0s in a[i]
static int __builtin_ctz(int a)
{
int count = 0;
for(int i = 0; i < 40; i++)
if(((a >> i) & 1) == 0)
{
count++;
}
else
break;
return count;
}
// Function to return the minimum number
// of given integer powers of 2 required
// to represent a number as sum of these powers
static int power_of_two(int n, int []a, int x)
{
// To store the count of powers of two
int[] cnt = new int[32];
for (int i = 0; i < n; ++i)
{
// __builtin_ctz(a[i]) returns the count
// of trailing 0s in a[i]
cnt[__builtin_ctz(a[i])] =
cnt[__builtin_ctz(a[i])] ==
0?1 : cnt[__builtin_ctz(a[i])] + 1;
}
int ans = 0;
for (int i = 30; i >= 0 && x > 0; --i)
{
// If current power is available
// in the array and can be used
int need = Math.Min(x >> i, cnt[i]);
// Update the answer
ans += need;
// Reduce the number
x -= (1 << i) * need;
}
// If the original number is not reduced to 0
// It cannot be represented as the sum
// of the given powers of 2
if (x > 0)
ans = -1;
return ans;
}
// Driver code
static void Main()
{
int []arr = { 2, 2, 4, 4, 8 };
int x = 6;
int n = arr.Length;
Console.WriteLine(power_of_two(n, arr, x));
}
}
// This code is contributed by mits
输出:
2
如果您希望与行业专家一起参加现场课程,请参阅《 Geeks现场课程》和《 Geeks现场课程美国》。