-
Notifications
You must be signed in to change notification settings - Fork 0
/
noOfFriendCircles.java
31 lines (31 loc) · 1.01 KB
/
noOfFriendCircles.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
public class Main
{
public static void findFriends(char[][] friends, boolean[] visited, int frnd){
for(int i=0;i<friends.length;i++){
if(!visited[i] && friends[frnd][i]=='Y'){
visited[i]=true;
findFriends(friends,visited,i);
}
}
}
public static int getFriendCircles(char[][] friends){
if(friends == null || friends.length < 1)
return 0;
int noOfCircles = 0;
boolean visited[] = new boolean[friends.length];
for(int i=0;i<friends.length;i++)
visited[i]=false;
for(int i=0;i<friends.length;i++){
if(!visited[i]){
noOfCircles++;
visited[i]=true;
findFriends(friends,visited,i);
}
}
return noOfCircles;
}
public static void main(String[] args) {
char[][] friends={{'Y','Y','N','N'},{'Y','Y','Y','N'},{'N','Y','Y','N'},{'N','N','N','Y'}};
System.out.println(getFriendCircles(friends));
}
}