# Closest number to N having the given digit sum

• Last Updated : 05 Aug, 2022

Given two integers N and D, the task is to find the closest number to N with the sum of digits equal to D.

Examples:

Input: N = 2022, D = 25
Output: 1996
Explanation: First smaller number than 2022 with sum of digits 25 is 1996
and first greater number than 2022 with sum of digits 25 is 2599.
But 1996 is closer to 2022.

Input: N = 87220000221835856, D = 102
Output: 87219999999993000

Naive Approach:

Iterate through numbers smaller and greater than the given number and check if the sum of digits is equal to the given sum.

Follow the steps to implement the naive approach:

• Initialize num1 = num2 = N.
• Decrement num1 and increment num2 at each iteration until one of them has the sum of digits equal to D and return that number.

Below is the implementation of the naive approach:

## C++

 `// C++ program to implement Naive Approach``#include ``using` `namespace` `std;`` ` `int` `sum_current(ulong n)``{``    ``int` `sum = 0;``    ``while` `(n > 0) {``        ``sum = sum + n % 10;``        ``n /= 10;``    ``}``    ``return` `sum;``}`` ` `ulong closest_number(ulong N, ``int` `D)``{``    ``ulong n1 = N, n2 = N;``    ``while` `(``true``) {``        ``if` `(sum_current(n1) == D)``            ``return` `n1;``        ``n1--;``        ``if` `(sum_current(n2) == D)``            ``return` `n2;``        ``n2++;``    ``}``}`` ` `// Driver code``int` `main()``{``    ``ulong N = 2022;``    ``int` `D = 25;``    ``cout << closest_number(N, D) << endl;``    ``return` `0;``}`

Output

`1996`

Time Complexity: O(d*N) where d is the number of digits
Auxiliary Space: O(1)

Efficient Approach:

Manipulate digits and generate two numbers that have sum of digits D which are closest smaller and bigger than N, and return the one which is closest to N.

Follow the steps to solve this problem efficiently:

• Calculate the current sum of digits of N and store it in a variable cur_sum.
• If D = cur_sum, return N.
• If D < cur_sum:
• Starting from the last digit, make digits of N = 0 until cur_sum – D in less than 10.
• To generate a number with the sum of digits = D and is just smaller than N, set the last digit that is not 0 to make cur_sum = D.
• To generate a number with the sum of digits = D and is just greater than N, increment the last digit that is not 0 and then:
• starting from the last digit set digits = 9 until cur_sum – D is less than 10.
• set the current digit that is not 9 to make cur_sum = D.
• Among the numbers obtained above, return the one closer to N.
• If D > cur_sum:
• To generate a number with the sum of digits = D and is just greater than N:
• starting from the last digit set digits equal to 9 until D – cur_sum is less than 10.
• set the current digit that is not set to 9 to make cur_sum = D.
• To generate a number with the sum of digits = D and is just smaller than N:
• starting from the last digit set digits equal to 9 cur_sum > D.
• decrease by 1 the last digit that is not set to 9.
• starting from last digit, set digits = 0, until cur_sum – D is less than 10.
• set the current digit to make cur_sum = D.
• Among the numbers obtained above, return the one closer to N.

Below is the implementation of the efficient approach:

## C++

 `// C++ program for the above approach`` ` `#include ``using` `namespace` `std;`` ` `// Fucntion to convert vector to integer``ulong toInt(``const` `vector<``int``>& digits)``{``    ``ulong n = 0;``    ``for` `(``int` `i = digits.size() - 1; i >= 0; i--)``        ``n = n * 10 + digits[i];``    ``return` `n;``}`` ` `// Function to find the number closest to N``// with sum of digits D``ulong closest_number(ulong N, ``int` `D)``{``    ``vector<``int``> digits;``    ``ulong temp_num = N;`` ` `    ``int` `cur_sum = 0, temp_cur_sum, index;``    ``while` `(temp_num > 0) {``        ``int` `digit = temp_num % 10;``        ``digits.push_back(digit);``        ``cur_sum += digit;``        ``temp_num /= 10;``    ``}``    ``temp_cur_sum = cur_sum;``    ``if` `(D == cur_sum)``        ``return` `N;`` ` `    ``else` `if` `(D < cur_sum) {``        ``auto` `n1 = digits;``        ``for` `(index = 0; cur_sum - D >= n1[index]; index++) {``            ``cur_sum -= n1[index];``            ``n1[index] = 0;``        ``}``        ``n1[index] -= (cur_sum - D);``        ``auto` `n2 = digits;``        ``cur_sum = temp_cur_sum;``        ``for` `(index = 0; cur_sum >= D; index++) {``            ``cur_sum -= n2[index];``            ``n2[index] = 0;``        ``}``        ``if` `(index < n2.size())``            ``++n2[index];``        ``else``            ``n2.push_back(1);``        ``++cur_sum;``        ``for` `(``int` `i = index; i < n2.size() && n2[i] > 9;``             ``i++) {``            ``n2[i] = 0;``            ``if` `(i == n2.size() - 1)``                ``n2.push_back(1);``            ``else``                ``++n2[i + 1];``            ``cur_sum -= 9;``        ``}``        ``for` `(index = 0; D - cur_sum + n2[index] >= 10;``             ``index++) {``            ``cur_sum += (9 - n2[index]);``            ``n2[index] = 9;``        ``}``        ``n2[index] += D - cur_sum;``        ``if` `(N - toInt(n1) <= toInt(n2) - N)``            ``return` `toInt(n1);``        ``else``            ``return` `toInt(n2);``    ``}``    ``else` `{``        ``auto` `n1 = digits;``        ``for` `(index = 0; D - cur_sum + n1[index] >= 10;``             ``index++) {``            ``if` `(index == n1.size())``                ``n1.push_back(0);``            ``cur_sum += (9 - n1[index]);``            ``n1[index] = 9;``        ``}``        ``if` `(index == n1.size())``            ``n1.push_back(0);``        ``n1[index] += D - cur_sum;``        ``auto` `n2 = digits;``        ``int` `cur_sum = temp_cur_sum;``        ``for` `(index = 0; cur_sum < D && index < n2.size();``             ``index++) {``            ``cur_sum += (9 - n2[index]);``            ``n2[index] = 9;``        ``}``        ``--n2[index];``        ``--cur_sum;``        ``if` `(cur_sum < D)``            ``n2.clear();``        ``else` `{``            ``for` `(``int` `i = index; i < n2.size() && n2[i] < 0;``                 ``i++) {``                ``n2[i] = 9;``                ``if` `(i == n2.size() - 1)``                    ``n2.pop_back();``                ``else``                    ``--n2[i + 1];``                ``cur_sum += 9;``            ``}``            ``for` `(index = 0; cur_sum - D >= n2[index];``                 ``index++) {``                ``cur_sum -= n2[index];``                ``n2[index] = 0;``            ``}``            ``n2[index] = 9 - (cur_sum - D);``        ``}``        ``if` `(n2.size() > 0 && N - toInt(n2) <= toInt(n1) - N)``            ``return` `toInt(n2);``        ``else``            ``return` `toInt(n1);``    ``}``}`` ` `// Driver code``int` `main()``{``    ``ulong N = 87220000221835856;``    ``int` `D = 102;`` ` `    ``// Function call``    ``cout << closest_number(N, D);``    ``return` `0;``}`

Output

`87219999999993000`

Time Complexity: O(d) where d is the number of digits
Auxiliary Space: O(d)

My Personal Notes arrow_drop_up