Statistics

Problem Statement for "NumberPartition"

Problem Statement

A partition of the number n is the set of numbers that all sum up to n. For example, we have 5 different partitions of 4: {1, 1, 1, 1}, {1, 1, 2}, {1, 3}, {2, 2}, {4}. If we sort all the numbers within each set, we can treat each partition as a sorted list of numbers and so introduce a lexicographic order on all partitions. For example, the partitions of 4 mentioned before are given in order. You are given ints n and k. Determine all the partitions of n, sort them in lexicographical order, and return a int[] containing the k-th one (k is a 1-based index). If there are not enough partitions of n, return an empty int[].

Definition

Class:
NumberPartition
Method:
kthPartition
Parameters:
int, int
Returns:
int[]
Method signature:
int[] kthPartition(int n, int k)
(be sure your method is public)

Notes

  • The lexicographical order is exactly the same as in a dictionary. A int[] A is lexicographically before a int[] B if there exists an integer i such that A[i] < B[i] and A[j] = B[j] for all j < i or if A is a proper prefix of B.

Constraints

  • n will be between 1 and 50, inclusive.
  • k will be between 1 and 1000000, inclusive.

Examples

  1. 4

    1

    Returns: {1, 1, 1, 1 }

    Example from the problem statement.

  2. 4

    3

    Returns: {1, 3 }

  3. 17

    123

    Returns: {1, 1, 1, 3, 3, 3, 5 }

  4. 12

    1234

    Returns: { }

    There are less then 1234 partitions of 12.

  5. 23

    1234

    Returns: {4, 19 }

  6. 50

    1

    Returns: {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 }

  7. 50

    2

    Returns: {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2 }

  8. 50

    204226

    Returns: {50 }

  9. 50

    204227

    Returns: { }

  10. 50

    200000

    Returns: {3, 4, 5, 5, 5, 28 }

  11. 50

    204200

    Returns: {13, 16, 21 }

  12. 47

    123456

    Returns: {4, 5, 5, 5, 7, 7, 7, 7 }

  13. 43

    12342

    Returns: {1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 3, 6, 6 }

  14. 23

    42354

    Returns: { }

  15. 45

    23434

    Returns: {1, 1, 1, 1, 1, 1, 1, 2, 2, 3, 4, 6, 6, 15 }

  16. 43

    59999

    Returns: {2, 4, 7, 7, 11, 12 }

  17. 11

    32

    Returns: {1, 2, 2, 2, 4 }

  18. 32

    8222

    Returns: {4, 5, 9, 14 }

  19. 49

    159567

    Returns: {2, 2, 3, 5, 11, 13, 13 }

  20. 47

    59999

    Returns: {1, 1, 1, 1, 2, 4, 7, 7, 11, 12 }

  21. 17

    234

    Returns: {2, 2, 2, 2, 2, 3, 4 }

  22. 45

    14253

    Returns: {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 3, 3, 3, 7, 19 }

  23. 46

    48230

    Returns: {1, 1, 1, 1, 2, 2, 3, 4, 5, 5, 5, 7, 9 }

  24. 23

    5343

    Returns: { }

  25. 43

    60454

    Returns: {3, 3, 3, 3, 3, 3, 3, 3, 6, 13 }

  26. 43

    62497

    Returns: {4, 4, 11, 12, 12 }

  27. 7

    24

    Returns: { }

  28. 7

    11

    Returns: {1, 6 }

  29. 7

    5

    Returns: {1, 1, 1, 4 }

  30. 39

    1234

    Returns: {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 4, 19 }

  31. 41

    40431

    Returns: {2, 2, 3, 4, 8, 22 }

  32. 50

    27

    Returns: {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 3, 3, 3 }

  33. 1

    1

    Returns: {1 }

  34. 1

    1000000

    Returns: { }

  35. 50

    1000000

    Returns: { }

  36. 2

    1

    Returns: {1, 1 }

  37. 2

    2

    Returns: {2 }

  38. 3

    1

    Returns: {1, 1, 1 }

  39. 3

    2

    Returns: {1, 2 }

  40. 3

    3

    Returns: {3 }

  41. 4

    2

    Returns: {1, 1, 2 }

  42. 4

    4

    Returns: {2, 2 }

  43. 4

    5

    Returns: {4 }

  44. 50

    54321

    Returns: {1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 4, 7, 10, 10 }

  45. 50

    10000

    Returns: {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 4, 5, 12, 12 }


This problem statement is the exclusive and proprietary property of TopCoder, Inc. Any unauthorized use or reproduction of this information without the prior written consent of TopCoder, Inc. is strictly prohibited. (c)2024, TopCoder, Inc. All rights reserved.
This problem was used for: