Horje
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:

  • Set variables n, a[15], i, j, and k. where n is the given integer, a[15] is an array to count occurrences of digits, and i, j, k are loop variables and a variable to track the maximum digit.
  • Start a loop (for) where i iterates over powers of 10 (1, 10, 100, …).
  • Inside the loop:
    • Calculate j as the current digit in the ith place (n/i%10).
    • Update k as the maximum of k and j.
    • For each non-zero value of j:
      • Increment the corresponding count in the array a.
      • This step effectively adds powers of 10 to the representation.
  • Print the maximum digit k.
  • Print the representation using the array a.

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:

C++

#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;
}

Java

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] + " ");
        }
    }
}

Python3

# 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#

// 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

// 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)




Reffered: https://www.geeksforgeeks.org


Competitive Programming

Related
Hashing in Competitive Programming Hashing in Competitive Programming
Find the path from root to a vertex such that all K vertices belongs to this path or 1 distance away Find the path from root to a vertex such that all K vertices belongs to this path or 1 distance away
Find Edge Weights in a Spanning Tree with Edge (u, v) Find Edge Weights in a Spanning Tree with Edge (u, v)
Find the Longest Non-Prefix-Suffix Substring in the Given String Find the Longest Non-Prefix-Suffix Substring in the Given String
Minimizing and Maximizing longest Increasing Subsequence (LIS) Minimizing and Maximizing longest Increasing Subsequence (LIS)

Type:
Geek
Category:
Coding
Sub Category:
Tutorial
Uploaded by:
Admin
Views:
15