Problem Statement
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
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
{9,2,3,8,8}
Returns: 1
The interval from 8 to 9 includes 3 of the 5 values.
{9,2,8,8}
Returns: 0
The interval from 8 to 8 contains 2 of the 4 values.
{1000000,-3,-1000000}
Returns: 999997
{231000,344,3440,34400,-1,19,19,802}
Returns: 345
{231000,344,3440,34400,-1,19,19,802,-60000}
Returns: 803
{9,9,3,8,8}
Returns: 1
{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
{9,8,8,6,5,4}
Returns: 1
{-9,-8,-7,-6,0,0,0,2}
Returns: 2
Four values lie in the interval from 0 to 2
{3,8,9,15,16,22}
Returns: 6
{3,8,9,15,16,22}
Returns: 6
{8,9}
Returns: 0
{1000000}
Returns: 0
{-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
{-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
{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
{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
{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
{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
{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
{ -1000000, -50000, 0, 50000, 1000000 }
Returns: 100000
{ 1, 3 }
Returns: 0
{ 1, 2 }
Returns: 0
{ 60 }
Returns: 0
{ 5 }
Returns: 0
{ 9, 2, 3 }
Returns: 1
{ 8 }
Returns: 0
{ 9 }
Returns: 0
{ -3, 98, 2, -1, 99 }
Returns: 5
{ -1000000, 0, 1000000 }
Returns: 1000000
{ -9, -9, -9, -9, -9, 0, 0, 2 }
Returns: 0
{ 1, 2, 3, 4, 1, 2, 3, 4, 5 }
Returns: 2