Statistics

Problem Statement for "SortInversions"

Problem Statement

Bob had an array that contained the integers from 1 to N, inclusive. He converted each integer into a string. Then, he sorted the array. Finally, he converted each string back into an integer.

Count and return the number of inversions in Bob's final array.

Definition

Class:
SortInversions
Method:
count
Parameters:
int
Returns:
long
Method signature:
long count(int N)
(be sure your method is public)

Notes

  • A pair of indices (i, j) into an array B is an inversion if and only if (i < j and B[i] > B[j]). In other words, an inversion is a pair of elements such that the bigger element is to the left of the smaller one.

Constraints

  • N will be between 1 and 10^9, inclusive.

Examples

  1. 12

    Returns: 24

    Bob had the array {1, 2, ..., 9, 10, 11, 12}. He converted the elements to strings: {"1", "2", ..., "9", "10", "11", "12"}. He sorted those strings: {"1", "10", "11", "12", "2", ..., "9"}. He converted everything back to integers: {1, 10, 11, 12, 2, ..., 9}. This array now contains 24 inversions.

  2. 7

    Returns: 0

  3. 199

    Returns: 9610

  4. 1

    Returns: 0

  5. 10

    Returns: 8

  6. 17

    Returns: 64

  7. 47

    Returns: 250

  8. 98

    Returns: 360

  9. 99

    Returns: 360

  10. 100

    Returns: 457

  11. 1000000000

    Returns: 45454542010101000

  12. 986765889

    Returns: 45444810963558720

  13. 997987989

    Returns: 45454316117225460

  14. 909876567

    Returns: 45003306204116886

  15. 899999999

    Returns: 44898985860101010

  16. 888888888

    Returns: 44768670401047512

  17. 800000000

    Returns: 43232319632323224

  18. 777000000

    Returns: 42691819722378781

  19. 777000777

    Returns: 42691838974674326

  20. 967876654

    Returns: 45397212833254550

  21. 76543210

    Returns: 423977313421975

  22. 7457453

    Returns: 4186286830268

  23. 46467

    Returns: 295218548

  24. 5757

    Returns: 3533570

  25. 498756453

    Returns: 31496482464133056

  26. 999999

    Returns: 45451601010

  27. 1000

    Returns: 45006


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: