Represent a number as sum of minimum possible pseudobinary numbers
Last Updated :
19 Dec, 2022
Given a number, you have to represent this number as sum of minimum number of possible pseudobinary numbers. A number is said to be pseudobinary number if its decimal number consists of only two digits (0 and 1). Example: 11,10,101 are all pseudobinary numbers.
Examples :-
Input : 44
Output : 11 11 11 11
Explanation : 44 can be represented as sum of
minimum 4 pseudobinary numbers as 11+11+11+11
Input : 31
Output : 11 10 10
Explanation : 31 can be represented as sum of
minimum 3 pseudobinary numbers as 11+10+10
The idea to do this is to first observe carefully that we need to calculate minimum number of possible pseudobinary numbers. To do this we find a new number m such that if for a place in given number n, the digit is non-zero then the digit in that place in m is 1 otherwise zero. For example, if n = 5102, then m will be 1101. Then we will print this number m and subtract m from n. We will keep repeating these steps until n is greater than zero.
C++
#include<iostream>
using namespace std;
void pseudoBinary( int n)
{
while (n > 0)
{
int temp = n, m = 0, p = 1;
while (temp)
{
int rem = temp % 10;
temp = temp / 10;
if (rem != 0)
m += p;
p *= 10;
}
cout << m << " " ;
n = n - m;
}
}
int main()
{
int n = 31;
pseudoBinary(n);
return 0;
}
|
Java
import java.util.*;
import java.lang.*;
class GFG
{
public static void pseudoBinary( int n)
{
while (n != 0 )
{
int temp = n, m = 0 , p = 1 ;
while (temp != 0 )
{
int rem = temp % 10 ;
temp = temp / 10 ;
if (rem != 0 )
m += p;
p *= 10 ;
}
System.out.print(m + " " );
n = n - m;
}
System.out.println( " " );
}
public static void main(String[] args)
{
int n = 31 ;
pseudoBinary(n);
}
}
|
Python3
def pseudoBinary(n):
while (n > 0 ):
temp = n;
m = 0 ;
p = 1 ;
while (temp):
rem = temp % 10 ;
temp = int (temp / 10 );
if (rem ! = 0 ):
m + = p;
p * = 10 ;
print (m,end = " " );
n = n - m;
n = 31 ;
pseudoBinary(n);
|
C#
using System;
class GFG
{
public static void pseudoBinary( int n)
{
while (n != 0)
{
int temp = n, m = 0, p = 1;
while (temp != 0)
{
int rem = temp % 10;
temp = temp / 10;
if (rem != 0)
m += p;
p *= 10;
}
Console.Write(m + " " );
n = n - m;
}
Console.Write( " " );
}
public static void Main()
{
int n = 31;
pseudoBinary(n);
}
}
|
PHP
<?php
function pseudoBinary( $n )
{
while ( $n > 0)
{
$temp = $n ; $m = 0; $p = 1;
while ( $temp )
{
$rem = $temp % 10;
$temp = $temp / 10;
if ( $rem != 0)
$m += $p ;
$p *= 10;
}
echo $m , " " ;
$n = $n - $m ;
}
}
$n = 31;
pseudoBinary( $n );
?>
|
Javascript
<script>
function pseudoBinary( n)
{
while (n != 0)
{
var temp = n, m = 0, p = 1;
while (temp != 0) {
var rem = temp % 10;
temp = parseInt(temp / 10);
if (rem != 0)
m += p;
p *= 10;
}
document.write(m + " " );
n = n - m;
}
document.write( " " );
}
var n = 31;
pseudoBinary(n);
</script>
|
Output:
11 10 10
Time Complexity : O( log n )
Auxiliary Space : O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...