Question Name:PRIYANKA AND TOYS

import java.io.*;
import java.util.*;
public class TestClass {
	 public static void main(String[] args) { 
		      Scanner sc = new Scanner(System.in);
        int arraySize = sc.nextInt();
        int[]numArray = new int[arraySize];
        for(int i = 0;i<arraySize;i++){
            numArray[i] = sc.nextInt();
        }
       
        Arrays.sort(numArray);
        
        int count = 1;
        int currentToy=numArray[0];
        for(int i = 1;i<numArray.length;i++){
            if(numArray[i]>currentToy+4){
                count++;
                currentToy=numArray[i];
            }
        }
        System.out.println(count);
	}
}

  • Problem Description
    Little Priyanka visited a kids’ shop. There are N toys and their weight is represented by an array W=[w1, w2,…,wN]. Each toy costs 1 unit, and if she buys a toy with weight w’, then she can get all other toys whose weight lies between [w’,w’+4] (both inclusive) free of cost. 

    Input Format:
    The first line contains an integer N i.e. number of toys.
    Next line will contain N integers,w1,w2,…,wN , representing the weight array.

    Output Format:
    Minimum units with which Priyanka could buy all of toys.

    Constraints
    1 <= N <= 10^5
    0 <= wi <= 10^4, where i belongs to [1,N]
  • Test Case 1
    Input (stdin)5
    1 2 3 17 10
    Expected Output3
  • Test Case 2
    Input (stdin)4
    1 2 3 17
    Expected Output2
Ads Blocker Image Powered by Code Help Pro

Ads Blocker Detected!!!

We have detected that you are using extensions to block ads. Please support us by disabling these ads blocker.

Powered By
Best Wordpress Adblock Detecting Plugin | CHP Adblock