C++ code to find minimum different digits to represent n


Suppose we have a number n. We want to split it into some non-zero digits whose sum is n. We want to find a solution with minimum possible number of different digits.

So, if the input is like n = 13, then the output will be [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]

Steps

To solve this, we will follow these steps −

for initialize i := 0, when i < n, update (increase i by 1), do:
   print 1

Example

Let us see the following implementation to get better understanding −

#include <bits/stdc++.h>
using namespace std;
void solve(int n){
   for (int i = 0; i < n; i++)
      printf("1, ");
}
int main(){
   int n = 13;
   solve(n);
}

Input

13

Output

1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,

Updated on: 15-Mar-2022

104 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements