TopcoderARCHIVE
Archive/Problems/PickTwoMatch
SRM · Problem 18004

PickTwoMatch

Problem statement, definition, constraints, and public examples.

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.
← All problems