Extra long factorials

Problem Statement

You are given an integer N. Print the factorial of this number.

N!=N×(N1)×(N2)××3×2×1

Note: Factorials of N>20 can’t be stored even in a 64bit long long variable. Big integers must be used for such calculations. Languages like Java, Python, Ruby etc. can handle big integers but we need to write additional code in C/C++ to handle such large values.

We recommend solving this challenge using BigIntegers.

Input Format
Input consists of a single integer N.

Constraints
1N100

Output Format
Output the factorial of N.

Sample Input

25

Sample Output

15511210043330985984000000

Solution

  
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;

public class Solution {

    public static void main(String[] args)  throws Exception{
        /* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */
        //BufferedReader br = new BufferedReader( new InputStreamReader(System.in));
        //int n = Integer.parseInt(br.read());
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        BigInteger ans = BigInteger.ONE;
        for (int i=1;i<=n;i++)
            {
            ans = ans.multiply(new BigInteger(String.valueOf(i))); 
        }
        System.out.println(ans);
    }
}

Library Fine

Problem Statement

The Head Librarian at a library wants you to make a program that calculates the fine for returning the book after the return date. You are given the actual and the expected return dates. Calculate the fine as follows:

  1. If the book is returned on or before the expected return date, no fine will be charged, in other words fine is 0.
  2. If the book is returned in the same month as the expected return date, Fine = 15 Hackos × Number of late days
  3. If the book is not returned in the same month but in the same year as the expected return date, Fine = 500 Hackos × Number of late months
  4. If the book is not returned in the same year, the fine is fixed at 10000 Hackos.

Input Format

You are given the actual and the expected return dates in D M Y format respectively. There are two lines of input. The first line contains the D M Y values for the actual return date and the next line contains the D M Y values for the expected return date.

Constraints
1D31
1M12
1Y3000

Output Format

Output a single value equal to the fine.

Sample Input

9 6 2015
6 6 2015

Sample Output

45

Explanation

Since the actual date is 3 days later than expected, fine is calculated as 15×3=45 Hackos.

Solution

  
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;


int main() {
    /* Enter your code here. Read input from STDIN. Print output to STDOUT */
    int d1,d2,m1,m2,y1,y2;
    cin>>d2>>m2>>y2;
    cin>>d1>>m1>>y1;
    if(y2>y1)
    {
        cout<<10000;
        exit;
    }
    else if(y1==y2 && m2>m1)
        {
        cout<<(m2-m1)*500;
    }
    else if (y1==y2 && m2==m1 && d2>d1)
        cout<<(d2-d1)*15;
    else cout<<0;
    return 0;
}


Time Conversion

Problem Statement

You are given time in AM/PM format. Convert this into a 24 hour format.

Note Midnight is 12:00:00AM or 00:00:00 and 12 Noon is 12:00:00PM.

Input Format

Input consists of time in the AM/PM format i.e. hh:mm:ssAM or hh:mm:ssPM
where
01hh12
00mm59
00ss59

Output Format

You need to print the time in 24 hour format i.e. hh:mm:ss
where
00hh23
00mm59
00ss59

Sample Input

07:05:45PM

Sample Output

19:05:45

Solution

  
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;


int main() {
    /* Enter your code here. Read input from STDIN. Print output to STDOUT */
    int h,m,s;
    char c;
    scanf("%d:%d:%d%c",&h,&m,&s,&c);
    if(c=='P')
        if(h==12)
            cout<<12;
        else cout<<h+12;
    else if(h<10)
            cout<<"0"<<h;
    else if(h==12)
        cout<<"00";
    else cout<<h;    
    if(m<10)
        cout<<":0"<<m;
    else cout<<":"<<m;
    if(s<10)
        cout<<":0"<<s;
    else cout<<":"<<s;    
    
    return 0;
}


Staircase

Problem Statement

Your teacher has given you the task to draw the structure of a staircase. Being an expert programmer, you decided to make a program for the same. You are given the height of the staircase. You need to print a staircase as shown in the example.

Input Format

You are given an integer N depicting the height of the staircase.

Constraints
1N100

Output Format

Draw a staircase of height N in the format given below.

For example:

     #
    ##
   ###
  ####
 #####
######

