Connection Status:
Competition Arena > PickTwoMatch
Rookie SRM 19 · 2023-03-17 · by erinn · Math
Class Name: PickTwoMatch
Return Type: double
Method Name: chance
Arg Types: (vector<int>)
Problem Statement

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?

Constraints

  • cards will contain between 2 and 50 elements, inclusive.
  • Each element of cards will be between 1 and 10, inclusive.
Examples
0)
{ 1, 1, 1 }
Returns: 1.0

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

1)
{ 1, 2, 2 }
Returns: 0.3333333333333333

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

2)
{ 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.

3)
{ 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
4)
{ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 }
Returns: 1.0

Submissions are judged against all 8 archived test cases, of which 5 are shown here. Case numbers match the judge’s.

Coding Area

Language: C++17 · define a public class PickTwoMatch with a public method double chance(vector<int> cards) · 8 test cases · 2 s / 256 MB per case

Submitting as anonymous