Question

C Programming Language Problem Title : Climbing Stairs Bibi climbs stairs of a multi-leveled building. Every...

C Programming Language

Problem Title : Climbing Stairs

Bibi climbs stairs of a multi-leveled building. Every time Bibi climbs a set of stairs, she counts the steps starting from 1 to the number of steps in that particular set of stairs while climbing the stairs. For example if she climbs two set of stairs, the first containing 5 steps and the second containing 3 steps, she will say 1, 2, 3, 4, 5, 1, 2, 3 and the total number of steps would be 8. Find the number of steps that Bibi climbed in each set of steps.

Format Input

The first line of the input contains an integer N, the number of numbers Bibi said. The second line of the input contains N integers Ai , the i-th number Bibi said. The given sequence of Ai will be a valid sequence, that means the whole input can be cut into sequences of 1, 2, . . . , X, where X is the number of steps in a set of stairs.

Format Output

Print the number of steps that Bibi climbed for each set of steps, in the order of the input sequence, separated by single spaces. There is no leading and trailing spaces in the output.

Constraints

  • 1 ≤ N ≤ 1, 000
  • 1 ≤ Ai ≤ 1, 000
  • The given sequence of Ai will be a valid sequence, that means the whole input can be cut into sequences of 1, 2, . . . , X, where X is the number of steps in a set of stairs

Sample Input & Output (1) (standard input & output)

8
1 2 3 4 5 1 2 3
5 3

Sample Input & Output (2) (standard input & output)

10
1 2 1 1 2 3 4 1 2 3
2 1 4 3

Sample Input & Output (3) (standard input & output)

5
1 2 3 4 5
5

Explanation:

The first sample is the example from the problem description.


In the second sample:
The first set of stairs have 2 steps, current sequence is ”1, 2”
second set have 1 steps, current sequence is ”1, 2, 1”
third set have 4 steps, current sequence is ”1, 2, 1, 1, 2, 3, 4”
last set have 3 steps, current sequence is ”1, 2, 1, 1, 2, 3, 4, 1, 2, 3”, just like the input


In the third sample, there is only one set of stairs which contains 5 steps.

0 0
Add a comment Improve this question Transcribed image text
Answer #1
#include <stdio.h>
#include <stdlib.h>
 
// function to return count of stairs in each set of stairs.
int* climbingStairs(int arr[], int n, int N) {
 
    // dynamically creating an array to store the count of stairs in each set of stairs. Variable 'n' stores the number of sets. 'N' is the size of sequence.
    int* result = (int*)malloc(n * sizeof(int));
    int idx = 0;
     
    // maintaining to variables to point at indices of 1 to calculate the difference between them to give number of stairs.
    int prev_idx = 0, curr_idx = 0;
    for (int i = 1; i < N; i++) {
        if (arr[i] == 1) {
            curr_idx = i;
            result[idx++] = curr_idx - prev_idx;
            prev_idx = curr_idx;
        }
    }
    // this is handling the last set of steps case as mentioned in the explanation above.
    result[idx] = N - curr_idx;
    return result;
}
 
int main()
{
    // added this print statements for clarity, remove if you want.
    printf("Enter number of numbers\n");
    int N;
    scanf("%d", &N);
 
    // array to hold the sequence of stairs.
    int* arr = (int*)malloc(N * sizeof(int));
    for (int i = 0; i < N; i++) {
        scanf("%d", &arr[i]);
    }
 
    // variable to store the number of sets.
    int set_of_steps = 0;
    for (int i = 0; i < N; i++) {
        if (arr[i] == 1) {
            set_of_steps++;
        }
    }
      
    // calling the function to return pointer to array.
    int* ans = climbingStairs(arr, set_of_steps, N);
    printf("number of steps in each set:\n");
 
    // above print just for clarity.
    // below loop is printing the result.
    for (int i = 0; i < set_of_steps; i++) {
        printf("%d ", ans[i]);
    }
 
    return 0;
}


