We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
The strategy that I used was that I visualized the problem as a square grid in which both the columns and rows represent the different persons. It is assumed that a person does not shake hands with him or herself so that means the diagonal is empty. If person 1 shakes hands with person 2, I can place a mark on the first row, second column. If person 2 shakes hands with person 3 I place a mark on the second row, third colummn and so on. In the end you will see that you have marked all elements of the grid above the diagonal.
Unfortunately, I think that at this point you do need some background in mathematics to quickly see that there is a formula that will give you the number of these elements. If not, you can always try to get the solution for small numbers of N and hopefully discover a relationship before N gets too big :)
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Handshake
You are viewing a single comment's thread. Return to all comments →
The strategy that I used was that I visualized the problem as a square grid in which both the columns and rows represent the different persons. It is assumed that a person does not shake hands with him or herself so that means the diagonal is empty. If person 1 shakes hands with person 2, I can place a mark on the first row, second column. If person 2 shakes hands with person 3 I place a mark on the second row, third colummn and so on. In the end you will see that you have marked all elements of the grid above the diagonal.
Unfortunately, I think that at this point you do need some background in mathematics to quickly see that there is a formula that will give you the number of these elements. If not, you can always try to get the solution for small numbers of N and hopefully discover a relationship before N gets too big :)