Statistics

Problem Statement for "Stats"

Problem Statement

A common problem in dealing with data is to determine how "spread out" the data is. Two commonly used measures are the range (the maximum value minus the minimum value) and the standard deviation.

We propose using the Half-Range, which we define as the smallest number, N, such that at least half of the data values are contained within some interval of size N. (At least half of 17 or 18 values would be 9 or more.) The size of an interval is its maximum value minus its minimum value.

For example, if our data values are {1,2,4,5,6,8}, then the Half-Range is 2 because the interval from 4 to 6 (size 2) contains half of the values (4, 5, and 6) and furthermore, there is no interval whose size is less than 2 which contains half of the values.

Create a class Stats that contains a method range that is given a int[] data and returns its Half-Range.

Definition

Class:
Stats
Method:
range
Parameters:
int[]
Returns:
int
Method signature:
int range(int[] data)
(be sure your method is public)

Constraints

  • data will contain between 1 and 50 elements inclusive
  • each element of data will be between -1,000,000 and 1,000,000 inclusive

Examples

  1. {9,2,3,8,8}

    Returns: 1

    The interval from 8 to 9 includes 3 of the 5 values.

  2. {9,2,8,8}

    Returns: 0

    The interval from 8 to 8 contains 2 of the 4 values.

  3. {1000000,-3,-1000000}

    Returns: 999997

  4. {231000,344,3440,34400,-1,19,19,802}

    Returns: 345

  5. {231000,344,3440,34400,-1,19,19,802,-60000}

    Returns: 803

  6. {9,9,3,8,8}

    Returns: 1

  7. {1,2,3,4,5,1,2,3,4,5,1,2,3,4,5,1,2,3,4,5,1,2,3,4,5, 1,2,3,4,5,1,2,3,4,5,1,2,3,4,5,1,2,3,5,1,2,3,4,5,6}

    Returns: 2

  8. {9,8,8,6,5,4}

    Returns: 1

  9. {-9,-8,-7,-6,0,0,0,2}

    Returns: 2

    Four values lie in the interval from 0 to 2

  10. {3,8,9,15,16,22}

    Returns: 6

  11. {3,8,9,15,16,22}

    Returns: 6

  12. {8,9}

    Returns: 0

  13. {1000000}

    Returns: 0

  14. {-1000000,1000000,-900000,900000,-800000,800000, -700000,700000, -600000,600000,-500000,500000, -400000,400000,-300000,300000,-200000,200000, -100000,100000,0,0,-400922}

    Returns: 900000

  15. {-1000000,1000000,-900000,900000,-800000,800000, -700000,700000, -600000,600000,-500000,500000, -400000,400000,-300000,300000,-200000,200000, -100000,100000,0,0,-400922,-321111}

    Returns: 800000

  16. {999999,-999998,999997,-999996,999995,-999994, 999999,-999998,999997,-999996,999995,-999994, 999999,-999998,999997,-999996,999995,-999994, 999999,-999998,999997,-999996,999995,-999994, 999999,-999998,999997,-999996,999995,-999994, 999999,-999998,999997,-999996,999995,-999994, 999999,-999998,999997,-999996,999995,-999994, 999999,-999998,999997,-999996,999995,-999994, 999999,-999998}

    Returns: 4

  17. {999999,-999998,999997,-999996,999995,-999994, 999999,-999998,999997,-999996,999995,-999994, 999999,-999998,999997,-999996,999995,-999994, 999999,-999998,999997,-999996,999995,-999994, 999999,-999998,999997,-999996,5,2, 999999,-999998,999997,-999996,999995,-999994, 999999,-999998,999997,-999996,999995,-999994, 999999,-999998,999997,-999996,999995,-999994, 999999,-999998}

    Returns: 999994

  18. {1,2,3,4,5,1,2,3,4,5,1,2,3,4,5,1,2,3,4,5,1,2,3,4,5, 101,102,103,104,105,101,102,103,104,105,101,102, 103,104,105,101,102,103,105,101,102,103,104,105,106}

    Returns: 4

  19. {1,2,3,4,5,1,2,3,4,5,1,2,3,4,5,1,2,3,4,5,1,2,3,4,100005, 101,102,103,104,105,101,102,103,104,105,101,102, 103,104,105,101,102,103,105,101,102,103,104,105,106}

    Returns: 5

  20. {1,2,3,4,5,1,2,3,4,5,1,2,3,4,5,1,2,3,4,5,1,2,3,4,100005, 101,102,103,104,105,101,102,103,104,105,101,-100002, 103,104,105,101,102,103,105,101,102,103,104,105,106}

    Returns: 100

  21. { -1000000, -50000, 0, 50000, 1000000 }

    Returns: 100000

  22. { 1, 3 }

    Returns: 0

  23. { 1, 2 }

    Returns: 0

  24. { 60 }

    Returns: 0

  25. { 5 }

    Returns: 0

  26. { 9, 2, 3 }

    Returns: 1

  27. { 8 }

    Returns: 0

  28. { 9 }

    Returns: 0

  29. { -3, 98, 2, -1, 99 }

    Returns: 5

  30. { -1000000, 0, 1000000 }

    Returns: 1000000

  31. { -9, -9, -9, -9, -9, 0, 0, 2 }

    Returns: 0

  32. { 1, 2, 3, 4, 1, 2, 3, 4, 5 }

    Returns: 2


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: