Problem · Math
Count Numbers with Digit Sum
Learn this problemProblem statement
Given an integer X, write an algorithm to find the number of integers which are less than or equal to X and whose digits add up to Y.
Description Input
The first line of input consists of an integer - inputNum1, representing the given number X. The next line consists of an integer - inputNum2, representing the given number Y.
Output
Print the count of numbers whose digits add up to Y for the given number X.
Note
If no numbers are found whose digits add up to Y for the given number X, then print -1.
Function
countNumbersWithDigitSum(inputNum1: int, inputNum2: int) → intExamples
Example 1
inputNum1 = 20inputNum2 = 5return = 2X is 20 and Y is 5. There are only 2 integers <= 20, i.e., 5 and 14 whose digits add up to 5.
Constraints
O~OMore Cisco problems
- Longest Non-Repeating SubstringPHONE SCREEN · Seen Jul 2026
- Collect CoinsOA · Seen Jun 2025
- FizzBuzz ProblemOA · Seen May 2025
- Find Largest Sum Contiguous SubarrayOA · Seen May 2025
- Find Largest Sum of Continuous SequenceOA · Seen May 2025
- Find Palindrome Sub-stringOA · Seen May 2025
- Maximum Chocolates from Jars (L.C. 198 :)OA · Seen Mar 2025
- Find Elements Largest in Row Smallest in ColumnOA · Seen Mar 2025