具有 [0, X] 范围内的元素或 2 的奇数幂且总和为 N 的集合的最小大小
给定两个正整数N和X ,任务是找到最小整数集合的大小,使得集合中所有元素的总和为N并且每个集合元素要么在[0, X]范围内,要么是2的奇次幂。如果无法找到这样的集合大小,则打印“-1” 。
例子:
Input: N = 11, X = 2
Output: 3
Explanation: The set {1, 2, 8} is the set of minimum number of elements such that the sum of elements is 11 and each element is either in range [0, 2] (i.e, 1 and 2) or is an odd power of 2 (i.e., 8 = 23).
Input: N = 3, X = 0
Output: -1
Explanation : No valid set exist.
方法:给定的问题可以使用以下步骤解决:
- 维护一个可变大小,用于存储有效集合的最小可能大小,并将其初始化为0 。
- 迭代直到N的值大于X并执行以下步骤:
- 从N中减去小于或等于N的2的最大奇数次方i 。
- 将size的值增加1 。
- 如果N的值为正,则将size的值增加1 。
- 完成上述步骤后,打印size的值作为所需的结果。
下面是上述方法的实现:
C++
// CPP program for the above approach
#include
using namespace std;
// Function to find the highest odd power
// of 2 in the range [0, N]
int highestPowerof2(int n)
{
int p = int(log2(n));
// If P is even, subtract 1
if(p % 2 == 0)
p -= 1;
return int(pow(2, p));
}
// Function to find the minimum operations
// to make N
int minStep(int N, int X)
{
// If N is odd and X = 0, then no
// valid set exist
if(N % 2 and X == 0)
return -1;
// Stores the minimum possible size
// of the valid set
int size = 0;
// Loop to subtract highest odd power
// of 2 while X < N, step 2
while(X < N){
N -= highestPowerof2(N);
size += 1;
}
// If N > 0, then increment the value
// of answer by 1
if(N)
size += 1;
// Return the resultant size of set
return size;
}
// Driver Code
int main(){
int N = 11;
int X = 2;
cout<<(minStep(N, X));
}
// This code is contributed by ipg2016107.
Java
// Java program for the above approach
import java.io.*;
class GFG {
// Function to find the highest odd power
// of 2 in the range [0, N]
static int highestPowerof2(int n)
{
int p = (int)Math.floor(Math.log(n)/Math.log(2.0));
// If P is even, subtract 1
if(p % 2 == 0)
p -= 1;
int result = (int)(Math.pow(2,p));
return result;
}
// Function to find the minimum operations
// to make N
static int minStep(int N, int X)
{
// If N is odd and X = 0, then no
// valid set exist
if (N % 2 != 0 && X == 0)
return -1;
// Stores the minimum possible size
// of the valid set
int size = 0;
// Loop to subtract highest odd power
// of 2 while X < N, step 2
while(X < N){
N -= highestPowerof2(N);
size += 1;
}
// If N > 0, then increment the value
// of answer by 1
if (N != 0)
size += 1;
// Return the resultant size of set
return size;
}
// Driver Code
public static void main (String[] args)
{
int N = 11;
int X = 2;
System.out.println(minStep(N, X));
}
}
// This code is contributed by shivanisinghss2110
Python3
# Python program for the above approach
import math
# Function to find the highest odd power
# of 2 in the range [0, N]
def highestPowerof2(n):
p = int(math.log(n, 2))
# If P is even, subtract 1
if p % 2 == 0:
p -= 1
return int(pow(2, p))
# Function to find the minimum operations
# to make N
def minStep(N, X):
# If N is odd and X = 0, then no
# valid set exist
if N % 2 and X == 0:
return -1
# Stores the minimum possible size
# of the valid set
size = 0
# Loop to subtract highest odd power
# of 2 while X < N, step 2
while X < N:
N -= highestPowerof2(N)
size += 1
# If N > 0, then increment the value
# of answer by 1
if N:
size += 1
# Return the resultant size of set
return size
# Driver Code
if __name__ == '__main__':
N = 11
X = 2
print(minStep(N, X))
C#
// C# program for the above approach
using System;
class GFG {
// Function to find the highest odd power
// of 2 in the range [0, N]
static int highestPowerof2(int n)
{
int p = (int)Math.Floor(Math.Log(n)/Math.Log(2.0));
// If P is even, subtract 1
if(p % 2 == 0)
p -= 1;
int result = (int)(Math.Pow(2,p));
return result;
}
// Function to find the minimum operations
// to make N
static int minStep(int N, int X)
{
// If N is odd and X = 0, then no
// valid set exist
if (N % 2 != 0 && X == 0)
return -1;
// Stores the minimum possible size
// of the valid set
int size = 0;
// Loop to subtract highest odd power
// of 2 while X < N, step 2
while(X < N){
N -= highestPowerof2(N);
size += 1;
}
// If N > 0, then increment the value
// of answer by 1
if (N != 0)
size += 1;
// Return the resultant size of set
return size;
}
// Driver Code
public static void Main (String[] args)
{
int N = 11;
int X = 2;
Console.Write(minStep(N, X));
}
}
// This code is contributed by shivanisinghss2110
Javascript
输出:
3
时间复杂度: O(log N)
辅助空间: O(1)