Skip to the code
cf.
+25 XP

#561 Count Ticket Numbers by Digit Sum

medium

An event site prints "lucky" on every ticket whose number has digits that add up to exactly target. Tickets are numbered from 1 to limit.

Write countLuckyTickets(limit, target) that returns how many ticket numbers from 1 to limit (inclusive) have a digit sum equal to target.

Example 1

Input: limit = 25, target = 5
Output: 3

5, 14, and 23.

Example 2

Input: limit = 100, target = 10
Output: 9

19, 28, 37, 46, 55, 64, 73, 82, and 91.

Constraints

  • 1 <= limit <= 10^15
  • 1 <= target <= 150
Saved in this browser

Run tries your code on the examples. Submit runs every test, including hidden edge cases, and solves the problem when they all pass.

Ctrl + Enter runs, Ctrl + Shift + Enter submits (⌘ on a Mac).