How do I solve the staircase problem on Hacker rank

Related searches

I don't know how would you solve, but I would do this:

#include <stdio.h>

int main()
{
    int n = 4; 
    for (int i = n; i > 0; i--) {
        printf("%*s", i - 1 , ""); // print i - 1 number of spaces
        for (int j = n - i + 1; j > 0; j--) 
            printf("#"); // printing n - i + 1 number of '#'
        printf("\n");
    }
    return 0;
}

Recursion: Davis' Staircase, using System; class Solution { static void Main(String[] args) { int N = int.Parse( Console.ReadLine()); for (int i = 0; i < N; i++) Console.WriteLine(new String('#', i + � We would like to show you a description here but the site won’t allow us.


From the requirement:

Note: The last line must have 0 spaces in it.

Now change printf(" "); to something like printf("O"); in your code and you will see what the problem is about

Staircase Discussions | Algorithms, Staircase hackerrank problem can be solved using one for loop also. The complexity of Duration: 10:57 Posted: Jan 20, 2018 Print a right-aligned staircase with n steps. We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.


void staircase(int n)
{
    for(int step = 1; step <= n; step++)
    {
        for(int spaces = n - step; spaces; spaces--)
        {
            putchar(' ');
        }
        for(int star = 0; star < step; star++)
        {
            putchar('#');
        }
        putchar('\n');
    }
}

How To Solve Staircase HackerRank Problem in Java [Using one for , Staircase - Hacker Rank Solution. Your teacher has given you the task of drawing a staircase structure. Being an expert programmer, you� Observe that its base and height are both equal to n, 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 n. Input Format. A single integer, n, denoting the size of the staircase. Output Format.


In this loop

for(j=i;j<n;j++)
{
    printf(" ");
}

there is outputted one redundant space.

Also according to the assignment presented at the provided reference n shall be between (0, 100]. Maybe you should check in the function that outputs the pattern that the passed argument has a value in this range.

And if I am not mistaken the program should be written in C++. Something like the following

#include <bits/stdc++.h>

using namespace std;

// Complete the staircase function below.
void staircase(int n) 
{
    const int MAX_VALUE = 100;

    if ( 0 < n && n <= MAX_VALUE )
    {
        for ( int i = 0; i < n; i++ )
        {
            std::cout << std::setw( n - i )
                      << std::setfill( ' ' )
                      << '#';
            std::cout << std::setw( i + 1 )
                      << std::setfill( '#' )
                      << '\n';
        }
    }
}

int main()
{
    int n;
    cin >> n;
    cin.ignore(numeric_limits<streamsize>::max(), '\n');

    staircase(n);

    return 0;
}

In C the completed program template can look like

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

char* readline();

// Complete the staircase function below.
void staircase(int n) 
{
    const int MAX_VALUE = 100;

    if ( 0 < n && n <= MAX_VALUE )
    {
        for ( int i = 0; i < n; i++ )
        {
            printf( "%*c", n - i, '#' );
            for ( size_t j = 0; j < i; j++ ) putchar( '#' );
            putchar( '\n' );
        }
    }

}

int main()
{
    char* n_endptr;
    char* n_str = readline();
    int n = strtol(n_str, &n_endptr, 10);

    if (n_endptr == n_str || *n_endptr != '\0') { exit(EXIT_FAILURE); }

    staircase(n);

    return 0;
}

char* readline() {
    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; }

        size_t new_length = alloc_length << 1;
        data = realloc(data, new_length);

        if (!data) { break; }

        alloc_length = new_length;
    }

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

    data = realloc(data, data_length);

    return data;
}

Perhaps instead of the test

    if ( 0 < n && n <= MAX_VALUE )

you should use an assert.

Staircase - Hacker Rank Solution - Hacker Rank Solutions, If you have studied for software development interviews, then I am sure you have, or will eventually run into the “Staircase” problem where… Start your range() at 1, and n should start at num_stairs - 2 (as Multiple arguments to print() adds a space: from __future__ import print_function def staircase(num_stairs): n = num_stairs - 2 for stairs in range(1, num_stairs): print(' ' * n, '#' * stairs) n -= 1 print('#' * num_stairs) You can simplify this to one loop:


Two Ways to Solve the HackerRank Staircase Problem in Javascript , The true test of problem solving: when one realizes that time and memory aren't infinite.


Over the course of the next few (actually many) days, I will be posting the solutions to previous Hacker Rank challenges. The page is a good start for people to solve these problems as the time constraints are rather forgiving. The majority of the solutions are in Python 2. Some are in C++, Rust and GoLang. My public HackerRank profile here.


Thanks for the A2A, Anurag Sharma. It is absolutely normal to get overwhelmed when you start solving programming problems. Most of the problems on HackerRank or HackerEarth are designed for programming contests.