Number of handshakes such that a person shakes hands only once
Last Updated :
28 Dec, 2022
There is N number of people at a party. Find the total number of handshakes such that a person can handshake only once.
Examples:
Input : 5
Output : 10
Input : 9
Output : 36
We can see a recursive nature in the problem.
// n-th person has (n-1) choices and after
// n-th person chooses a person, problem
// recurs for n-1.
handshake(n) = (n-1) + handshake(n-1)
// Base case
handshake(0) = 0
Below is the implementation of the above recursive formula.
C++
#include <bits/stdc++.h>
using namespace std;
int handshake( int n)
{
if (n == 0)
return 0;
else
return (n - 1) + handshake(n - 1);
}
int main()
{
int n = 9;
cout << " " << handshake(n);
return 0;
}
|
C
#include <stdio.h>
int handshake( int n)
{
if (n == 0)
return 0;
else
return (n - 1) + handshake(n - 1);
}
int main()
{
int n = 9;
printf ( "%d" , handshake(n));
return 0;
}
|
Java
import java.io.*;
class GFG {
static int handshake( int n)
{
if (n == 0 )
return 0 ;
else
return (n - 1 ) + handshake(n - 1 );
}
public static void main(String[] args)
{
int n = 9 ;
System.out.print(handshake(n));
}
}
|
Python3
def handshake(n):
if (n = = 0 ):
return 0
else :
return (n - 1 ) + handshake(n - 1 )
n = 9
print (handshake(n))
|
C#
using System;
class GFG
{
static int handshake( int n)
{
if (n == 0)
return 0;
else
return (n - 1) + handshake(n - 1);
}
public static void Main (String []args)
{
int n = 9;
Console.WriteLine(handshake(n));
}
}
|
PHP
<?php
function handshake( $n )
{
if ( $n == 0)
return 0;
else
return ( $n - 1) + handshake( $n - 1);
}
$n = 9;
echo (handshake( $n ));
?>
|
Javascript
<script>
function handshake(n) {
if (n === 0)
return 0;
else
return n - 1 + handshake(n - 1);
}
var n = 9;
document.write(handshake(n));
</script>
|
Time Complexity: O(n)
Auxiliary Space: O(1), As the function is tail recursive the extra stack space will not be used.
We can come up with a direct formula by expanding the recursion.
handshake(n) = (n-1) + handshake(n-1)
= (n-1) + (n-2) + handshake(n-2)
= (n-1) + (n-2) + .... 1 + 0
= n * (n - 1)/2
C++
#include <bits/stdc++.h>
using namespace std;
int handshake( int n) { return n * (n - 1) / 2; }
int main()
{
int n = 9;
cout << handshake(n) << endl;
return 0;
}
|
C
#include <stdio.h>
int handshake( int n) { return n * (n - 1) / 2; }
int main()
{
int n = 9;
printf ( "%d" , handshake(n));
return 0;
}
|
Java
class GFG {
static int handshake( int n) { return n * (n - 1 ) / 2 ; }
public static void main(String args[])
{
int n = 9 ;
System.out.println(handshake(n));
}
}
|
Python3
def handshake(n):
return int (n * (n - 1 ) / 2 )
n = 9
print (handshake(n))
|
C#
using System;
class GFG
{
static int handshake( int n)
{
return n * (n - 1) / 2;
}
static public void Main ()
{
int n = 9;
Console.WriteLine(handshake(n));
}
}
|
PHP
<?php
function handshake( $n )
{
return $n * ( $n - 1) / 2;
}
$n = 9;
echo (handshake( $n ));
?>
|
Javascript
<script>
function handshake(n)
{
return n * parseInt((n - 1) / 2, 10);
}
let n = 9;
document.write(handshake(n));
</script>
|
Time Complexity: O(1)
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...