Wednesday, December 25, 2019

Staircase hackerrank Solution

Problem:
Consider a staircase of size

            # 
        ##
    ### 
####

Observe that its base and height are both equal to , and the image is drawn using # symbols and spaces. The last line is not preceded by any spaces.

Write a program that prints a staircase of size .
Function Description
Complete the staircase function in the editor below. It should print a staircase as described above.
staircase has the following parameter(s):
  • n: an integer
Input Format
A single integer, , denoting the size of the staircase.
Constraints
0 < n <= 100
Output Format
Print a staircase of size  using # symbols and spaces.
Note: The last line must have  spaces in it.
Sample Input 
           6
Sample Output
                   #
                ##
            ###
        ####
    #####
######  
Explanation
The staircase is right-aligned, composed of # symbols and spaces, and has a height and width of . 
Solution:
#include <bits/stdc++.h>
using namespace std;

/*
     *
     * Siddharth Goyal
     * Chandigarh Engineering College, Landran
     *
*/
int main()
{
    int i,n,j,k;
    cin>>n;
    for(i=0;i<n;i++)
    {
        for(j=i+1;j<n;j++)
        {
            cout<<" ";
        }
        for(j=0;j<=i;j++)
        {
            cout<<"#";
        }
        cout<<"endl;
    }
    return 0;
}

Plus Minus Hacker Rank Solution

Problem:

Given an array of integers, calculate the fractions of its elements that are positive, negative, and are zeros. Print the decimal value of each fraction on a new line.

Note: This challenge introduces precision problems. The test cases are scaled to six decimal places, though answers with absolute error of up to  are acceptable.

For example, given the array  there are  elements, two positive, two negative and one zero. Their ratios would be ,  and . It should be printed as 

0.400000
0.400000
0.200000 
Function Description
Complete the plusMinus function in the editor below. It should print out the ratio of positive, negative and zero items in the array, each on a separate line rounded to six decimals.
plusMinus has the following parameter(s):
  • arr: an array of integers
Input Format
The first line contains an integer, , denoting the size of the array.
The second line contains  space-separated integers describing an array of numbers .
Constraints

Output Format
You must print the following  lines:
  1. A decimal representing of the fraction of positive numbers in the array compared to its size.
  2. A decimal representing of the fraction of negative numbers in the array compared to its size.
  3. A decimal representing of the fraction of zeros in the array compared to its size.
Sample Input 
         6 
        -4 3 -9 0 4 1
Sample Output 
        0.500000 
        0.333333 
        0.166667
Solution:
#include <bits/stdc++.h>
using namespace std;
int main()
{
    int arr[100],n,i,c1,c2,c3;
    float s1,s2,s3;
    // c1 for negative numbers
    // c2 for negative numbers
    // c3 for zeroes
    cin>>n;
    for(i=0;i<n;i++)
    {
        cin>>arr[i];
    }  
    c1=0;
    c2=0;
    c3=0;
    for(i=0;i<n;i++)
    {
        if(arr[i]>0)
        {
            c1=c1+1;
        }
        if(arr[i]==0)
        {
            c2=c2+1;
        }
        if(arr[i]<0)
        {
            c3=c3+1;
        }
    }
    s1=(float)c1/(float)n;
    s2=(float)c2/(float)n;
    s3=(float)c3/(float)n;
    cout<<s1<<endl<<s3<<endl<<s2;
    return 0;
}

Staircase hackerrank Solution

Problem: Consider a staircase of size             #           ##     ###   #### Observe that its base and height are both equal to...