Math, asked by munmunsarkar886, 2 months ago

.. Add the smallest number and the number exactly halfway in
the row, and subtract their sum from the largest number in the
row.​

Answers

Answered by anant0007
0

Answer:

Input : s = 9, d = 2

Output : 18

There are many other possible numbers

like 45, 54, 90, etc with sum of digits

as 9 and number of digits as 2. The

smallest of them is 18.

Input : s = 20, d = 3

Output : 299

Step-by-step explanation:

A Simple Solution is to consider all m digit numbers and keep track of minimum number with digit sum as s. A close upper bound on time complexity of this solution is O(10m).

There is a Greedy approach to solve the problem. The idea is to one by one fill all digits from rightmost to leftmost (or from least significant digit to most significant).

We initially deduct 1 from sum s so that we have smallest digit at the end. After deducting 1, we apply greedy approach. We compare remaining sum with 9, if remaining sum is more than 9, we put 9 at the current position, else we put the remaining sum. Since we fill digits from right to left, we put the highest digits on the right side. Below is implementation of the idea.

Similar questions