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++
// C++ program to represent a given
// number as sum of minimum possible
// pseudobinary numbers
#include<iostream>
using namespace std;
// function to represent a given
// number as sum of minimum possible
// pseudobinary numbers
void pseudoBinary(int n)
{
// Repeat below steps until n > 0
while (n > 0)
{
// calculate m (A number that has same
// number of digits as n, but has 1 in
// place of non-zero digits 0 in place
// of 0 digits)
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 << " ";
// subtract m from n
n = n - m;
}
}
// Driver code
int main()
{
int n = 31;
pseudoBinary(n);
return 0;
}
Java
// Java program to represent a given
// number as sum of minimum possible
// pseudobinary numbers
import java.util.*;
import java.lang.*;
class GFG
{
public static void pseudoBinary(int n)
{
// Repeat below steps until n > 0
while (n != 0)
{
// calculate m (A number that has same
// number of digits as n, but has 1 in
// place of non-zero digits 0 in place
// of 0 digits)
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 + " ");
// subtract m from n
n = n - m;
}
System.out.println(" ");
}
// Driver code
public static void main(String[] args)
{
int n = 31;
pseudoBinary(n);
}
}
// This code is contributed by Mohit Gupta_OMG
Python3
# Python3 program to represent
# a given number as sum of
# minimum possible pseudobinary
# numbers
# function to represent a
# given number as sum of
# minimum possible
# pseudobinary numbers
def pseudoBinary(n):
# Repeat below steps
# until n > 0
while (n > 0):
# calculate m (A number
# that has same number
# of digits as n, but
# has 1 in place of non-zero
# digits 0 in place of 0 digits)
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=" ");
# subtract m from n
n = n - m;
# Driver code
n = 31;
pseudoBinary(n);
# This code is contributed
# by mits.
C#
// C# program to represent a given
// number as sum of minimum possible
// pseudobinary numbers
using System;
class GFG
{
public static void pseudoBinary(int n)
{
// Repeat below steps until n > 0
while (n != 0)
{
// calculate m (A number that has same
// number of digits as n, but has 1 in
// place of non-zero digits 0 in place
// of 0 digits)
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 + " ");
// subtract m from n
n = n - m;
}
Console.Write(" ");
}
// Driver code
public static void Main()
{
int n = 31;
pseudoBinary(n);
}
}
// This code is contributed by nitin mittal
PHP
<?php
// PHP program to represent a
// given number as sum of minimum
// possible pseudobinary numbers
// Function to represent a
// given number as sum of minimum
// possible pseudobinary numbers
function pseudoBinary($n)
{
// Repeat below steps until n > 0
while ($n > 0)
{
// calculate m (A number
// that has same number of
// digits as n, but has 1
// in place of non-zero
// digits 0 in place of 0
// digits)
$temp = $n; $m = 0; $p = 1;
while ($temp)
{
$rem = $temp % 10;
$temp = $temp / 10;
if ($rem != 0)
$m += $p;
$p *= 10;
}
echo $m , " ";
// subtract m from n
$n = $n - $m;
}
}
// Driver code
$n = 31;
pseudoBinary($n);
// This code is contributed
// by nitin mittal.
?>
JavaScript
<script>
// JavaScript program to represent a given
// number as sum of minimum possible
// pseudobinary numbers
function pseudoBinary( n)
{
// Repeat below steps until n > 0
while (n != 0)
{
// calculate m (A number that has same
// number of digits as n, but has 1 in
// place of non-zero digits 0 in place
// of 0 digits)
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 + " ");
// subtract m from n
n = n - m;
}
document.write(" ");
}
// Driver code
var n = 31;
pseudoBinary(n);
// This code is contributed by Amit Katiyar
</script>
Output:
11 10 10
Time Complexity : O( log n )
Auxiliary Space : O(1)
Similar Reads
Represent a number as the sum of positive numbers ending with 9 Given an integer N, the task is to check if N can be expressed as a sum of integers having 9 as the last digit (9, 19, 29, 39...), or not. If found to be true, then find the minimum count of such integers required to obtain N. Otherwise print -1. Examples: Input: N = 156Output: 4Explanation:156 = 9
5 min read
Minimum number of Binary strings to represent a Number Given a number N. The task is to find the minimum number of binary strings required to represent the given number as the sum of the binary strings.Examples: Input : 131 Output : Minimum Number of binary strings needed: 3 111 10 10 Input : 564 Output :Minimum Number of binary strings needed: 6 111 11
7 min read
Minimum number using set bits of a given number Given an unsigned number, find the minimum number that could be formed by using the bits of the given unsigned number. Examples : Input : 6 Output : 3 Binary representation of 6 is 0000....0110. Smallest number with same number of set bits 0000....0011. Input : 11 Output : 7 Simple Approach: 1. Find
4 min read
Find second smallest number from sum of digits and number of digits Given the sum of digits as S and the number of digits as D, the task is to find the second smallest number Examples: Input: S = 9, D = 2Output: 27Explanation: 18 is the smallest number possible with sum = 9 and total digits = 2, Whereas the second smallest is 27. Input: S = 16, D = 3Output: 178Expla
8 min read
Sum of decimal equivalent of all possible pairs of Binary representation of a Number Given a number N. The task is to find the sum of the decimal equivalent of all the pairs formed from the binary representation of the given number. Examples: Input: N = 4 Output: 4 Binary equivalent of 4 is 100. All possible pairs are 10, 10, 00 and their decimal equivalent are 2, 2, 0 respectively.
5 min read
M-th smallest number having k number of set bits. Given two non-negative integers m and k. The problem is to find the m-th smallest number having k number of set bits.Constraints: 1 <= m, k.Examples: Input : m = 4, k = 2 Output : 9 (9)10 = (1001)2, it is the 4th smallest number having 2 set bits. Input : m = 6, k = 4 Output : 39 Approach: Follow
6 min read