In a particular social network friends are automatically allocated to users by the system and users cannot add friends of their choice on their own. There are currently N users on the social network, labeled from 2 to N+1.
For every ith user (where i ranges from 2 to N+1), the system allocated all the users labeled with multiples of i as the user's friends (if possible).
One day, all users of the social network come together for a meeting and form groups such that each person in a group is a direct friend or a friend of friend of every other person of that group.
Find the total number of groups.
Input Specifications:
Input1: N, denoting the number of users on the social network
Output Specification: your function should return the number of groups that can be formed on given conditions
Example 1:
Input1: 5 Output: 2
Explanation: Two groups will be formed
2,3,4,6
5
Example 2:
Input1: 10 Output: 3
Explanation: Three groups will be formed:
2,3,4,5,6,8,9,10
7
11
Please optimize my solution. My solution is working perfectly but doesn't look optimal.
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map.Entry;
import java.util.Set;
public class SocialNetwork {
public static void main(String[] args) {
InputStreamReader r = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(r);
int value = 0;
try {
value = Integer.parseInt(br.readLine());
} catch (IOException e) {
System.out.println(e.getMessage());
}
HashMap<Integer, List<Integer>> map = new HashMap<>();
for (int i = 2; i <= value + 1; i++) {
List<Integer> list = new ArrayList<>();
for (int j = 1; j * i <= value + 1; j++) {
int tempValue = j * i;
list.add(tempValue);
if (i != tempValue) {
List<Integer> addedList = map.get(tempValue);
if (addedList == null) {
addedList = new ArrayList<>();
}
if (!addedList.contains(i)) {
addedList.add(i);
map.put(tempValue, addedList);
}
}
}
List<Integer> currList = map.get(i);
if (currList != null)
currList.addAll(list);
else
currList = list;
map.put(i, currList);
}
// Iterate through all elements of map
Iterator<Entry<Integer, List<Integer>>> iterator = map.entrySet().iterator();
List<Integer> visitedKeys = new ArrayList<>();
List<Set<Integer>> listSet = new ArrayList<>();
while (iterator.hasNext()) {
Entry<Integer, List<Integer>> entry = iterator.next();
Integer key = entry.getKey();
List<Integer> keyValue = entry.getValue();
if (visitedKeys.contains(key)) {
continue;
}
Set<Integer> setItem = new HashSet<>();
updateSet(key, keyValue, visitedKeys, map, setItem);
listSet.add(setItem);
}
System.out.println("groups=" + listSet);
System.out.println("Number of groups=" + listSet.size());
}
private static void updateSet(Integer key, List<Integer> keyValue, List<Integer> visitedKeys,
HashMap<Integer, List<Integer>> map, Set<Integer> setItem) {
for (Integer item : keyValue) {
if (visitedKeys.contains(item)) {
continue;
}
if (!item.equals(key)) {
List<Integer> mapVal = map.get(item);
if (mapVal != null) {
updateSet(item, mapVal, visitedKeys, map, setItem);
}
}
visitedKeys.add(item);
setItem.add(item);
}
}
}