HiveBrain v1.2.0
Get Started
← Back to all entries
patterncsharpMinor

Hackerank - value of friendship

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
friendshiphackerankvalue

Problem

Problem statement

You're researching friendships between groups \$n\$ of new college students where each student is distinctly numbered from \$1\$ to \$n\$. At the beginning of the semester, no student knew any other student; instead, they met and formed individual friendships as the semester went on. The friendships between students are:

-
Bidirectional

If student \$a\$ is friends with student \$b\$, then student \$b\$ is also friends with student \$a\$.

-
Transitive

If student \$a\$ is friends with student \$b\$ and student \$b\$ is friends with student \$c\$ , then student \$a\$ is friends with student \$c\$. In other words, two students are considered to be friends even if they are only indirectly linked through a network of mutual (i.e., directly connected) friends.
The purpose of your research is to find the maximum total value of a group's friendships, denoted by \$total\$. Each time a direct friendship forms between two students, you sum the number of friends that each of the \$n\$ students has and add the sum to \$total\$.

You are given \$q\$ queries, where each query is in the form of an unordered list of \$m\$ distinct direct friendships between \$n\$ students. For each query, find the maximum value of \$total\$ among all possible orderings of formed friendships and print it on a new line.

Input Format

The first line contains an integer, \$q\$, denoting the number of queries. The subsequent lines describe each query in the following format:

The first line contains two space-separated integers describing the respective values of \$n\$ (the number of students) and \$m\$ (the number of distinct direct friendships).
Each of the \$m\$ subsequent lines contains two space-separated integers describing the respective values of \$x\$ and \$y\$ (where \$x<>y\$) describing a friendship between student \$x\$ and student \$y\$.

Constraints

1. 1

For each query, print the maximum value of \$total\$ on a new line.

Sample Input 0

1

5 4

1 2`

Solution


  • code review:



  • Everything is in one function, should break down a few of pieces




Good idea. Why don't you actually implement it? It makes sense. The same can be done to MaximizeValues method. Right now it does at least three distinct things:

  • it converts string values to numbers for every test case



  • it builds graph for every test case



  • it solves the problem for every test case



Those should be three different methods.

You can further simplify MaximizeValues method if you change its signature to:

public static long MaximizeValues(string[] datas, string[] allFriendships)


and call it once for every query instead.

Your code does not follow C# naming conventions. Public fields and properties should use PascalCase. Also the names themselves could be better. What is id_1? How is it different from id_2? Hard to tell.

Your algorithm is pretty hard to follow, at least for me (I am no expert in graph theory). If you add some local variables with descriptive names - that might help. For example, instead of writing:

result += (i + 1) * (long)i + sum;


you could write:

var explainWhatThatIs = (i + 1) * (long)i + sum;
result += explainWhatThatIs;

Code Snippets

public static long MaximizeValues(string[] datas, string[] allFriendships)
result += (i + 1) * (long)i + sum;
var explainWhatThatIs = (i + 1) * (long)i + sum;
result += explainWhatThatIs;

Context

StackExchange Code Review Q#152844, answer score: 4

Revisions (0)

No revisions yet.