Question Name:PRIYANKA AND TOYS


#include<iostream>
#include<algorithm>
using namespace std;

int w[100000];

int main()
{
	int n;
	cin >> n;
	for (int i = 0; i < n; i++)
		cin >> w[i];
	sort(w, w + n);
	int ans = 1, prev = w[0];
	for (int i = 1; i < n; i++)
		if (w[i] - prev > 4)
			prev = w[i], ans++;
	cout << ans << endl;
	return 0;
}
  • 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

Leave a Reply

Your email address will not be published. Required fields are marked *

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.