LeetCode 3058: Friends With No Mutual Friends Solution

Master LeetCode problem 3058 (Friends With No Mutual Friends), a medium challenge, with our optimized solutions in Java, C++, and Python. Explore detailed explanations, test your code in our interactive editor, and prepare for coding interviews.

3058. Friends With No Mutual Friends

Medium

Problem Explanation

Explanation:

To solve this problem, we can iterate through each pair of friends and check if they have any mutual friends. If they don't have any mutual friends, we consider them as friends with no mutual friends. We can achieve this by creating a set for each friend to store their friends and then compare the sets to find the friends with no mutual friends.

Algorithm:

  1. Create a HashMap to store each friend and their corresponding set of friends.
  2. Iterate through each pair of friends and add each friend to the other friend's set of friends in the HashMap.
  3. Iterate through the HashMap and check if any two friends have an empty intersection of their sets of friends. If so, add them to the result list.
  4. Return the result list containing friends with no mutual friends.

Time Complexity:

The time complexity of this algorithm is O(n^2) where n is the number of friends.

Space Complexity:

The space complexity of this algorithm is O(n) where n is the number of friends.

: :

Solution Code

import java.util.*;

class Solution {
    public List<List<Integer>> findFriendsWithNoMutualFriends(int[][] friends) {
        Map<Integer, Set<Integer>> map = new HashMap<>();
        List<List<Integer>> result = new ArrayList<>();

        for (int[] pair : friends) {
            map.computeIfAbsent(pair[0], k -> new HashSet<>()).add(pair[1]);
            map.computeIfAbsent(pair[1], k -> new HashSet<>()).add(pair[0]);
        }

        for (int friend1 : map.keySet()) {
            for (int friend2 : map.keySet()) {
                if (friend1 != friend2 && map.get(friend1).isEmpty() && map.get(friend2).isEmpty()) {
                    result.add(Arrays.asList(friend1, friend2));
                }
            }
        }

        return result;
    }
}

Try It Yourself

Loading code editor...

Related LeetCode Problems

Frequently Asked Questions

How to solve LeetCode 3058 (Friends With No Mutual Friends)?

This page provides optimized solutions for LeetCode problem 3058 (Friends With No Mutual Friends) in Java, C++, and Python, along with a detailed explanation and an interactive code editor to test your code.

What is the time complexity of LeetCode 3058 (Friends With No Mutual Friends)?

The time complexity for LeetCode 3058 (Friends With No Mutual Friends) varies by solution. Check the detailed explanation section for specific complexities in Java, C++, and Python implementations.

Can I run code for LeetCode 3058 on DevExCode?

Yes, DevExCode provides an interactive code editor where you can write, test, and run your code for LeetCode 3058 in Java, C++, or Python.

Back to LeetCode Solutions