FastPrepOrdered Book Displays with a Featured Book

Ordered Book Displays with a Featured Book

IMC Trading logoIMC Trading● EasyNEW GRADOA
Learn

Problem statement

A library owns n distinct books. Create a display by choosing k different books and arranging them in a specific order.

After arranging the display, choose exactly one of its books as the featured Book of the Month. Two results differ if their ordered display differs or their featured book differs.

Return the number of possible display-and-feature choices. A book cannot appear twice in a display, and the featured book must belong to the display.

For this exercise, assume n and k generalize the reported thirty-book, four-position question. If k = 0 or k > n, return 0: no featured displayed book can be selected.

Complete countBookDisplays(n, k) and return the exact count as a signed 64-bit integer.

Function

countBookDisplays(n: int, k: int) → long

Examples

Example 1

n = 30k = 4return = 2630880

The four ordered positions have 30 * 29 * 28 * 27 = 657720 possible assignments. Each has four possible featured books, so the count is 657720 * 4 = 2630880.

Example 2

n = 3k = 2return = 12

The ordered pairs are [A,B], [A,C], [B,A], [B,C], [C,A], and [C,B]. Each pair has two featured choices, giving 12.

Example 3

n = 4k = 0return = 0

An empty display contains no book to feature.

Example 4

n = 2k = 3return = 0

Three distinct displayed books cannot be selected from only two books.

Constraints

  • For this exercise, assume 0 <= n <= 50 and 0 <= k <= 10.
  • Books are distinct and may be selected only once.
  • The exact answer and the intermediates in the presented solutions fit in signed 64-bit arithmetic.

More IMC Trading problems

See IMC Trading hiring insights
public long countBookDisplays(int n, int k) {
    // Write your solution here.
}
n30
k4
expected2630880
Checking account…