Advertisement
Guest User

Untitled

a guest
Mar 22nd, 2019
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.18 KB | None | 0 0
  1.     static int valueOfFriendsship(int n, int[][] friendships) {
  2.        int total = 0;
  3.        int [][] friends = new int [n][n];
  4.        int numberOfFriendships = friendships[0][1];
  5.        for(int i=1;i<friendships.length;i++)
  6.        {
  7.            int x = friendships[i][0];
  8.            int y = friendships[i][1];
  9.            //adding 1 in the friends array corresponding to each friend that person make
  10.             friends[x][y] = 1 ;
  11.             friends[y][x] = 1 ;
  12.             //looping on friends list to update it
  13.             for (int z = 0;z<friends.length;z++)
  14.             {
  15.                 if(friends[z][x]>0)
  16.                 {  
  17.                     friends[x][z] = 1;
  18.                     friends[y][z]=1;
  19.                 }
  20.                 if(friends[z][y]>0)
  21.                 {
  22.                     friends[y][z] = 1;
  23.                     friends[x][z] = 1;
  24.                 }
  25.  
  26.             }
  27.             //updating total
  28.             for(int k = 0;k<friends.length;k++)
  29.             {
  30.               for(int l = 0;l<friends[k].length;l++)
  31.               {
  32.                 total += friends[k][l];
  33.               }
  34.             }
  35.  
  36.         }
  37.  
  38.         return total;
  39.  
  40.  
  41.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement