FastPrepCount Lucky Tickets

Count Lucky Tickets

The D.E. Shaw Group logoThe D.E. Shaw Group● MediumFULLTIMEOA

Problem statement

A ticket is a string of exactly n decimal digits, where n is even. Leading zeros are allowed, so 0000 is one four-digit ticket.

A ticket is lucky when the sum of its first n / 2 digits equals the sum of its last n / 2 digits.

Return the exact number of lucky tickets of length n. Different digit strings count separately. Do not take a modulus.

Function

countLuckyTickets(n: int) → long

Examples

Example 1

n = 2return = 10

Each half has one digit, so the two digits must be equal. The lucky tickets are 00, 11, 22, ..., 99, giving 10 tickets.

Example 2

n = 4return = 670

A two-digit half has digit-sum counts 1, 2, ..., 9, 10, 9, ..., 2, 1 for sums from 0 through 18. Pairing two halves with the same sum gives 2 * (1^2 + 2^2 + ... + 9^2) + 10^2 = 670 tickets.

Constraints

  • For this exercise, assume 2 <= n <= 16.
  • The number of digits is even.
  • The answer fits in a signed 64-bit integer.

More The D.E. Shaw Group problems

See The D.E. Shaw Group hiring insights
Saved
public long countLuckyTickets(int n) {
    // Write your code here.
}
Checking account…