Staircase of height 6, note that last line has 0 spaces before it.

Sample Input

6

Sample Output

     #
    ##
   ###
  ####
 #####
######

Solution

  
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;


int main() {
    /* Enter your code here. Read input from STDIN. Print output to STDOUT */
    int n,i,j;
    cin>>n;
    for(i=1;i<=n;i++)
    {    
        for(j=i;j0;j--)
            cout<<"#"; 
        cout<<endl;        
            
    }        
    return 0;
}


Plus Minus

Problem Statement

You’re given an array containing integer values. You need to print the fraction of count of positive numbers, negative numbers and zeroes to the total numbers. Print the value of the fractions correct to 3 decimal places.

Input Format

First line contains N, which is the size of the array.
Next line contains N integers A1,A2,A3,,AN, separated by space.

Constraints
1N100
100Ai100

Output Format

Output three values on different lines equal to the fraction of count of positive numbers, negative numbers and zeroes to the total numbers respectively correct to 3 decimal places.

Sample Input

6
-4 3 -9 0 4 1          

Sample Output

0.500
0.333
0.167

Explanation

There are 3 positive numbers, 2 negative numbers and 1 zero in the array.
Fraction of the positive numbers, negative numbers and zeroes are 36=0.500, 26=0.333 and 16=0.167 respectively.

Note This challenge introduces precision problems. You can even print output to 4 decimal places and above but only the difference at 3rd decimal digit is noted. That is the reason you’ll notice testcases have much higher precision (more decimal places) than required.
Answers with absolute error upto 104 will be accepted.

Solution

  
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;


int main() {
    /* Enter your code here. Read input from STDIN. Print output to STDOUT */
    int a;
    float n,c1,c2,c3;
    cin>>n;
    for(int i=0;i>a;
       if(a>0)    
            ++c1;
       else if (a==0)
           ++c2;
       else ++c3;    
    }
    cout<<c1/n<<endl;
    cout<<c3/n<<endl;
    cout<<c2/n<<endl;
    return 0;
}


Diagonal Difference

Problem Statement

You are given a square matrix of size N×N. Calculate the absolute difference of the sums across the two main diagonals.

Input Format

The first line contains a single integer N. The next N lines contain N integers (each) describing the matrix.

Constraints
1N100
100A[i]100

Output Format

Output a single integer equal to the absolute difference in the sums across the diagonals.

Sample Input

3
11 2 4
4 5 6
10 8 -12

Sample Output

15

Explanation

The first diagonal of the matrix is:

11
    5
        -12

Sum across the first diagonal = 11+5-12= 4

The second diagonal of the matrix is:

        4
    5
10

Sum across the second diagonal = 4+5+10 = 19
Difference: |4-19| =15

Solution

  
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;


int main() {
    /* Enter your code here. Read input from STDIN. Print output to STDOUT */
    int a[100][100],sum1=0,sum2=0,n,i,j;
    cin>>n;
    for(i=0;i<n;i++)
        for(j=0;j<n;j++)
            cin>>a[i][j];
    for(i=0;i<n;i++)
    {
        sum1+=a[i][i];
        sum2+=a[i][n-i-1];
    }
    cout<<abs(sum1-sum2);    
    return 0;
}


A Very Big Sum

Problem Statement

You are given an array of integers of size N. You need to print the sum of the elements of the array.

Note: The range of the 32-bit integer is 231 to 2311 or [2147483648,2147483647]. When we add several integer values, the resulting sum might exceed this range. You might need to use long long int in C/C++ or long data type in Java to store such sums.

Input Format

The first line of the input consists of an integer N. The next line contains N space-separated integers describing the array.

Constraints
1N10
0A[i]1010

Output Format

Output a single value equal to the sum of the elements of the array.

Sample Input

5
1000000001 1000000002 1000000003 1000000004 1000000005

Sample Output

5000000015

Solution

  
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;


int main() {
    /* Enter your code here. Read input from STDIN. Print output to STDOUT */
    long a[10],sum=0;
    int t;
    cin>>t;
    while(t--){
        cin>>a[t];
        sum+=a[t];
    }
    cout<<sum;
    return 0;
}


Simple Array Sum

Problem Statement

You are given an array of integers of size N. You need to print the sum of the elements of the array.

