3

I have this problem where I have two people willing to sell some object, but only if they have the same object.

First they say how many objects they would like to sell in order. Example: 3 3 - which indicates that they both would like to sell 3 objects each, whereafter they indicate the number on their objects. If any matches are found there's a counter incrementing. At last the terminal closes iff. the last two integers are two 0's.

Sample input: (I've made some spacing between the two, to make it more clear as to which the integers belong to)

3 3

1
2
3

1
2
4

0 0

Sample output:

2

Another sample input:

5 3

1
2
3
4
5

1
2
3

0 0

'another' Sample Output:

3

This is my code:

import java.util.*;

public class CD {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int testCases = sc.nextInt();
        int testCases1 = sc.nextInt();
        HashSet<Integer> set = new HashSet<Integer>();
        HashSet<Integer> set1 = new HashSet<Integer>();
        for (int i = 0; i < testCases; i++) {
            set.add(sc.nextInt());
        }
        for (int i = 0; i < testCases1; i++) {
            set1.add(sc.nextInt());
        }
        int counter = 0;
        if (set.size() > set1.size() || set.size() == set1.size()) {
            for (Integer x : set1) {
                if (set.contains(x)) {
                    counter++;
                }
            }
        }else{
            for (Integer x : set) {
                if (set1.contains(x)) {
                    counter++;
                }
            }
        }
        int ending = sc.nextInt();
        int ending1 = sc.nextInt();
        if ((ending == 0 && ending1 == 0)) {
            System.out.println(counter);
            sc.close();
        }
    }
}

Also with the sample input my code does fine, but it's not marked as correct with other types of input. Any ideas, suggestions etc? Would love to hear. Best Regards.

EDIT: There can not be any duplicates as the question explicitly says that each person only sells one as a max of each object. So person 1 for instance can't sell object '1' more than once.

Second Edit: I've been notified that another question may have the answer the other similar question. Yet this is somewhat different.

Robert
  • 43
  • 6
  • Can you show us an input that doesn't work as expected, with the expected output VS actual output? – Imaguest Nov 26 '19 at 13:12
  • @Imaguest Can no do unfortunately as for all of the input I've tested it works, but when I go submit it, it comes back as if there's one of the 'checking' inputs which outputs the wrong answer. I can try to find some where it doesn't match the expected output. – Robert Nov 26 '19 at 13:58
  • The case where it could not work is if the users can sell the same object more than once. Check if it's a requirement – Imaguest Nov 26 '19 at 14:10
  • @Imaguest There can _not_ be any dublicates as the question explicitly says that each person only sells one as a max of each object. So person 1 for instance can't sell object '1' more than once. Probably should have included that in my question – Robert Nov 26 '19 at 18:05
  • 2
    This looks like a re-invented wheel. What you want is the size of the intersection of `set` and `set1` (which, by the way, are not very useful names). You can just write `set.retainAll(set1); int counter = set.size();`. It doesn't matter that this destroys the original contents of `set` because they won't be needed afterwards. – kaya3 Nov 26 '19 at 18:31
  • Possible duplicate of [Efficiently finding the intersection of a variable number of sets of strings](https://stackoverflow.com/questions/2851938/efficiently-finding-the-intersection-of-a-variable-number-of-sets-of-strings) – Progman Nov 26 '19 at 18:32
  • @Progman I see why this might be a possible duplicate as the content of the question is quite similar, yet a bit different in some sort I believe. @kaya3 This does work yes, cleaner code so thanks for that. Only problem is that it still wont accept my answer. What I did was change `if statements` and what they contained with what you wrote `set.retainAll(set1); int counter = set.size();` but no success as the second control input to verify my code returned as "wrong answer". This task really makes me stubborn ^^ – Robert Nov 26 '19 at 19:21
  • why is your output 2 in your example? because they both have object 1 and 2? – Bentaye Nov 26 '19 at 19:32
  • @Bentaye I've made another example and made indentation to make it more clear. – Robert Nov 26 '19 at 19:54
  • Do you have logs from when the test runs and fail? – Bentaye Nov 26 '19 at 20:18
  • Unfortunately no as I don't get the logs as when the task marks the code as "wrong answer" on the second control input – Robert Nov 26 '19 at 20:58

0 Answers0