Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- static int valueOfFriendsship(int n, int[][] friendships) {
- int total = 0;
- int [][] friends = new int [n][n];
- int numberOfFriendships = friendships[0][1];
- for(int i=1;i<friendships.length;i++)
- {
- int x = friendships[i][0];
- int y = friendships[i][1];
- //adding 1 in the friends array corresponding to each friend that person make
- friends[x][y] = 1 ;
- friends[y][x] = 1 ;
- //looping on friends list to update it
- for (int z = 0;z<friends.length;z++)
- {
- if(friends[z][x]>0)
- {
- friends[x][z] = 1;
- friends[y][z]=1;
- }
- if(friends[z][y]>0)
- {
- friends[y][z] = 1;
- friends[x][z] = 1;
- }
- }
- //updating total
- for(int k = 0;k<friends.length;k++)
- {
- for(int l = 0;l<friends[k].length;l++)
- {
- total += friends[k][l];
- }
- }
- }
- return total;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement