Open In App

Find the minimum number containing only 0s and 1s that represents N.

Given an integer N, the task is to represent N as the sum of numbers such that the numbers can only be formed using the digits 0 and 1 and the total numbers used should be minimum.

Example:



Input: n = 9
Output:
9
1 1 1 1 1 1 1 1 1

Input: n = 32
Output:
3
10 11 11



Approach:

The requirement can be achieved by iteratively constructing the representation using powers of 10, starting from the most significant digit. The maximum digit encountered during the process determines the number of digits needed in the final representation.

Step by step algorithm:

Illustration:

Initialization:

  • n = 32
  • i = 1, j = 0, k = 0

First Iteration (i = 1):

  • Extract the rightmost digit: j = n / i % 10 = 32 / 1 % 10 = 2
  • Update maximum digit to 2.
  • Iterate through each non-zero value of the digit (j = 2):
  • a[2] += 1; (Add 1 to the representation of 2)

Second Iteration (i = 10):

  • Extract the next digit: j = n / i % 10 = 32 / 10 % 10 = 3
  • Update maximum digit to 3.
  • Iterate through each non-zero value of the digit (j = 3):
  • a[3] += 10; (Add 10 to the representation of 3)

Third Iteration (i = 100):

  • Extract the next digit: j = n / i % 10 = 32 / 100 % 10 = 0
  • Maximum digit remains 3 (no change).
  • Since j is 0, nothing is added to the representation.

Output:

  • Maximum Digit: 3
  • Representation: 11 11 10

Below are the implementation of the above approach:




#include <bits/stdc++.h>
using namespace std;
 
int n, a[15], i, j, k;
 
int main()
{
    // Input the integer
    n = 32;
 
    // Initialize variables to track digits and maximum
    // digit
    for (i = 1; i <= n; i *= 10) {
        j = n / i % 10; // Extract the current digit
        k = max(k, j); // Update maximum digit
 
        // Iterate through each non-zero value of the digit
        for (; j; --j)
            a[j] += i; // Add powers of 10 to the
                       // representation
    }
 
    // Output the maximum digit
    printf("%d\n", k);
 
    // Output the representation
    for (i = k; i; --i)
        printf("%d ", a[i]);
 
    return 0;
}




import java.util.Arrays;
 
public class Main {
 
    public static void main(String[] args) {
        // Input the integer
        int n = 32;
 
        // Initialize variables to track digits and maximum digit
        int[] a = new int[15];
        int i, j, k = 0;
 
        for (i = 1; i <= n; i *= 10) {
            j = n / i % 10; // Extract the current digit
            k = Math.max(k, j); // Update maximum digit
 
            // Iterate through each non-zero value of the digit
            for (; j > 0; --j) {
                a[j] += i; // Add powers of 10 to the representation
            }
        }
 
        // Output the maximum digit
        System.out.println(k);
 
        // Output the representation
        for (i = k; i > 0; --i) {
            System.out.print(a[i] + " ");
        }
    }
}




# Python program for the above approach
n = 32
a = [0] * 15
i, j, k = 1, 0, 0
 
# Initialize variables to track digits and maximum digit
while i <= n:
    j = n // i % 10  # Extract the current digit
    k = max(k, j)    # Update maximum digit
 
    # Iterate through each non-zero value of the digit
    while j:
        a[j] += i      # Add powers of 10 to the representation
        j -= 1
 
    i *= 10
 
# Output the maximum digit
print(k)
 
# Output the representation
for i in range(k, 0, -1):
    print(a[i], end=" ")
 
# This code is contributed by Susobhan Akhuli




// C# program for the above approach
using System;
 
class Program {
    static void Main()
    {
        // Input the integer
        int n = 32;
 
        // Initialize variables to track digits and maximum
        // digit
        int[] a = new int[15];
        int i, j, k = 0;
 
        for (i = 1; i <= n; i *= 10) {
            j = n / i % 10; // Extract the current digit
            k = Math.Max(k, j); // Update maximum digit
 
            // Iterate through each non-zero value of the
            // digit
            for (; j > 0; j--)
                a[j] += i; // Add powers of 10 to the
                           // representation
        }
 
        // Output the maximum digit
        Console.WriteLine(k);
 
        // Output the representation
        for (i = k; i > 0; i--)
            Console.Write($"{a[i]} ");
    }
}
 
// This code is contributed by Susobhan Akhuli




// Javascript program for the above approach
let n = 32;
let a = new Array(15).fill(0);
let i, j, k = 0;
 
// Initialize variables to track digits and maximum digit
for (i = 1; i <= n; i *= 10) {
    j = Math.floor((n / i) % 10); // Extract the current digit
    k = Math.max(k, j); // Update maximum digit
 
    // Iterate through each non-zero value of the digit
    for (; j; j--) {
        a[j] += i; // Add powers of 10 to the representation
    }
}
 
// Output the maximum digit
console.log(k);
 
// Output the representation
for (i = k; i; i--) {
    process.stdout.write(`${a[i]} `);
}
 
// This code is contributed by Susobhan Akhuli

Output
3
10 11 11 






Time Complexity: O(log n), where n is the input integer.
Auxiliary Space: O(1)


Article Tags :