Break the number N in odd powers of 2
Given a positive integer N. The task is to print the array in decreasing order in which the numbers are odd powers of 2 and the sum of all the numbers in the array is N and the size of the array should be minimum if it is not possible to form the array then print -1.
Examples:
Input: 18
Output: 8 8 2
Explanation: Array with sum 18 and which consists
minimum numbers with odd powers of 2 are [8 8 2] .
Since 8+8+2=19 and 21=2, 23=8 where 1, 3 are odd numbers.
Input: 7
Output: -1
Explanation: Since there is no array with sum equal to 7.
Approach: To solve the problem follow the below idea:
The idea is to create a binary array of size (32) and check the set bit position.If the bit position is odd, store the power of position else store power of (position-1).
Follow the below steps to solve the problem:
- Create a binary array of max size 32 which will store the number’s binary.
- If the number is odd then print -1.
- If the number is even traverse the binary array from the reverse side and check if the bit is 1 and the position of the bit is odd then take its power of 2 and store it in the answer array.
- If the bit is 1 and the position of the bit is even then take the 2 instances of 2^(position-1).
- If the bit is 0 then continue traversing the binary array.
- Print the answer array
Below is the Implementation of the above approach.
C++
#include <bits/stdc++.h>
using namespace std;
int power( int x, int y)
{
if (y == 0)
return 1;
else if (y % 2 == 0)
return power(x, y / 2) * power(x, y / 2);
else
return x * power(x, y / 2) * power(x, y / 2);
}
vector< int > solve( int N)
{
vector< int > binary(32);
vector< int > answer;
for ( int i = 31; i >= 0; i--) {
if ((N & (1 << i)) != 0)
binary[i] = 1;
}
for ( int i = 31; i >= 0; i--) {
if (binary[i] == 1 && i & 1) {
answer.push_back(power(2, i));
}
else if (binary[i] == 1 && !(i & 1)) {
answer.push_back(power(2, i - 1));
answer.push_back(power(2, i - 1));
}
else
continue ;
}
return answer;
}
int main()
{
int N = 18;
if (N & 1) {
cout << -1 << endl;
return 0;
}
vector< int > ans = solve(N);
for ( int x : ans)
cout << x << " " ;
return 0;
}
|
Java
import java.io.*;
import java.util.*;
class GFG
{
public static int power( int x, int y)
{
if (y == 0 )
return 1 ;
else if (y % 2 == 0 )
return power(x, y / 2 ) * power(x, y / 2 );
else
return x * power(x, y / 2 ) * power(x, y / 2 );
}
public static ArrayList<Integer> solve( int N)
{
int binary[] = new int [ 32 ];
ArrayList<Integer> answer
= new ArrayList<Integer>();
for ( int i = 31 ; i >= 0 ; i--) {
if ((N & ( 1 << i)) != 0 )
binary[i] = 1 ;
}
for ( int i = 31 ; i >= 0 ; i--) {
if (binary[i] == 1 && (i & 1 ) != 0 ) {
answer.add(power( 2 , i));
}
else if (binary[i] == 1 && (i & 1 ) == 0 ) {
answer.add(power( 2 , i - 1 ));
answer.add(power( 2 , i - 1 ));
}
else
continue ;
}
return answer;
}
public static void main(String[] args)
{
int N = 18 ;
if ((N & 1 ) != 0 ) {
System.out.println(- 1 );
return ;
}
ArrayList<Integer> ans = solve(N);
for (Integer x : ans)
System.out.print(x + " " );
}
}
|
Python3
import math
def power( x, y):
if (y = = 0 ):
return 1 ;
elif (y % 2 = = 0 ):
return power(x, math.floor(y / 2 )) * power(x, math.floor(y / 2 ));
else :
return x * power(x, math.floor(y / 2 )) * power(x, math.floor(y / 2 ));
def solve( N):
binary = [ 0 ] * ( 32 );
answer = [];
for i in range ( 31 , 0 , - 1 ) :
if ((N & ( 1 << i)) ! = 0 ):
binary[i] = 1 ;
for i in range ( 31 , 0 , - 1 ):
if binary[i] = = 1 and i & 1 :
answer.append(power( 2 , i));
elif binary[i] = = 1 and (i & 1 ) = = 0 :
answer.append(power( 2 , i - 1 ));
answer.append(power( 2 , i - 1 ));
else :
continue ;
return answer;
N = 18 ;
if (N & 1 ):
print ( - 1 )
ans = solve(N);
for x in ans:
print (x,end = " " );
|
C#
using System;
using System.Collections.Generic;
class GFG
{
public static int power( int x, int y)
{
if (y == 0)
return 1;
else if (y % 2 == 0)
return power(x, y / 2) * power(x, y / 2);
else
return x * power(x, y / 2) * power(x, y / 2);
}
public static List< int > solve( int N)
{
int [] binary = new int [32];
List< int > answer
= new List< int >();
for ( int i = 31; i >= 0; i--) {
if ((N & (1 << i)) != 0)
binary[i] = 1;
}
for ( int i = 31; i >= 0; i--) {
if (binary[i] == 1 && (i & 1) != 0) {
answer.Add(power(2, i));
}
else if (binary[i] == 1 && (i & 1) == 0) {
answer.Add(power(2, i - 1));
answer.Add(power(2, i - 1));
}
else
continue ;
}
return answer;
}
public static void Main()
{
int N = 18;
if ((N & 1) != 0) {
Console.Write(-1);
return ;
}
List< int > ans = solve(N);
foreach ( int x in ans)
Console.Write(x + " " );
}
}
|
Javascript
<script>
const power = (x, y) => {
if (y == 0)
return 1;
else if (y % 2 == 0)
return power(x, parseInt(y / 2)) * power(x, parseInt(y / 2));
else
return x * power(x, parseInt(y / 2)) * power(x, parseInt(y / 2));
}
const solve = (N) => {
let binary = new Array(32).fill(0);
let answer = [];
for (let i = 31; i >= 0; i--) {
if ((N & (1 << i)) != 0)
binary[i] = 1;
}
for (let i = 31; i >= 0; i--) {
if (binary[i] == 1 && i & 1) {
answer.push(power(2, i));
}
else if (binary[i] == 1 && !(i & 1)) {
answer.push(power(2, i - 1));
answer.push(power(2, i - 1));
}
else
continue ;
}
return answer;
}
let N = 18;
if (N & 1) {
document.write( "-1<br/>" );
}
let ans = solve(N);
for (let x in ans)
document.write(`${ans[x]} `);
</script>
|
Time Complexity: O(N*log N) where in the worst case N is 32.
Auxiliary Space: O(N) where N is 32.
Last Updated :
29 Sep, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...