Problem Statement
You have quite a few items that you want to keep in the bag, and you are wondering if you put all the items in the bag randomly, what are the odds that you'll be able to reach a certain item. Clearly you will only be able to reach that item if no items as big or bigger than it were put into the bag after it, and the ordering of items smaller than the item in question doesn't matter. Create a class BagOfHolding with a method oddsReachable that takes a
Definition
- Class:
- BagOfHolding
- Method:
- oddsReachable
- Parameters:
- int[], int
- Returns:
- double
- Method signature:
- double oddsReachable(int[] sizes, int item)
- (be sure your method is public)
Constraints
- sizes will contain between 1 and 10 elements, inclusive.
- Every element of sizes will be between 1 and 10000, inclusive.
- item will be between 0 and the number of elements in sizes-1, inclusive.
Examples
{1,2,3}
1
Returns: 0.5
There are six orders in which these items could be put in the bag: {1,2,3}, {1,3,2}, {2,1,3}, {2,3,1}, {3,1,2}, {3,2,1}. We want to be able to reach the item with a size of 2, and we will be able to in 3 of the 6 orderings, so we return .5.
{1,2,3}
2
Returns: 1.0
Same items, but this time we want to be able to reach the item with a size of 3. Since it is larger than all of the other items we will always be able to reach it, so we return 1.
{1,1,2,3}
2
Returns: 0.5
Like example 0, but now there is another item of size 1. Remember that {1,1,2,3} can be two different orderings, since there are two distinct items of size 1.
{1,2,3,4,5,6,7,8,9,10}
4
Returns: 0.16666666666666666
{1,2,3,4,5,6,7,8,9,10}
0
Returns: 0.1
{1,2,3,4,5,6,7,8,9,10}
0
Returns: 0.1
{1,2,3,4,5,6,7,8,9,10}
5
Returns: 0.2
{1,2,3,4,5,6,7,8,9,10}
9
Returns: 1.0
{1,10,100,1000,10000,1,10,100,1000,10000}
3
Returns: 0.25
{10000,9000,8000,7000,6000,5000,4000,3000,2000,1000}
3
Returns: 0.25
{1}
0
Returns: 1.0
{10000}
0
Returns: 1.0
{1576,4085,2782,5475,900,9953,6186,7887}
7
Returns: 0.5
{7867,5852,5289,8322,9367,520,8053}
3
Returns: 0.5
{1073,522,4399,1616}
0
Returns: 0.3333333333333333
{3707,8316,153,1282,7623}
0
Returns: 0.3333333333333333
{6686,3123,9780,5105,1866}
1
Returns: 0.25
{6525,214,3835,8652,9593,2066,55,6248,2213,5758}
6
Returns: 0.1
{7729,2266,3263,7984,3181}
0
Returns: 0.5
{156,1817,2754,3863,9160,6677}
1
Returns: 0.2
{8404,8097,6762,7519,2480,8045,8294}
6
Returns: 0.5
{1689,1570,2119,1944,9797,7822,6563}
0
Returns: 0.16666666666666666
{656,6925,9654,3099,5036}
2
Returns: 1.0
{2839,7640,4117,3806,6259,7796}
4
Returns: 0.3333333333333333
{3792,8397,6070,1691}
3
Returns: 0.25
{4466,8999,2376,1781,3278,7923,2020,8401,7850,47}
5
Returns: 0.3333333333333333
{8518,7499,8268,4937,7554,8551,2616}
6
Returns: 0.14285714285714285
{1651,1028,8993,7498,7119,2352,3021,7510}
2
Returns: 1.0
{5874,6199,7944,9349,6142,9123,4644,7105}
1
Returns: 0.2
{1901,4934,5899,8972,3481,1122,711,3720}
4
Returns: 0.2
{8083,3220,8209,58,9951,96,4861,2246,3307}
3
Returns: 0.1111111111111111
{343,3864,1189,7038,3299,1364,5825,7994}
6
Returns: 0.3333333333333333
{2669,5014,1243,2908,2089,7250,151,4456,7301}
8
Returns: 1.0
{7892,3297,9866,1071,3776,4365,166}
4
Returns: 0.25
{3305,8626,6471,1928,9845,2254,179,372}
0
Returns: 0.25
{9037,1478,4011,6940,8505,5250,8823,4904}
4
Returns: 0.3333333333333333
{2739,612,5937,3594,4991,6720,9485,6822,603,8124}
4
Returns: 0.16666666666666666
{ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }
4
Returns: 0.16666666666666666
{ 1, 1, 2, 3 }
2
Returns: 0.5
{ 1, 2, 3, 4, 5, 6, 6, 6, 7, 8 }
6
Returns: 0.2
{ 1, 2, 3 }
1
Returns: 0.5
{ 1, 1, 2, 3 }
1
Returns: 0.25
{ 1, 2, 2 }
2
Returns: 0.5
{ 2, 5, 3, 4, 9, 9, 9, 9, 9, 9 }
0
Returns: 0.1
{ 1, 1, 1, 1, 1 }
1
Returns: 0.2
{ 1, 2, 3, 2 }
1
Returns: 0.3333333333333333
{ 2, 2, 2, 2 }
0
Returns: 0.25
{ 5, 5, 5, 5 }
3
Returns: 0.25
{ 5, 5, 5, 5, 5, 5, 5, 5, 5, 5 }
6
Returns: 0.1
{ 1, 1, 2, 3 }
0
Returns: 0.25