Problem Statement
You have a collection of socks of various colors. You are given a int[] socks that describes this collection: each element of socks is the color of one of the socks. You consider two socks to be acceptable as a pair if and only if the absolute value of difference of their colors is less than or equal equal to colorDiff.
In the morning you will choose two different socks from your collection uniformly at random. Compute and return the probability that the two random socks will form an acceptable pair.
Definition
- Class:
- RainbowSocks
- Method:
- getPairProb
- Parameters:
- int[], int
- Returns:
- double
- Method signature:
- double getPairProb(int[] socks, int colorDiff)
- (be sure your method is public)
Notes
- Each sock from your collection can be used on either foot.
- Your return value must have an absolute or a relative error smaller than 1e-9.
Constraints
- socks will contain between 2 and 50 elements, inclusive.
- Each element of socks will be between 1 and 50, inclusive.
- colorDiff will be between 0 and 49, inclusive.
Examples
{1, 2, 3, 4, 5, 6, 7}7
Returns: 1.0
You are not particularly picky, so any pair of socks is acceptable.{1, 50, 50, 1}0
Returns: 0.3333333333333333
There are 6 ways to pick a pair of socks from this collection: one pair (1, 1), one pair (50, 50) and four different pairs (1, 50). Only the first two pairs are acceptable.{44, 14, 24, 31, 30, 32, 46, 23, 27, 2}24
Returns: 0.8222222222222222