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
  • Prepare
    NEW
  • Certify
  • Compete
  • Career Fair
  • Hiring developers?
  1. Prepare
  2. Algorithms
  3. Greedy
  4. Priyanka and Toys
  5. Discussions

Priyanka and Toys

Problem
Submissions
Leaderboard
Discussions
Editorial

Sort 440 Discussions, By:

recency

Please Login in order to post a comment

  • thecodingsoluti2
    1 week ago+ 0 comments

    Here is problem solution - https://programs.programmingoneonone.com/2021/07/hackerrank-priyanka-and-toys-problem-solution.html

    0|
    Permalink
  • lokzy
    2 weeks ago+ 0 comments

    Python3 Solution

    def toys(weights):
            # Sort the list of weights in non-decreasing order
            weights.sort()
    
            # Initialize the container count
            container_count = 1
    
            # Set the minimum weight as the threshold
            threshold = weights[0]
    
            for i in range(1, len(weights)):
                    # Check if the weight of current item is greater than threshold + 4
                    if weights[i] > threshold + 4:
                            # Increase the container count
                            container_count += 1
                            # Update the threshold to the current item
                            threshold = weights[i]
    
            # Return the container count
            return container_count
    

    -----------------> github

    0|
    Permalink
  • grbz_kerem
    3 weeks ago+ 0 comments

    JavaScript Solution

    function toys(w) {
      let container = 1;
      const sortedWeights = w.sort((x, y) => x - y);
      let currentMinimum = sortedWeights[0] + 4;
      sortedWeights.forEach((value) => {
        if (value > currentMinimum) {
          currentMinimum = value + 4;
          container++;
        }
      });
      return container;
    }
    
    0|
    Permalink
  • davepaul321
    3 weeks ago+ 0 comments
    def toys(w):
        # Write your code here
        w.sort()
        thr=w[0]
        count=1
        for i in range(len(w)):
            if w[i]>thr+4:
                thr=w[i]
                count+=1
        return count
    
    0|
    Permalink
  • mostafa_ragheb_1
    1 month ago+ 0 comments

    cpp

    int toys(vector<int> w) {
    sort(w.begin(),w.end());
    int counter=0;
    for(int i=0;i<w.size();i++){
        int temp=w[i];
        if(i<w.size()){
                while(temp+4>=w[i]){ 
                    i++;
                }
    
        }
        i--;
        counter++;
    }
    

    return counter; }

    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