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.
  • HackerRank Home

    HackerRank

  • |
  • Prepare
  • Certify
  • Compete
  • Apply
  • Hiring developers?
  1. Prepare
  2. Algorithms
  3. Implementation
  4. Organizing Containers of Balls
  5. Discussions

Organizing Containers of Balls

Problem
Submissions
Leaderboard
Discussions
Editorial

Sort 563 Discussions, By:

recency

Please Login in order to post a comment

  • divyankthewarri1
    4 days ago+ 0 comments

    C++ code :

    string organizingContainers(vector> container) { vector totalBallsInContainer; vector numberOfBallsOfType;

    for(int i = 0 ; i < container.size(); ++i){ int sum = 0; for(int j = 0 ; j < container[i].size(); ++j){ sum += container[i][j]; } totalBallsInContainer.push_back(sum); } sort(totalBallsInContainer.begin(), totalBallsInContainer.end()); for(int i = 0 ; i < container[0].size(); ++i){ int sum = 0; for(int j = 0; j < container.size(); ++j){ sum += container[j][i];
    } numberOfBallsOfType.push_back(sum); } sort(numberOfBallsOfType.begin(),numberOfBallsOfType.end());

    int possible = 1; for(int i = 0; i < numberOfBallsOfType.size(); ++i) { if(numberOfBallsOfType[i] != totalBallsInContainer[i]){ possible = 0; break; } } if(possible) return "Possible"; else return "Impossible"; }

    0|
    Permalink
  • shivshar16
    2 weeks ago+ 0 comments

    way

    • // see if even after n swappings a container will only have that much of balls which it has initially , i.e the no. of balls in the container would not change even after swapping ;
    • // so lets take an example where there're 3 containers and there are 5 balls of type-2 and initailly each container have 4 balls in them, forget the other types just go with the example, no need to give this much stress on understanding, just imagine there is something like that OK, NOW
    • //now even after swapping it how much times you want there wouldn't be a container to contain all the 5 balls of type-2 , this means that the core condition for this problem is that the no. of balls in the containers should be matched with the no. of each type of balls ;;; that's it brother ;)
    1|
    Permalink
  • shivshar16
    2 weeks ago+ 0 comments

    Here's a solution in C :

    //this is a part of qsort function so copy it same
    int compareIntegers(const void *a, const void *b) {
        return (*(const long *)a > *(const long *)b) - (*(const long *)a < *(const long *)b);
    }
     
    char* organizingContainers(int container_rows, int container_columns, int** container) {
        bool flag = true ;
        
        int n = container_rows ;   //cont_rows = cont_columns ==> no. of boxes = no. of types of balls 
        
        long *box = (long *)calloc(container_rows, sizeof(long));
        long *type = (long *)calloc(container_columns, sizeof(long));
        
        for(int i=0; i<n; i++){
            for(int j=0; j<n; j++){
                box[i] += container[i][j];//stores the no. of balls each box contains 
                type[j] += container[i][j];//stores the total no. of each type of ball
            }
        }
        
        qsort(box, n, sizeof(long), compareIntegers);
        qsort(type, n, sizeof(long), compareIntegers);
        
        for(int i=0; i<n; i++){
            if(box[i] != type[i]){ flag = false ; }
        }
        free(box);  free(type);
        
        if(flag == true){return "Possible";}
        return "Impossible" ;
    }
    
    0|
    Permalink
  • Ankit_93
    2 weeks ago+ 1 comment

    I cannot understand how output of below testcase is "Possible" 3 {{0 2 1} {1 1 1} {2 0 0}} Please help me understand.

    0|
    Permalink
  • ao_shadows
    3 weeks ago+ 0 comments

    My C++ solution:

    string organizingContainers(vector<vector<int>> container) {
    
        unordered_map<int,int> existent_type_sums;
        unordered_map<int,int> type_sums;
        unordered_map<int,int> bucket_capacity;
        
        for(size_t i = 0; i < container.size(); i++)
        {
            for(size_t j = 0; j < container.size(); j++)
            {
                int val = container[j][i];
                type_sums[i] += val;
                bucket_capacity[j] += val;   
                
                if(j == container.size()-1)
                {
                    existent_type_sums[type_sums[i]] += 1;
                }
            }
        }
        
        size_t not_filled = container.size();
        
        for(size_t i = 0; i < container.size(); i++)
        {
    	// is there a bucket that matches the bucket capacity?
            if(existent_type_sums[bucket_capacity[i]] > 0)
            {
                existent_type_sums[bucket_capacity[i]] = max(0, existent_type_sums[bucket_capacity[i]] - 1);  
                not_filled--;               
            }
        }
        
        return ((not_filled > 0) ? "Impossible" : "Possible");
    }
    
    0|
    Permalink
Load more conversations

Need Help?


View editorial
View top submissions
  • Blog
  • Scoring
  • Environment
  • FAQ
  • About Us
  • Support
  • Careers
  • Terms Of Service
  • Privacy Policy