Statistics

Problem Statement for "PickTwoMatch"

Problem Statement

You have a set of cards, each with a number from 1 through 10 printed on it. The values of all the cards you have are given in int[] cards.

If you randomly select two cards, what are the chances they have the same value?

Definition

Class:
PickTwoMatch
Method:
chance
Parameters:
int[]
Returns:
double
Method signature:
double chance(int[] cards)
(be sure your method is public)

Constraints

  • cards will contain between 2 and 50 elements, inclusive.
  • Each element of cards will be between 1 and 10, inclusive.

Examples

  1. { 1, 1, 1 }

    Returns: 1.0

    With all the cards the same value, the two selected are guaranteed to match.

  2. { 1, 2, 2 }

    Returns: 0.3333333333333333

    There are three ways to pick two cards, and exactly one of those three gives a match.

  3. { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }

    Returns: 0.0

    It's pretty hard to get a match when all the cards are different.

  4. { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }

    Returns: 0.08163265306122448

  5. { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 }

    Returns: 1.0

  6. {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 }

    Returns: 1.0

  7. {1, 1 }

    Returns: 1.0

  8. {1, 1, 1, 2, 2, 2 }

    Returns: 0.4


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: