Codeforces 228A Is your horseshoe on the other hoof? Solution & Explanation

Difficulty : 800

Problem Description

Valera the Horse is going to the party with friends. He has been following the fashion trends for a while, and he knows that it is very popular to wear all horseshoes of different color. Valera has got four horseshoes left from the last year, but maybe some of them have the same color. In this case he needs to go to the store and buy some few more horseshoes, not to lose face in front of his stylish comrades.

Fortunately, the store sells horseshoes of all colors under the sun and Valera has enough money to buy any four of them. However, in order to save the money, he would like to spend as little money as possible, so you need to help Valera and determine what is the minimum number of horseshoes he needs to buy to wear four horseshoes of different colors to a party.


The first line contains four space-separated integers s1, s2, s3, s4 (1 ≤ s1, s2, s3, s4 ≤ 109) — the colors of horseshoes Valera has.

Consider all possible colors indexed with integers.


Print a single integer — the minimum number of horseshoes Valera needs to buy.


Input1 7 3 3
Input7 7 7 7

There was just one color, so he needs three more colors.


In this problem, we need to find out the distinct color numbers of the current horseshoes. If it is less than 4, we need to buy the remaining ones.

C# Solution

Solution1 – HashSet

string[] arr = Console.ReadLine().Split(' ').ToArray();
HashSet<string> hSet = new HashSet<string>();

foreach(var i in arr){

int res = 4 - hSet.Count;

HashSet’s Add method automatically inserts and ignores duplicates.

See another question about using Count of HashSet ➡Codeforces 236A Boy or Girl

Solution2 – LINQ

string[] arr = Console.ReadLine().Split(' ').ToArray();

int res = 4 - arr.Distinct().Count();


🧡If my solution helps, that is my honor!

🧡You can support me by sharing my posts, thanks you~~

✅If you got any problem about the explanation or you need other programming language solution, please feel free to let me know !!

The problem link : Problem – 228A – Codeforces

Random Codeforces Posts

Leave a Reply

Your email address will not be published. Required fields are marked *