Sort by

recency

|

410 Discussions

|

  • + 0 comments

    Simplest solution: Prepare required storages then Perform insert (with realloc if necessary)

    Code Solution ------------------------>

    int main() ........ { int total_number_of_shelves; scanf("%d", &total_number_of_shelves);

    int total_number_of_queries;
    scanf("%d", &total_number_of_queries);
    
    //1. Prepare Storage
    total_number_of_books = (int *)malloc(total_number_of_shelves * sizeof(int));
    total_number_of_pages = (int **)malloc(total_number_of_shelves * sizeof(int*));
    
        //2. Init Storage
    for (int i = 0; i < total_number_of_shelves; i++) 
    {
        total_number_of_books[i] = 0;
        total_number_of_pages[i] = (int*)malloc(sizeof(int));
        total_number_of_pages[i][0] = 0;
    }
    
    while (total_number_of_queries--) {
        int type_of_query;
        scanf("%d", &type_of_query);
    
        if (type_of_query == 1) {
            /*
             * Process the query of first type here.
             */
            int x, y;
            scanf("%d %d", &x, &y);
    
                        //3. Insert Book
            total_number_of_books[x]++;
    
                        //4. Insert Page
            if(total_number_of_books[x] == 1)
            {
                total_number_of_pages[x][0] = y;
            }
            else 
            {
                total_number_of_pages[x] = realloc(total_number_of_pages[x], total_number_of_books[x]*sizeof(int));
                total_number_of_pages[x][total_number_of_books[x]-1] = y;
            }
    
  • + 0 comments

    The problem is not clear about implementation.

    Here is my explanation.

    // For this, before `while` loop, you need to initialize dynamic arrays (which are declared on top of the main) to store numbers of:
    // - total_number_of_books
    // - total_number_of_pages
    
    // x y : (a) Insert a book (b) with y pages (c) at the end of the x^th shelf.
    
    // (a) Insert a book
    total_number_of_books[x] += 1;  
    // *(total_number_of_books + x) += 1; // the same - equivalent in old books/tutorials
    
    // (b) allocate memory (int) for the new shelf: ( void *ptr, size_t new_size );
    // - total_number_of_pages just get bigger by int as we inserted a book
    
    // (c) assign number of pages [y] to the x^th shelf
    

    Simpler, isn't it?

  • + 0 comments

    Here is Dynamic Array in C solution - https://programmingoneonone.com/hackerrank-dynamic-array-in-c-solution.html

  • + 0 comments

    Just wanted to share this cause I found this easier

    include

    include

    int main() { int shelfbook[100][100], count=0, count2=0; int total_number_of_shelves; scanf("%d", &total_number_of_shelves);

    int total_number_of_queries;
    scanf("%d", &total_number_of_queries);
    
    while (total_number_of_queries>0)
    {   
        total_number_of_queries--;
        int type_of_query;
        scanf("%d", &type_of_query);
    
        if (type_of_query == 1) 
        {
            int x, y; 
    
            for(count; count>0; count--)
            { shelfbook[x][count]=shelfbook[x][count-1];}
    
            scanf("%d %d", &x, &y);
            shelfbook[x][0]= y;
    
            count++;
    
        } 
        else if (type_of_query == 2) 
        {
            int x, y;
            scanf("%d %d", &x, &y);
            printf("%d",shelfbook[x][y]);
        } 
        else if (type_of_query == 3)
        {
            int x;
            scanf("%d", &x);
    
    
            for (int y=0; y<100; y++)
            {
                if (shelfbook[x][y]!=0)
                { count2++;}
            }
            printf("%d\n", count2);
        }
    }
    
    return 0;
    

    }

  • + 1 comment

    In the assigment they missed to tell that you have to allocate the page matrice and the book array :)