## Algorithm

Problem Name: Mathematics - Handshake

In this HackerRank in Mathematics - Handshake solutions,

At the annual meeting of Board of Directors of Acme Inc. If everyone attending shakes hands exactly one time with every other attendee, how many handshakes are there?

Example

n = 3

There are 3 attendees, p1,p2 and p3.p1 shakes hands with p2 and p3 and p2 shakes hands with p3 . Now they have all shaken hands after 3 handshakes.

Function Description

Complete the handshakes function in the editor below.

handshakes has the following parameter:

• int n: the number of attendees

Returns

• int: the number of handshakes

Input Format
The first line contains the number of test cases t.

Each of the following t lines contains an integer n.

Constraints

1  <= t <= 1000

0 <= n <= 106

Sample Input

2
1
2


Sample Output

0
1


Explanation

Case 1 : The lonely board member shakes no hands, hence 0.
Case 2 : There are 2 board members, so 1 handshake takes place.

## Code Examples

### #1 Code Example with C Programming

Code - C Programming


#include <assert.h>
#include <ctype.h>
#include <imits.h>
#include <math.h>>
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

char* ltrim(char*);
char* rtrim(char*);

int parse_int(char*);

/*
* Complete the 'handshake' function below.
*
* The function is expected to return an INTEGER.
* The function accepts INTEGER n as parameter.
*/

int handshake(int n) {

n = ((n*(n+1))/2)-n ;
return  n ;
}

int main()
{
FILE* fptr = fopen(getenv("OUTPUT_PATH"), "w");

int t = parse_int(ltrim(rtrim(readline())));

for (int t_itr = 0; t_itr  <  t; t_itr++) {
int n = parse_int(ltrim(rtrim(readline())));

int result = handshake(n);

fprintf(fptr, "%d\n", result);
}

fclose(fptr);

return 0;
}

size_t alloc_length = 1024;
size_t data_length = 0;

char* data = malloc(alloc_length);

while (true) {
char* cursor = data + data_length;
char* line = fgets(cursor, alloc_length - data_length, stdin);

if (!line) {
break;
}

data_length += strlen(cursor);

if (data_length  <  alloc_length - 1 || data[data_length - 1] == '\n') {
break;
}

alloc_length <<= 1;

data = realloc(data, alloc_length);

if (!data) {
data = '\0';

break;
}
}

if (data[data_length - 1] == '\n') {
data[data_length - 1] = '\0';

data = realloc(data, data_length);

if (!data) {
data = '\0';
}
} else {
data = realloc(data, data_length + 1);

if (!data) {
data = '\0';
} else {
data[data_length] = '\0';
}
}

return data;
}

char* ltrim(char* str) {
if (!str) {
return '\0';
}

if (!*str) {
return str;
}

while (*str != '\0' && isspace(*str)) {
str++;
}

return str;
}

char* rtrim(char* str) {
if (!str) {
return '\0';
}

if (!*str) {
return str;
}

char* end = str + strlen(str) - 1;

while (end >= str && isspace(*end)) {
end--;
}

*(end + 1) = '\0';

return str;
}

int parse_int(char* str) {
char* endptr;
int value = strtol(str, &endptr, 10);

if (endptr == str || *endptr != '\0') {
exit(EXIT_FAILURE);
}

return value;
}


Copy The Code &

### #2 Code Example with C++ Programming

Code - C++ Programming


#include<iostream>

using namespace std;

int main(){
int t,n;
cin >> t;
while(t--){
cin >> n;
cout << (n*(n+1)) / 2 - n << endl;
}
}

Copy The Code &

### #3 Code Example with C# Programming

Code - C# Programming


using System.CodeDom.Compiler;
using System.Collections.Generic;
using System.Collections;
using System.ComponentModel;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.Serialization;
using System.Text.RegularExpressions;
using System.Text;
using System;

class Result
{

/*
* Complete the 'handshake' function below.
*
* The function is expected to return an INTEGER.
* The function accepts INTEGER n as parameter.
*/

public static int handshake(int n)
{
int result=0;

for(int i = 1; i  <  n ; i++)
result +=(n-i);

return result;
}

}

class Solution
{
public static void Main(string[] args)
{
TextWriter textWriter = new StreamWriter(@System.Environment.GetEnvironmentVariable("OUTPUT_PATH"), true);

int t = Convert.ToInt32(Console.ReadLine().Trim());

for (int tItr = 0; tItr  <  t; tItr++)
{
int n = Convert.ToInt32(Console.ReadLine().Trim());

int result = Result.handshake(n);

textWriter.WriteLine(result);
}

textWriter.Flush();
textWriter.Close();
}
}


Copy The Code &

### #4 Code Example with Java Programming

Code - Java Programming


import java.io.*;
import java.math.*;
import java.security.*;
import java.text.*;
import java.util.*;
import java.util.concurrent.*;
import java.util.function.*;
import java.util.regex.*;
import java.util.stream.*;
import static java.util.stream.Collectors.joining;
import static java.util.stream.Collectors.toList;

class Result {

/*
* Complete the 'handshake' function below.
*
* The function is expected to return an INTEGER.
* The function accepts INTEGER n as parameter.
*/

public static int handshake(int n)
{
int counter = 0;
for(int i = 1 ; i  <  n ; i++){
counter = counter + i;
}
return counter;
}

}

public class Solution {
public static void main(String[] args) throws IOException {
BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(System.getenv("OUTPUT_PATH")));

IntStream.range(0, t).forEach(tItr -> {
try {

int result = Result.handshake(n);

bufferedWriter.write(String.valueOf(result));
bufferedWriter.newLine();
} catch (IOException ex) {
throw new RuntimeException(ex);
}
});

bufferedWriter.close();
}
}

Copy The Code &

### #5 Code Example with Javascript Programming

Code - Javascript Programming


'use strict';

const fs = require('fs');

process.stdin.resume();
process.stdin.setEncoding('utf-8');

let inputString = '';
let currentLine = 0;

process.stdin.on('data', function(inputStdin) {
inputString += inputStdin;
});

process.stdin.on('end', function() {
inputString = inputString.split('\n');

main();
});

return inputString[currentLine++];
}

/*
* Complete the 'handshake' function below.
*
* The function is expected to return an INTEGER.
* The function accepts INTEGER n as parameter.
*/

function handshake(n) {
if(n <= 1> return 0;
if(n >=2 ) return n-1 + handshake(n - 1);
}

function main() {
const ws = fs.createWriteStream(process.env.OUTPUT_PATH);

const t = parseInt(readLine().trim(), 10);

for (let tItr = 0; tItr < t; tItr++) {
const n = parseInt(readLine().trim(), 10);

const result = handshake(n);

ws.write(result + '\n');
}

ws.end(>;
}

Copy The Code &

### #6 Code Example with Python Programming

Code - Python Programming


#!/bin/python3

import math
import os
import random
import re
import sys

#
# Complete the 'handshake' function below.
#
# The function is expected to return an INTEGER.
# The function accepts INTEGER n as parameter.
#

def handshake(n):
return sum(range(n))

if __name__ == '__main__':
fptr = open(os.environ['OUTPUT_PATH'], 'w')

t = int(input().strip())

for t_itr in range(t):
n = int(input().strip())

result = handshake(n)

fptr.write(str(result) + '\n')

fptr.close()

Copy The Code &