Sort by

recency

|

411 Discussions

|

  • + 0 comments

    //simple and easy understanding code for this problem in this problem we have to first allocate the memory to the total_number_of_books and total_number_of_pages using the calloc because we have to set the data to 0 at first

    int main()
    {
        int total_number_of_shelves;
        scanf("%d", &total_number_of_shelves);
        
        int total_number_of_queries;
        scanf("%d", &total_number_of_queries);
        total_number_of_books = (int*)calloc(total_number_of_shelves,sizeof(int));
        total_number_of_pages = (int**)calloc(total_number_of_shelves,sizeof(int*));
        
        for(int i =0; i< total_number_of_shelves;i++){
            total_number_of_pages[i] = (int*)calloc(total_number_of_books[i],sizeof(int));
        }
        
        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);
                
                total_number_of_books[x] += 1;
                
                int size = total_number_of_books[x];
                total_number_of_pages[x] = (int*)realloc(total_number_of_pages[x], sizeof(int)*size);
                total_number_of_pages[x][size-1] = y;
    
            } else if (type_of_query == 2) {
                int x, y;
                scanf("%d %d", &x, &y);
                printf("%d\n", *(*(total_number_of_pages + x) + y));
            } else {
                int x;
                scanf("%d", &x);
                printf("%d\n", *(total_number_of_books + x));
            }
        }
    
        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;
    }
    
  • + 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;
    

    }