Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.company;
- import java.util.Arrays;
- import java.util.HashMap;
- import java.util.HashSet;
- import java.util.Scanner;
- public class Problem {
- public static void main(String[] args){
- Scanner sc = new Scanner(System.in);
- int n = sc.nextInt();
- int[][] likes = new int[n][2];
- for(int i=0; i < n; i++){
- likes[i][0] = sc.nextInt();
- likes[i][1] = sc.nextInt();
- }
- int[] result_array = helpMocha(likes, n);
- for(int a : result_array)
- System.out.print(a + " ");
- System.out.println();
- }
- public static int[] helpMocha(int[][] likes, int n) {
- HashMap<Integer, HashSet<Integer>> mp = new HashMap<>();
- HashSet<Integer> resultSet = new HashSet<>();
- for (int[] like : likes) {
- if (!mp.containsKey(like[0])) {
- mp.put(like[0], new HashSet<>());
- }
- HashSet<Integer> temp = mp.get(like[0]);
- temp.add(like[1]);
- mp.put(like[0], temp);
- if (mp.containsKey(like[1]) && mp.get(like[1]).contains(like[0])) {
- resultSet.add(like[0]);
- resultSet.add(like[1]);
- }
- }
- if(resultSet.isEmpty())
- return new int[]{-1};
- int[] result_array = new int[resultSet.size()];
- int i=0;
- for(int a : resultSet){
- result_array[i] = a;
- i++;
- }
- Arrays.sort(result_array);
- return result_array;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement