Saturday, 13 January 2018

Handshake Hacker Rank Problem Solution.

Problem:

At the annual meeting of Board of Directors of Acme Inc, every one starts shaking hands with everyone else in the room. Given the fact that any two persons shake hand exactly once, Can you tell the total count of handshakes?
Input Format 
The first line contains the number of test cases T, T lines follow. 
Each line then contains an integer N, the total number of Board of Directors of Acme.
Output Format
Print the number of handshakes for each test-case in a new line.
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, 1 handshake takes place.

Solution:

#include <iostream>
using namespace std;
/*
    * Prosen Ghosh
    * American International University - Bangladesh (AIUB)
    *
*/
int main(){
    int T;
    cin >> T;
    for(int a0 = 0; a0 < T; a0++){
        long int N;
        cin >> N;
        cout << ((N-1)*N)/2 << endl;
    }
    return 0;
}

Maximum Draws Hacker Rank Problem Solution.

Problem:

Jim is off to a party and is searching for a matching pair of socks. His drawer is filled with socks, each pair of a different color. In its worst case scenario, how many socks (x) should Jim remove from his drawer until he finds a matching pair?
Input Format 
The first line contains the number of test cases T. 
Next T lines contains an integer N which indicates the total pairs of socks present in the drawer.
Output Format 
Print the number of Draws (x) Jim makes in the worst case scenario.
Constraints 
 
Sample Input
2
1
2
Sample Output
2
3
Explanation 
Case 1 : A pair of socks are present, hence exactly 2 draws for the socks to match. 
Case 2 : 2 pair of socks are present in the drawer. The first and the second draw might result in 2 socks of different color. The 3rd sock picked will definitely match one of previously picked socks. Hence, 3.
Solution:


#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
/*
    * Prosen Ghosh
    * American International University - Bangladesh (AIUB)
    *
*/
int main() {
    int n,a;
    cin >> n;
    while(n --){
        cin >> a;
        cout << a+1 << endl;
    }
    return 0;
}