TeachingBee

Dynamic Array in C Hackerrank Solution [Easy]

Dynamic Array in C Hackerrank Solution- TeachingBee

Problem Statement

Snow Howler is the librarian at the central library of the city of HuskyLand. He must handle requests which come in the following forms:

1 x y : Insert a book with y pages at the end of the xth shelf.
2 x y : Print the number of pages in the yth book on the xth shelf.
3 x : Print the number of books on the xth shelf.

Snow Howler has got an assistant, Oshie, provided by the Department of Education. Although inexperienced, Oshie can handle all of the queries of types 2 and 3.

Write the logic for the requests of type 1. The logic for requests of types 2 and 3 are provided.

Help Snow Howler deal with all the queries of type 1.

Oshie has used two arrays:

int* total_number_of_books;
/*
 * This stores the total number of books on each shelf.
 */

int** total_number_of_pages;
/*
 * This stores the total number of pages in each book of each shelf.
 * The rows represent the shelves and the columns represent the books.
 */

Constraints:

  • 1 <= total_number_of_shelves <= 10^5
  • 1 <= total_number_of_queries <= 10^5
  • For each query of the second type, it is guaranteed that a book is present on the xth shelf at yth index.
  • 0 <= x < total_number_of_shelves
  • Both the shelves and the books are numbered starting from 0.
  • Maximum number of books per shelf <= 1100.

Input Format

  • The first line contains an integer total_number_of_shelves, the number of shelves in the library.
  • The second line contains an integer total_number_of_queries, the number of requests.
  • Each of the following total_number_of_queries lines contains a request in one of the three specified formats.

Example 1:

5
5
1 0 15
1 0 20
1 2 78
2 2 0
3 0

Output

78
2

Explanation:

There are 5 shelves and 5 requests, or queries.
– 1 Place a 15 page book at the end of shelf 0.
– 2 Place a 20 page book at the end of shelf 0.
– 3 Place a 78 page book at the end of shelf 2.
– 4 The number of pages in the 0th book on the 2nd shelf is 78.
– 5 The number of books on the 0th shelf is 2.

Dynamic Array in C Hackerrank Solution Code

#include <stdio.h>
#include <stdlib.h>

/*
 * This stores the total number of books in each shelf.
 */
int* total_number_of_books;

/*
 * This stores the total number of pages in each book of each shelf.
 * The rows represent the shelves and the columns represent the books.
 */
int** total_number_of_pages;

int main() {
    // Read the total number of shelves and queries.
    int total_number_of_shelves;
    scanf("%d", &total_number_of_shelves);
    
    int total_number_of_queries;
    scanf("%d", &total_number_of_queries);
    
    // Allocate memory for the shelves and initialize book count to 0.
    total_number_of_books = (int *)malloc(sizeof(int) * total_number_of_shelves);
    total_number_of_pages = (int **)malloc(sizeof(int *) * total_number_of_shelves);
    for(int i = 0; i < total_number_of_shelves; i++) {
        *(total_number_of_books + i) = 0;
    }
    
    // Process each query.
    while(total_number_of_queries--) {
        int type_of_query;
        scanf("%d", &type_of_query);
        
        // Add a book to a shelf.
        if(type_of_query == 1) {
            int x, y;
            scanf("%d %d", &x, &y);
            int booksInShelf = *(total_number_of_books + x);
            *(total_number_of_pages + x) = (int*)realloc(*(total_number_of_pages + x), sizeof(int) * (booksInShelf + 1));
            *(*(total_number_of_pages + x) + booksInShelf) = y;
            *(total_number_of_books + x) += 1;
        } 
        // Query the number of pages in a book on a given shelf.
        else if (type_of_query == 2) {
            int x, y;
            scanf("%d %d", &x, &y);
            printf("%d\n", *(*(total_number_of_pages + x) + y));
        } 
        // Query the number of books on a given shelf.
        else {
            int x;
            scanf("%d", &x);
            printf("%d\n", *(total_number_of_books + x));
        }
    }
    
    // Free allocated memory.
    if (total_number_of_books) {
        free(total_number_of_books);
    }
    
    for (int i = 0; i < total_number_of_shelves; i++) {
        if (*(total_number_of_pages + i)) {
            free(*(total_number_of_pages + i));
        }
    }
    
    if (total_number_of_pages) {
        free(total_number_of_pages);
    }
    
    return 0;
}

Explanation of Approach

  1. Initialization: The program starts by reading the total number of shelves and queries. It then allocates memory for the shelves and initializes the book count to zero for each shelf.
  2. Query Processing: The program enters a loop to process each query. There are three types of queries:
    • Type 1: Add a book with a certain number of pages to a specific shelf.
    • Type 2: Query the number of pages in a book on a specific shelf.
    • Type 3: Query the number of books on a specific shelf.
  3. Memory Management: After processing all queries, the program frees the allocated memory.

Dry Run

5
5
1 0 15
1 0 20
1 2 78
2 2 0
3 0

Output

78
2
  1. total_number_of_shelves = 5, total_number_of_queries = 5
  2. Memory allocated for 5 shelves.
  3. First query (Type 1): Add a book with 15 pages to shelf 0.
  4. Second query (Type 1): Add another book with 20 pages to shelf 0.
  5. Third query (Type 1): Add a book with 78 pages to shelf 2.
  6. Fourth query (Type 2): Query the number of pages in the first book on shelf 2. Output is 78.
  7. Fifth query (Type 3): Query the number of books on shelf 0. Output is 2.

The program successfully handles the queries and produces the expected output.

For video solution of this problem refer the below video.

Try out our free resume checker service where our Industry Experts will help you by providing resume score based on the key criteria that recruiters and hiring managers are looking for.

90% of Tech Recruiters Judge This In Seconds! 👩‍💻🔍

Don’t let your resume be the weak link. Discover how to make a strong first impression with our free technical resume review!

Related Articles

Hello World! in C HackerRank Solution 

Problem Statement In this challenge, we will learn some basic concepts of C that will get you started with the language. You will need to use the same syntax to

Why Aren’t You Getting Interview Calls? 📞❌

It might just be your resume. Let us pinpoint the problem for free and supercharge your job search. 

Newsletter

Don’t miss out! Subscribe now

Log In