Click here to Skip to main content
15,910,234 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Ginger the horse is going for brunch with his friends. He has been collecting the fashion trends magazine for a while, and he knows that it is trendy to wear all horseshoes of a different colors. Ginger has got four horseshoes left from the previous year, but maybe some of the horseshoes have the same color. So he needs to go to the shop and buy a few more horseshoes.

Surprisingly, the shop sells horseshoes of all colors under the sun and Ginger has enough money to buy any four of them. However, to save money, he would like to spend as little money as possible. Find the minimum number of horseshoes he needs to buy to wear four horseshoes of different colors to a brunch.

Input Format

Input consists four space-separated integers k1, k2, k3, k4 the colors of horseshoes Ginger has and consider all possible colors indexed with integers.

Constraints

(1 ≤ k1, k2, k3, k4 ≤ 10^9)
Output Format

Print single positive integer that is the minimum number shoes that he needs to shop for his brunch.

Sample Input 0

1 7 3 3
Sample Output 0

1
Explanation 0

In the input, the colours (indexed with integers) 1,7,3,3 represent the different colours of horseshoes that Ginger has. We can see that he has two of the same colour which would make it impossible for him to wear four different horseshoes at the brunch. That is why, Ginger needs to buy 1 horseshoe to make four different horseshoes.

Sample Input 1

255635360 732742923 798648949 883146723
Sample Output 1

0

What I have tried:

Java
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;

public class Solution {
    public static void main(String args[] ) throws Exception {
        /* Enter your code here. Read input from STDIN. Print output to STDOUT */
        
        
         
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        long[] arr = new long[4];
        for(int i = 0;i<n;i++){
            arr[i] = sc.nextInt();
        }
        for(int i=0;i<n;i++){
            for(int j=0;j<n;j++){
                if(arr[i] == arr[j]){
                    System.out.println("1");
                }
               if(arr[i] != arr[j]){
                   System.out.println("0");
               }
                }
            }
        }
        
    }
Posted
Updated 27-Jun-22 8:41am
v2

Read the question again, and pay attention to the examples, and the explanation they give for why.

Then remember that a horse has 4 legs (except Sleipnir which had 8), so the result will always be be between 0 and 3.
The simplest way is to use the Java Arrays.sort method[^] which orders the inputs: then identical values are next to each other and it becomes very easy to count distinct values in a single pass.
 
Share this answer
 
Your code do not behave the way you expect, or you don't understand why !

There is an almost universal solution: Run your code on debugger step by step, inspect variables.
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't know what your code is supposed to do, it don't find bugs, it just help you to by showing you what is going on. When the code don't do what is expected, you are close to a bug.
To see what your code is doing: Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.

Debugger - Wikipedia, the free encyclopedia[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]

jdb - The Java Debugger[^]
https://www.jetbrains.com/idea/help/debugging-your-first-java-application.html[^]

The debugger is here to only show you what your code is doing and your task is to compare with what it should do.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900