Priyanka and Toys

Sort by

recency

|

493 Discussions

|

  • + 0 comments

    Here is my PHP solution:

    function toys($w) {
        // Write your code here
        sort($w);
        $count_w = count($w);
        $w_first = $w[0];
        $counter = 1;
        
        for ($i=0; $i < $count_w; $i++) {
            if ($w[$i] > (4 + $w_first)) {
                $counter++;
                $w_first = $w[$i];
            }
        }  
        return $counter;
    }
    
  • + 0 comments

    Here is my c++ solution, you can watch the explanation here : https://youtu.be/Es5l0jbSH9I

    int toys(vector<int> w) {
        sort(w.begin(), w.end());
        int result = 1, mi = w[0];
        for(int item: w){
            if(item > mi + 4){
                mi = item;
                result++;
            }
        }
        return result;
    }
    
  • + 0 comments

    code in java made by poly

    import java.util.Arrays;
    import java.util.Scanner;
    
    public class Toys {
        public static int toys(int[] w) {
            Arrays.sort(w);
            int total = 1;
            int freeToys = w[0] + 4;
            for (int i = 1; i < w.length; i++) {
                if (w[i] > freeToys) {
                    total += 1;
                    freeToys = w[i] + 4;
                }
            }
            return total;
        }
    
        public static void main(String[] args) {
            Scanner scanner = new Scanner(System.in);
            int n = scanner.nextInt();
            int[] w = new int[n];
            for (int i = 0; i < n; i++) {
                w[i] = scanner.nextInt();
            }
            System.out.println(toys(w));
            scanner.close();
        }
    }
    
  • + 0 comments
    #nobara
    
    import math
    import os
    import random
    import re
    import sys
    
    
    
    
    def toys(w):
        w.sort()
        total = 1
        free_toys = w[0] + 4
        for i in range(1, len(w)):
            if w[i] > free_toys:
                total += 1
                free_toys = w[i] + 4
        return total
    
    n = int(input().strip())
    w = list(map(int, input().strip().split(' ')))
    print(toys(w))
    
  • + 0 comments

    code in python made by poly

    def toys(w):
        w.sort()
        total = 1
        free_toys = w[0] + 4
        for i in range(1, len(w)):
            if w[i] > free_toys:
                total += 1
                free_toys = w[i] + 4
        return total
    
    n = int(input().strip())
    w = list(map(int, input().strip().split(' ')))
    print(toys(w))