
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Find minimum possible digit sum after adding a number d in C++
In this problem, we are given two numbers n and d. Our task is to Find the minimum possible digit sum after adding a number d.
Problem Description − we need to minimise the digits sum by adding kth multiple of d to n.
Let’s take an example to understand the problem,
Input
n = 5230, d = 54
Output
1
Explanation
The number will be 5230 + (2*54) = 5338
Solution Approach
A simple approach to solve the problem would be to check all multiples of d from 1 to 8, as at 9th multiple the sum of digits will repeat. This is based on modulo 9, which will return the digits sum. So, a+d*(9k+l) modulo 9 is equivalent to a+d*l modulo 9. Hence we will check for all multiples of l*d from 1 to 8, and return the minimum found.
One advancement in the program can be made using the fact that digit sum will never be less than 1, so if we have digit sum = 1, we will return it.
Program to illustrate the working of our solution,
Example
#include <iostream> using namespace std; int calcDigitSum(int n) { int i = n % 9; if (i == 0) return 9; else return i; } int findMinDigitSum(int n, int d) { int minSum = 10; int number; for (int i = 1; i < 9; i++) { number = (n + i * d); minSum = min(minSum, calcDigitSum(number)); if(minSum == 1) return minSum; } return minSum; } int main() { int n = 5230, d = 54; cout<<"The minimum possible digitsum after adding the number is "<<findMinDigitSum(n, d); return 0; }
Output
The minimum possible digitsum after adding the number is 1
- Related Articles
- Represent a Number as Sum of Minimum Possible Pseudo-Binary Numbers in C++
- Program to find minimum possible maximum value after k operations in python
- Greater possible digit difference of a number in JavaScript
- Find the sum of the largest 5 -digit number and the smallest 6 -digit number.
- Minimum number of single digit primes required whose sum is equal to N in C++
- Negative number digit sum in JavaScript
- Program to find minimum number colors remain after merging in Python
- Find the Number which contain the digit d in C++
- 8085 program to find minimum value of digit in the 8 bit number
- C program to find sum of digits of a five digit number
- C++ program to find sum of digits of a number until sum becomes single digit
- C++ program to find minimum possible number of keyboards are stolen
- Digit sum upto a number of digits of a number in JavaScript
- Java Program to find minimum sum of factors of a number
- Program to find minimum possible integer after at most k adjacent swaps on digits in Python
