Priyanka and Toys

  • + 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();
        }
    }