Add a comment
Know the answer?
Add Answer to:
C Programming Language Problem Title : Climbing Stairs Bibi climbs stairs of a multi-leveled building. Every...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • C Programming Language Problem Title: Komodo National Park Do You know that Komodo National Park, located...

    C Programming Language Problem Title: Komodo National Park Do You know that Komodo National Park, located in Nusa Tenggara Timur (NTT), is a World Heritage Site by UNESCO, the specialized agency under United Nations (UN). One day, Lili and Bibi were on vacation on one of the islands in the Komodo National Park area and they just realized that they were living alone on that island because the cot- tage owner was completing his unfinished business on the main island....

  • write the solution of the program by python 3 language : I need the program using...

    write the solution of the program by python 3 language : I need the program using list : You are given a non-decreasing sequence of n positive integers a1,a2,…,an. Print the number of distinct values in the sequence. For example, if the sequence is 1,2,2,2,3,4,4,5,7,10, the answer is 6 since distinct values are 1,2,3,4,5,7,10. Input The first line contains a positive integer n (1≤n≤1000) — the length of the sequence. The second line contains n space-separated positive integers a1,a2,…,an (1≤ai≤1000)...

  • write the solution of the program by python 3 language : I need the program using...

    write the solution of the program by python 3 language : I need the program using list or string or loops (while and for) or if,elif,else : You are given a sequence of n non-zero integers a1,a2,…,an. Determine if the given sequence contains a pair of adjacent elements of the same sign (both negative or both positive). Two elements are called adjacent if they stand next to each other in the sequence. Input The first line contains an integer n...

  • write the solution of the program by python 3 language : I need the program using...

    write the solution of the program by python 3 language : I need the program using list or string or loops (while and for) or if,elif,else : i have 15 min to submit the solution please help me   You are given a sequence of n non-zero integers a1,a2,…,an. Determine if the given sequence contains a pair of adjacent elements of the same sign (both negative or both positive). Two elements are called adjacent if they stand next to each other...

  • Write in C language . Thank you Count Sheep Jojo is having problem to sleep at...

    Write in C language . Thank you Count Sheep Jojo is having problem to sleep at night. He can't fall asleep and it makes him feel tired every day. Having this problem, Jojo told his friend Bibi, and Bibi advised him to do sheep counting while he tries to sleep. Jojo decided to try using this trick for N nights, to test its effectiveness. Jojo realized that he would fall asleep if he imagined a total of 10 white sheep....

  • C Programming Language Problem Title: Discount Jojo is browsing the internet while suddenly he sees an...

    C Programming Language Problem Title: Discount Jojo is browsing the internet while suddenly he sees an ad about the new cafe. The promotion is if the price of an item is N dollars, then you can buy the second item for half the price, the third item for a quarter of the original price, and so on, but if it becomes less than M dollars, then you have to pay M dollars. He wonders how much he has to pay...

  • write the solution of the program by python 3 language : I need the program using...

    write the solution of the program by python 3 language : I need the program using list : You are given a sequence of n positive integers a1,a2,…,an, where n is even. Swap adjacent elements in the given sequence and print the resulting sequence: a2,a1,a4,a3,a6,a5,… Input The first line contains a positive even integer n (2≤n≤1000) — the length of the sequence. The second line contains n space-separated integers a1,a2,…,an (1≤ai≤1000) — the elements of the sequence. Output Print n...

  • The input consists of n numbers a1, a2, . . . , an and a target...

    The input consists of n numbers a1, a2, . . . , an and a target value t. The goal is to determine in how many possible ways can we add up two of these numbers to get t. Formally, your program needs to find the number of pairs of indices i, j, i < j such that ai+aj = t. For example, for 2, 7, 3, 1, 5, 6 and t = 7, we can get t in two...

  • Plz i want answer these question using list in python programme. You are given two sequences...

    Plz i want answer these question using list in python programme. You are given two sequences of n integers: 21, 22, ...,an and b1,b2, ..., bn Print a sequence of 2n integers created by alternating elements from the given sequences: a1, 61, 42, 62, a3, 63, ..., an, bn. Input The first line contains a positive integer n (1 <n<1000) - the length of the sequences The second line contains n space-separated integers 01, 02, ..., an (-1000 <a; <...

  • Please provide C language code no c++ ,txt file also needed with comment Finish task 5...

    Please provide C language code no c++ ,txt file also needed with comment Finish task 5 Task5: Breadth First Search (15 pts) · Write a program to read the graph information from the file and traverse the graph using BFS algorithm as introduced in lecture. The input for each algorithm is an undirected unweighted connected graph stored in a local file using an adjacency list. Following is the example of the input file (graph.txt) and the graph First line is...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT