Sort by

recency

|

363 Discussions

|

  • + 0 comments

    All tests passed...

    public class Solution {
    
        public static void main(String[] args) {
           Scanner in = new Scanner(System.in);
           int pairs = in.nextInt();
          
           HashSet<String> set = new HashSet<>();
           in.nextLine();
           
           while(pairs-- > 0){
                String left = in.next().trim();
                String right = in.next().trim();
                String pairLR = left + " " + right;
                String pairRL = right + " " + left;
                if(!set.contains(pairRL))
                    set.add(pairLR);
                System.out.println(set.size());
           }
           
           in.close();
        }
    }
    
  • + 1 comment
    import java.io.*;
    import java.util.*;
    
    public class Solution {
    
        public static void main(String[] args) {
            Scanner scan = new Scanner(System.in);
            
            int n = scan.nextInt();
            scan.nextLine();
            
            HashSet<String> pairs = new HashSet<>();
            int numUnique = 0;
            
            for (int i = 0; i < n; i++) {
                String line = scan.nextLine();
                if (!pairs.contains(line)) {
                    numUnique++;
                    pairs.add(line);
                }
                System.out.println(numUnique);
            }        
            
            scan.close();
        }
    }
    

    Hi guys. This is my solution but it is failing on the 5th test case. Could anyone tell me where I am going wrong?

  • + 0 comments

    It seems they have bug. 5th test is failing and the solution provided in Editorial was exactly the same code I had. Hackerrank should fix the issue on this problem.

    Set<String> listOfUsers = new HashSet<>();
            if (t>=1 && t<=100000){
                for (int counter = 0; counter < pair_left.length; counter++) {
                    listOfUsers.add(pair_left[counter] + " " + pair_right[counter]);
                    System.out.println(listOfUsers.size());
                }
            }
    
  • + 1 comment

    Scanner s = new Scanner(System.in); int t = s.nextInt();

        // Stream to read and process input pairs
        Set<String> uniquePairs = new HashSet<>();
    
        // We use a Stream to process the input and maintain uniqueness
        IntStream.range(0, t).forEach(i -> {
            String left = s.next();
            String right = s.next();
    
            // Create the two possible pair strings
            String pair1 = left + " " + right;
            String pair2 = right + " " + left;
    
            // Add the lexicographically smaller of the two pairs to the set
            uniquePairs.add(Stream.of(pair1, pair2)
                                  .min(String::compareTo) // Get the lexicographically smaller string
                                  .get());
    
            // Print the current number of unique pairs
            System.out.println(uniquePairs.size());
        });
    
  • + 0 comments

    If you got a problem with the test case 5 try with lexicographic order:

     Set<String> hs = new HashSet<>();
            for (int i = 0; i < t; i++) {
                String first = pair_left[i];
                String second = pair_right[i];
    
                if (first.compareTo(second) > 0) {
                    String temp = first;
                    first = second;
                    second = temp;
                }
    
                hs.add(first + "," + second);
                System.out.println(hs.size());
            }