Sort by

recency

|

365 Discussions

|

  • + 1 comment

    I think the test case 5 is wrong. Does anybody have the same opinion? I am totally confused...

  • + 1 comment

    Passed Test

    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) { Scanner s = new Scanner(System.in); int t = s.nextInt(); String [] pair_left = new String[t]; String [] pair_right = new String[t];

        for (int i = 0; i < t; i++) {
            pair_left[i] = s.next();
            pair_right[i] = s.next();
        }
       // Use a HashSet to store unique pairs
        Set<String> uniquePairs = new HashSet<>();
    
        for (int i = 0; i < t; i++) {
            String[] pair = {pair_left[i], pair_right[i]};
            Arrays.sort(pair); // Sort the pair to ensure consistent ordering
            String sortedPair = pair[0] + "," + pair[1];
            uniquePairs.add(sortedPair);
            System.out.println(uniquePairs.size());
        }
    

    } }

  • + 1 comment

    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());
                }
            }