
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Find Minimum Different Digits to Represent N in C++
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,
Advertisements