import java.util.*;
import java.lang.*;
import java.io.*;

// The main method must be in a class named "Main".
class Main {
    public static void main(String[] args) {
        String[] friends = {"muzi", "ryan", "frodo", "neo"};
        String[] gifts = {"muzi frodo", "muzi frodo", "ryan muzi", "ryan muzi", "ryan muzi", "frodo muzi", "frodo ryan", "neo muzi"};
        
        String[] friends_2 = {"a", "b", "c"};
        String[] gifts_2 = {"a b", "b a", "c a", "a c", "a c", "c a"};
        
        System.out.println(solution(friends, gifts));
        //System.out.println(solution(friends_2, gifts_2));
    }
    public static int solution(String[] friends, String[] gifts) {
        int answer = 0;
        
        HashMap<String, Integer> map = new HashMap<>();
        
        String[] stArr = new String[friends.length*(friends.length-1)];
        int[] give = new int[friends.length];
        int[] take = new int[friends.length];
        int[] moreTake = new int[friends.length];
        
        for (int i = 0; i < friends.length; i++) {
            for (int j = 0; j < friends.length; j++) {
                if(friends[i].equals(friends[j]))
                    continue;
                
                stArr[i] = (friends[i] + " " + friends[j]);
                map.put(stArr[i], 0);
            }
        }
        
        for (String gift : gifts) {
            map.put(gift, map.getOrDefault(gift, 0) + 1);
            for (String friend : friends) {
                if(gift.split(" ")[0].equals(friend)) {
                    give[Arrays.asList(friends).indexOf(friend)]++;
                    take[Arrays.asList(friends).indexOf(gift.split(" ")[1])]++;
                }
            }
        }
        
        for (Map.Entry<String, Integer> entry : map.entrySet()) {
            String reverse = entry.getKey().split(" ")[1]+" "+entry.getKey().split(" ")[0];
            int left = Arrays.asList(friends).indexOf(entry.getKey().split(" ")[0]);
            int right = Arrays.asList(friends).indexOf(entry.getKey().split(" ")[1]);
            
            if(entry.getValue() > map.get(reverse)) {
                moreTake[left]++;
            }
            if((entry.getValue() == 0 && map.get(reverse) == 0) || (entry.getValue() == map.get(reverse))) {
                if(give[left] - take[left] > give[right] - take[right]) {
                    moreTake[left]++;
                }
            }
        }
        
        for (int mr : moreTake) {
            if(mr > answer)
                answer = mr;
        }
        
        return answer;
    }
}

Embed on website

To embed this project on your website, copy the following code and paste it into your website's HTML: