Minimum size of set having either element in range [0, X] or an odd power of 2 with sum N
Last Updated :
27 Sep, 2021
Given two positive integers N and X, the task is to find the size of the smallest set of integers such that the sum of all elements of the set is N and each set element is either in the range [0, X] or is an odd power of 2. If it is not possible to find such a size of the set then print “-1”.
Examples:
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.
Approach: The given problem can be solved using the below steps:
- Maintain a variable size that stores the minimum possible size of a valid set and initialize it with 0.
- Iterate until the value of N is greater than X and perform the following steps:
- Subtract the largest odd power i of 2 that is less than or equal to N from N.
- Increment the value of size by 1.
- If the value of N is positive, then increment the value of size by 1.
- After completing the above steps, print the value of size as the required result.
Below is the implementation of the above approach:
C++
#include<bits/stdc++.h>
using namespace std;
int highestPowerof2( int n)
{
int p = int (log2(n));
if (p % 2 == 0)
p -= 1;
return int ( pow (2, p));
}
int minStep( int N, int X)
{
if (N % 2 and X == 0)
return -1;
int size = 0;
while (X < N){
N -= highestPowerof2(N);
size += 1;
}
if (N)
size += 1;
return size;
}
int main(){
int N = 11;
int X = 2;
cout<<(minStep(N, X));
}
|
Java
import java.io.*;
class GFG {
static int highestPowerof2( int n)
{
int p = ( int )Math.floor(Math.log(n)/Math.log( 2.0 ));
if (p % 2 == 0 )
p -= 1 ;
int result = ( int )(Math.pow( 2 ,p));
return result;
}
static int minStep( int N, int X)
{
if (N % 2 != 0 && X == 0 )
return - 1 ;
int size = 0 ;
while (X < N){
N -= highestPowerof2(N);
size += 1 ;
}
if (N != 0 )
size += 1 ;
return size;
}
public static void main (String[] args)
{
int N = 11 ;
int X = 2 ;
System.out.println(minStep(N, X));
}
}
|
Python3
import math
def highestPowerof2(n):
p = int (math.log(n, 2 ))
if p % 2 = = 0 :
p - = 1
return int ( pow ( 2 , p))
def minStep(N, X):
if N % 2 and X = = 0 :
return - 1
size = 0
while X < N:
N - = highestPowerof2(N)
size + = 1
if N:
size + = 1
return size
if __name__ = = '__main__' :
N = 11
X = 2
print (minStep(N, X))
|
C#
using System;
class GFG {
static int highestPowerof2( int n)
{
int p = ( int )Math.Floor(Math.Log(n)/Math.Log(2.0));
if (p % 2 == 0)
p -= 1;
int result = ( int )(Math.Pow(2,p));
return result;
}
static int minStep( int N, int X)
{
if (N % 2 != 0 && X == 0)
return -1;
int size = 0;
while (X < N){
N -= highestPowerof2(N);
size += 1;
}
if (N != 0)
size += 1;
return size;
}
public static void Main (String[] args)
{
int N = 11;
int X = 2;
Console.Write(minStep(N, X));
}
}
|
Javascript
<script>
function highestPowerof2(n)
{
let p = Math.floor(Math.log2(n));
if (p % 2 == 0)
{
p -= 1
}
return Math.pow(2, p)
}
function minStep(N, X)
{
if (N % 2 != 0 && X == 0)
return -1
let size = 0
while (X < N)
{
N -= highestPowerof2(N)
size += 1
}
if (N != 0)
size += 1
return size;
}
let N = 11
let X = 2
document.write(minStep(N, X))
</script>
|
Time Complexity: O(log N)
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...