I've got the same results as expected but verification didn't pass.
Could you help me figure out why?
package com.codegym.task.task36.task3611;
import java.util.HashSet;
import java.util.Set;
/*
How many potential friends does a person have?
*/
public class Solution {
private boolean[][] humanRelationships;
public static void main(String[] args) {
Solution solution = new Solution();
solution.humanRelationships = generateRelationships();
Set<Integer> allFriendsAndPotentialFriends =
solution.getAllFriendsAndPotentialFriends(4, 2);
System.out.println(allFriendsAndPotentialFriends); // Expected: [0, 1, 2, 3, 5, 7]
Set<Integer> potentialFriends =
solution.removeFriendsFromSet(allFriendsAndPotentialFriends, 4);
System.out.println(potentialFriends); // Expected: [2, 5, 7]
}
public Set<Integer> getAllFriendsAndPotentialFriends(int index, int deep) {
//write your code here
Set<Integer> set = new HashSet<>();
if (deep == 0) {
return set;
}
for(int i = 0; i < humanRelationships[index].length - 1; i++) {
if (humanRelationships[index][i]) {
set.add(i);
}
}
deep--;
while (deep > 0) {
Set<Integer> result = new HashSet<>();
for (Integer ind : set) {
for (int i = ind + 1; i < humanRelationships.length; i++) {
if (humanRelationships[i][ind]){
result.add(i);
}
}
}
set.addAll(result);
deep--;
}
set.remove(index);
return set;
}
// Remove from the set the people with whom you already have a relationship
public Set<Integer> removeFriendsFromSet(Set<Integer> set, int index) {
for (int i = 0; i < humanRelationships.length; i++) {
if ((i < index) && (index < humanRelationships.length) && humanRelationships[index][i]) {
set.remove(i);
} else if ((i > index) && humanRelationships[i][index]) {
set.remove(i);
}
}
return set;
}
// Return test data
private static boolean[][] generateRelationships() {
return new boolean[][]{
{true}, //0
{true, true}, //1
{false, true, true}, //2
{false, false, false, true}, //3
{true, true, false, true, true}, //4
{true, false, true, false, false, true}, //5
{false, false, false, false, false, true, true}, //6
{false, false, false, true, false, false, false, true} //7
};
}
}