Priyanka and Toys

Sort by

recency

|

504 Discussions

|

  • + 0 comments

    charge refund mil jaega aapko 1199 rupaye aapko pahle charge karna hoga FIR aapko refund mil jaega

  • + 0 comments
        w.sort()
        
        result = 1
        m = min(w)
        for i in w:
            if m + 4 < i:
                result+=1
                m = i
        return result
    
  • + 0 comments
    def toys(w):
        count = 0
        w.sort()
        N = len(w)
        i = 0
    
        while i < N:
            # The weight of the current toy.
            current_toy_weight = w[i]
            
            # We need at least one container, so we increment the count.
            count += 1
            
            # We can fit any toy with a weight up to 4 units greater than the first toy in the container.
            limit = current_toy_weight + 4
            
            # We find how many toys fit in this container.
            while i < N and w[i] <= limit:
                i += 1
                
        return count
    
  • + 0 comments
    def toys(w):
        # Write your code here
        i=0
        w.sort()
        minW = w[0]
        count = 1
        while(i< len(w)):
            if w[i] > 4+minW:
                count+=1
                minW = w[i]
            i+=1
        return count
    
  • + 2 comments

    C++ implementation : Intution:- First sort the array and then check with the first element(threshold) if the element is <= threshold+4 , keep going and when it fails the condition increment the answer.

    int toys(vector<int> w) {
        sort(w.begin(),w.end());
        int ans=1;
        int n=w.size();
        int j=0;
    
            int threshold = w[0]+4;
            
            while(j < n){
                if(w[j] <= threshold){
                    j++;
                }
                else {
                    ans++;
                    threshold = w[j]+4;
                    j++;
                    } 
                }
            
        return ans;
    }