Input Format
The first line of the input consists of an integer N. The next line contains N space-separated integers describing the array.

Constraints
1N1000
0A[i]1000

Output Format
Output a single value equal to the sum of the elements of the array.

Sample Input

6
1 2 3 4 10 11

Sample Output

31

Explanation
1+2+3+4+10+11=31

Solution

  
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
#define max 1000
using namespace std;


int main() {
    /* Enter your code here. Read input from STDIN. Print output to STDOUT */   
    int n,temp,sum;
    cin>>n;
    while(n--){
        cin>>temp;
        sum+=temp;
    }
        cout<<sum;
    return 0;
}


Solve me second

Problem Statement

You learnt about STDIN and STDOUT in Solve me first.

This is the second challenge in the introduction series. The purpose of this challenge is to give you a working I/O template in your preferred language. It includes scanning two space-separated integers from STDIN in a loop over T lines, calling a function, returning a value, and printing it to STDOUT.

A pseudo code looks like the following:

read T
loop from 1 to T
    read A and B
    compute the sum
    print value in a newline
end loop

The task is to scan two numbers from STDIN, and print the sum A+B on STDOUT. The code has already been provided for most of the popular languages. This is primarily for you to read and inspect how the IO is handled.

Note: The code has been saved in a template, which you can submit if you want. Or, you may try rewriting it and building it up from scratch.

Input Format
(This section specifies the Input Format.)
The first line contains T (number of test cases) followed by T lines
Each line contains A and B, separated by a space.

As you can see that we have provided in advance the number of lines, we discourage the use of scanning till EOF as not every language has an easy way to handle that. In fact, every HackerRank challenge is designed in such a way that multitests begin with a T line to indicate the number of lines.

Output Format
(This section specifies the Output Format.)
An integer that denotes Sum (A+B) printed on new line for every testcase.

Constraints
(This section tells what input you can expect. You can freely assume that the input will remain within the boundaries specified.)
1T,A,B1000

Sample Input

2
2 3
3 7

Sample Output

5
10

The above sample should be taken seriously. 2 in the first line describes how many lines will follow, and your test cases are 2, 3 and 3, 7 in two separate lines. Your output should be 5 and 10 printed on two separate lines. If you print extra lines or “The answer is: 5”, any such extra characters in output will result in a Wrong Answer, as the judging is done using diff checker.

Solution

  
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;

int solveMeSecond(int a, int b) {
  return a+b;
}

int main() {
  int num1, num2;
  int sum;
  int t;
  cin >> t;

  for (int i=0; i> num1 >> num2;
    sum = solveMeSecond(num1,num2);
    cout << sum << endl;
  }

  return 0;
}


Solve me first

Problem Statement

This is an introductory challenge. The purpose of this challenge is to give you a working I/O template in your preferred language. It includes scanning two integers from STDIN, calling a function, returning a value, and printing it to STDOUT.

The task is to scan two numbers from STDIN, and print the sum A+B on STDOUT. The code has already been provided for most of the popular languages. This is primarily for you to read and inspect how the IO is handled.

Note: The code has been saved in a template, which you can submit if you want. Or, you may try rewriting it and building it up from scratch.

Input Format
(This section specifies the Input Format.)
Given A and B on two different lines.

Output Format
(This section specifies the Output Format.)
An integer that denotes Sum (A+B)

Constraints
(This section tells what input you can expect. You can freely assume that the input will remain within the boundaries specified. As an example here given below, A and B will never be below 1 or above 1000.)
1A,B1000

Sample Input

2
3

Sample Output

5

The above sample should be taken seriously. The input will be 2 and 3 in two separate lines, and the output should be just one number, 5. You should not print any whitespace at the beginning of output (e.g. ” 5″ or “\n5”), unless specifically asked for. Also, printing any extra non-whitespace characters such as “The answer is: 5” will result in a Wrong Answer, as the judging is done using diff checker.

Solution

  
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;

public class Solution {


    static int solveMeFirst(int a, int b) {
        return a+b;
   }

   
 public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int _a;
        _a = in.nextInt();
        int _b;
        _b = in.nextInt();
        int sum;
        sum = solveMeFirst(_a, _b);
        System.out.println(sum);
   }
}