Number of handshakes such that a person shakes hands only once


Suppose you are in a social gathering. Can you calculate how many handshakes you can do if you were to shakes only once? This question might sound intriguing to you. This can be solved mathematically by using permutations and combinations. However, the mathematical operation might be time-consuming.

In this article, we will discuss how to solve such problem using C++. We will explore different approaches which ranges from mathematical formulas, to recursion, and other combinatorial techniques.

Input Output Scenarios

Suppose you have N number of people in a gathering. You want to calculate the number of handshakes possible such that a person shakes hands only once.

Input: N = 16
Output: 120
Input: N = 11
Output: 55

Using the Formula for Handshakes

The formula for finding the number of handshakes in a gathering of N people is −

No. of handshakes = N *(N-1) /2

Each of the N people will shake the hands with (N-1) individuals (excluding the person itself) and the handshakes between two individuals is not counted twice.

For Example, if the number of individuals is 14. Then, number of handshakes are

Handshakes = 14 * (14 - 1)/ 2
           = 14 * 13 / 2
           = 182/2
           = 91

Example

In the following example, we are using the formula which is used to calculate the number of handshakes. Here, we simply use the mathematical operators by taking the number of individuals at the gathering as the input.

#include <iostream>
using namespace std;

int count(int N) {
   // Formula: N * (N-1) / 2
   return (N * (N - 1)) / 2;
}

int main() {
   int totalIndividuals= 10;
   int numHandshakes = count(totalIndividuals);
   std::cout << "Number of handshakes: " << numHandshakes << std::endl;
   return 0;
}

Output

Number of handshakes: 45

Using For Loop

Here, we calculate the number of handshakes by iterating from 1 to ‘N-1’ and then adding all the values.

Example

#include <iostream>
using namespace std;

int count(int N) {
   int numHandshakes = 0;

   for (int x = 1; x < N; x++) {
      numHandshakes += x;
   }

   return numHandshakes;
}

int main() {
   int totalIndividuals = 10;
   int numHandshakes = count(totalIndividuals);
   std::cout << "Number of handshakes: " << numHandshakes << std::endl;
   return 0;
}

Output

Number of handshakes: 45

Using Recursion

We can use recursion for calculating the number of handshakes. By doing so, we divide the problem into smaller problems by considering one person at a time.

Example

#include <iostream>
using namespace std;

int count(int N) {
   if (N <= 1)
      return 0;
   return (N - 1) + count(N - 1);
}

int main() {
   int totalIndividuals = 20;
   int numHandshakes = count(totalIndividuals);
   std::cout << "Number of handshakes: " << numHandshakes << std::endl;
   return 0;
}

Output

Number of handshakes: 190

Using While Loop

Here, we have used a while loop which has a decrementing counter for calculating the number of handshakes. The loop starts with the total number of individuals and then decreases the counter one by one after each iteration.

Example

#include <iostream>
using namespace std;

int count(int N) {
   int numHandshakes = 0;

   while (N > 1) {
      numHandshakes += N - 1;
      N--;
   }

   return numHandshakes;
}

int main() {
   int totalIndividuals = 16;
   int numHandshakes = count(totalIndividuals);
   std::cout << "Number of handshakes: " << numHandshakes << std::endl;
   return 0;
}

Output

Number of handshakes: 120

Using Dynamic Programming

Here, we have used dynamic programming for the calculation.

  • Initialize a ‘dp’ vector to store the number of handshakes.

  • Iterate from 1 to N. In each iteration, it declares the number of handshakes as the sum of previous handshakes and the present number of individual minus 1.

Example

#include <iostream>
#include <vector>
using namespace std;

int count(int N) {
   std::vector<int> dp(N + 1);
   dp[0] = 0;

   for (int x = 1; x <= N; x++) {
      dp[x] = dp[x - 1] + (x - 1);
   }

   return dp[N];
}

int main() {
   int totalIndividuals = 21;
   int numHandshakes = count(totalIndividuals);
   std::cout << "Number of handshakes: " << numHandshakes << std::endl;
   return 0;
}

Output

Number of handshakes: 210

Note  This approach helps to avoid redundant calculations. Here, we store the previously computed values in the ‘dp’ vector, which you can access any time and reuse it. This makes the algorithm efficient. Also, it reduces the overall computing time.

Conclusion

We have discussed various approaches by which we can calculate the number of handshakes such that a person shakes hands only once. These methods include using the mathematical operators for the formula, using for loop, recursion, while loop and dynamic programming. Each of the approaches have their own advantages. The dynamic programming is a more systematic and organized approach to solve the problem. You can use any of the approaches according to the specific requirements.

Updated on: 12-Jul-2023

90 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements