Peter visited Big Bazar and he was very delighted to know the Loot offer on marbles. The offer was that, If he buys a marble with price p', then he will get all other marbles whose price lies between [p′,p′+4] (both inclusive) in free of cost. Suppose there are N marbles and their prices are represented by an array P=[p_1,p_2,p_3......p_N]. So, help Peter to find out an optimal way in which he shall get all the marbles.
Input Format:
The first line contains an integer N i.e. number of marbles.
Next line will contain N integers, p_1,p_2,…,p_N, representing the marble array.
Output Format:
Minimum units of marble with which Peter could take all marbles to his home.
Constraints:
1≤N≤10 ^ 5
40≤ p_i ≤10 ^ 4, where i∈[1,N]
SAMPLE INPUT
5
7 3 6 12 18
SAMPLE OUTPUT
3
Explanation
When he choose marble of price 3 ,then he will get marbles of price 6 and 7 free with it as, and both (6 and 7) lies in the range (3 + 4 = 7).
Now he choose 12 and will get no other marbles free with it as none of marbles price lies in the range (12+ 4 = 16).
Now only 1 marble is left i.e., marble of price 17 so he will simply take that also.
So, total 3 units are required to grab all marbles.
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
We are given a problem: Peter buys marbles; if he buys a marble with price p', then he gets for free all marbles whose price lies in [p', p' + 4] (both inclusive). The goal is to purchase all marbles with the minimum number of marbles he actually pays for (i.e., minimum "units" of marbles he buys). Input: N up to 1e5, p_i in [40, 10000] (though sample includes 3 which is outside that range? Actually constraints say 40 ≤ p_i ≤ 10^4, but sample has 3 and 6,7,12,18. Maybe the constraint is generic but sample violates? Might be a typo; ignore constraints for now, just handle range values). We need to find minimum number of purchased marbles such that every marble is either purchased or obtained free from some purchased marble.
Interpretation: There is a set of marbles with prices. The rule: If you pay for a marble with price p, you get all marbles whose prices are in the interval [p, p+4] for free (including that marble itself? Typically, when you buy a marble, you get the marble; but the free ones are additional. However, the objective is to count the number of marbles you actually pay for. If you pay for a marble, you also get that marble. So the total you have to pay for is a subset such that the union of intervals [p, p+4] for each p in the subset covers all marble prices (consider multiple copies? It says marbles, so each marble is a distinct item with a price; there may be duplicates? Usually array includes prices, there could be multiple marbles with same price. The free condition: If he buys a marble with price p', then he gets all other marbles whose price lies between [p', p'+4] free. That includes marbles with same price? The phrase "all other marbles" suggests excluding the one he buys? But if he buys it, he obviously gets it; the free ones are others. However, for covering all marbles, we need that each marble is either purchased or obtained free from some purchase. If there is a marble with price p, it can be obtained free if there exists a purchased marble with price q such that p ∈ [q, q+4] and p ≠ q? Or possibly if p = q, it's the purchased marble itself, so it's counted as purchased. So effectively, each marble must be covered by an interval from a purchased marble; the purchased marble is itself covered by its own purchase. So we need to choose a set S of marbles (their prices) such that for every marble with price x, there exists a y in S with y ≤ x ≤ y+4. That's a covering problem.
We want minimal |S|.
This is similar to covering points on the line with intervals of length 4 (with left endpoint at the chosen point). But note: The interval is defined by the purchased marble's price as left endpoint, and length exactly 4 inclusive. So interval length is 4 in terms of price. Since prices are integers, the interval covers exactly 5 integer values? Actually inclusive: [p, p+4] covers p, p+1, p+2, p+3, p+4. So width 4.
We have N points (prices) possibly with duplicates. We need to select a subset such that each point lies in at least one interval from a selected point. The selected point itself is included.
This is a classic greedy problem: Sort the prices. Then we can apply a covering algorithm: Starting from the leftmost uncovered point, choose a marble to purchase? But note that the interval is defined by the purchased marble's price; the interval covers from that price up to that price+4. So if we want to cover the leftmost uncovered marble (with price x), we could purchase that marble itself, or perhaps purchase some marble with price ≤ x ≤ price+4. But if we purchase a marble with price less than x, its interval would be [price, price+4]; if it covers x, then price ≤ x ≤ price+4 => price ≥ x-4. So any purchase with price in [x-4, x] would cover x. To minimize total purchases, it's a set cover but on a line it's easy: sort unique prices? But duplicates: we might have multiple marbles with same price. However, the selection of which specific marble to purchase matters: if we decide to purchase a marble with price p, that covers all marbles with prices in [p, p+4], including all duplicates. So the cost is 1 for that purchase, regardless of how many marbles it covers. So essentially we have a set of points (with multiplicities) on integer line; we can place intervals of length 4 starting at some of these points (must be actual existing prices) such that every point is covered. The goal minimize the number of intervals placed. This is reminiscent of a "minimum dominating set" in an interval graph? Actually each marble can cover others within distance up to 4 to the right, but not to the left (since interval is [p, p+4], so it covers points to the right only, not left of p). Wait: Interval is [p, p+4], so it covers prices greater than or equal to p, up to p+4. It does not cover prices less than p. So the coverage is only to the right (including the point itself). That is important: The interval is rightwards. So if you purchase a marble with price p, you cover that marble and any marbles with higher price up to p+4. It does not cover marbles with price lower than p. So coverage is only forward. That means the order matters: covering from left to right.
Thus this is like we have points sorted by price. We need to choose a set of points (purchases) such that every point is covered by some purchase with price <= point price <= purchase price+4. Since coverage goes rightwards, the optimal strategy is to process from smallest price to largest. For each uncovered marble, we want to select a purchase that covers as many as possible to the right. But the purchase must be a marble that exists. If we have multiple marbles at the same price, we could purchase one of them; then it covers all marbles at that price (since p to p+4 includes p) and marbles up to p+4. So duplicates at same price are covered automatically if we purchase any one of them? Actually if we purchase a marble of price p, it covers all other marbles whose price lies between [p, p+4]. Does it cover other marbles with the same price p? "All other marbles" suggests yes, but careful: if there are multiple marbles with price p, and you buy one of them, you get the other marbles of same price for free. So yes, they are covered. So a purchase at price p covers all marbles with price in [p, p+4], regardless of count.
Thus the problem reduces to: Given a multiset of numbers, we can select some numbers such that for every number x, there exists a selected y with y ≤ x ≤ y+4. Minimize number of selected numbers.
This is similar to "minimum number of points to cover intervals" but here intervals are anchored at selected points and cover forward. Typically, for covering points with intervals of fixed length when intervals can be placed anywhere, the greedy is to sort points and then repeatedly pick the smallest uncovered point and place an interval covering it and as many as possible, by setting the interval's left endpoint at that point (or at the point itself). However, here intervals must have their left endpoint at a point that exists. That adds a constraint: when we encounter the smallest uncovered price x, we cannot arbitrarily set the left endpoint to x if there is no marble with price exactly x? But we must have a marble to purchase at that price. However, we could purchase a marble with price in [x-4, x] that exists and use it to cover x. But since x is the smallest uncovered, the only way to cover it is to select some marble with price <= x that also covers x. But if there is a marble with price less than x, selecting it would also cover some points to its left? Actually, if we select a marble with price less than x, its coverage starts from that price and goes up to price+4. To cover x, we need price+4 >= x, i.e., price >= x-4. So we could select a marble with price in the range [x-4, x]. But note that if we select a marble with price less than x, it might have already been considered (maybe it's already purchased?) but we are at the smallest uncovered price x, meaning all points with price < x are already covered. So those points are covered, but we could still purchase one of them if it's not already purchased? But they are covered, not necessarily purchased. Could we purchase a marble that is already covered for free? Yes, we could purchase it, but that would add cost. However, if that marble's price is in [x-4, x] and it is not already purchased, we could purchase it to cover x and possibly more to the right. But is it allowed to purchase a marble that is already free? The problem: "Peter visited Big Bazar and he was very delighted to know the Loot offer on marbles. The offer was that, If he buys a marble with price p', then he will get all other marbles whose price lies between [p′,p′+4] (both inclusive) in free of cost. Suppose there are N marbles and their prices are represented by an array P=[p_1,p_2,p_3......p_N]. So, help Peter to find out an optimal way in which he shall get all marbles." So we need to get all marbles; we can decide which marbles to purchase. Purchasing a marble costs that marble's price? Actually the output: "Minimum units of marble with which Peter could take all marbles to his home." It means the minimum number of marbles he has to pay for (i.e., buy). The cost in money is not considered; just count the number of marbles purchased. So we can purchase any marble, even if it would be free from another purchase, but that would increase the count, so we wouldn't do that. So we want a set of purchased marbles such that all marbles are covered. So the purchased marbles are a subset of the multiset; they are the ones we actually pay for.
Thus, if a marble with price < x is already covered (by some previous purchase), it is not purchased. But could we purchase it? Yes, but it would be extra cost, so not optimal. So we assume we don't purchase already covered marbles unless necessary to cover future marbles.
Given that all points with price < x are covered, we need to cover x. The only way to cover x without purchasing an already covered marble (which would be extra) is to purchase a marble with price in [x-4, x] that is not already covered (i.e., it might be uncovered? But if it's in [x-4, x-1], since x is smallest uncovered, those points are less than x, so they must be covered already. They are covered, but they might not be purchased. Could we purchase one of them? That would increase count, but maybe necessary to cover x if there is no marble with price exactly x? Wait, there is a marble with price x because x is an existing price among marbles? x is the price of an uncovered marble, meaning there exists at least one marble with price x that is not covered. So we could purchase that marble (price x) to cover itself and maybe others. That seems natural: pick the uncovered marble as a purchased one. That would cover all marbles with prices in [x, x+4]. This seems optimal because it doesn't incur extra purchase beyond what's needed to cover this marble. Alternatively, we could purchase some marble with price y where y ∈ [x-4, x-1] that is already covered (so not purchased yet). That would add a purchase, but maybe it could cover more marbles to the right than just purchasing x? Since y < x, its coverage goes up to y+4 which might be less than x+4? Actually y+4 could be less than x+4 or greater, depending. Since y < x, y+4 could be greater than x+4 if y is close to x? Example: x=5, y=4 => y+4=8, x+4=9, so y+4=8 is less than 9? Actually 8 < 9, so x+4 larger. y=2 => y+4=6 <9. So generally, y+4 ≤ x+4? Because y ≤ x, so y+4 ≤ x+4. Actually if y < x, y+4 < x+4? Since adding 4 preserves inequality: y+4 < x+4. So the coverage from a purchase at y is strictly less far to the right than from a purchase at x (since the right endpoint is y+4 < x+4). So purchasing x gives a coverage that extends further to the right. So purchasing x is better for covering future marbles. Also, purchasing y would add a new purchase (since y is not currently purchased, though covered). So it's strictly worse: more purchases and less coverage. Therefore, it's never optimal to purchase a marble with price less than the smallest uncovered price. So the greedy approach: Sort all marble prices. Then iterate: keep track of the smallest price not yet covered. When we find such, we will purchase that marble (i.e., select it). Then that purchase covers all marbles with prices from that price up to that price+4. So we can mark all marbles within that range as covered. Then continue to the next uncovered price beyond that range.
But careful: Since we have duplicates, we need to consider all marbles. But essentially, we can compress: Sort the distinct prices? However, duplicates don't affect the covering because if we cover a price value, we cover all marbles with that price. So we can think in terms of distinct price values, but the purchases must be made on actual marbles. However, if there are multiple marbles at the same price, and we need to purchase one to cover that price, we can do so. That purchase also covers all other marbles at that price. So indeed, the covering problem reduces to covering the set of distinct price values, but we need to ensure that for each distinct price we cover, we have at least one marble at that price (which we do). So the minimal number of purchases is the minimum number of intervals (with left endpoints at existing price points) covering all distinct price values. But wait: There is a nuance: If there are multiple marbles at the same price, and we choose to purchase one of them, it covers all others at that price. That's fine. But what about covering a price value that has no marble? Actually, the problem only cares about covering marbles, not price numbers. If a price value appears, we need to cover all marbles with that price. If a price value does not appear, we don't care. So distinct price values with their multiplicities are fine. So we can compress: consider the sorted list of distinct prices (with counts). But the greedy algorithm can work on the sorted list of all marbles as well.
But is there any situation where it's optimal not to purchase the smallest uncovered marble, but to purchase a marble with a larger price to cover it? That would not cover the smallest uncovered because the interval starts at the purchase price, which is larger than the uncovered price, so the uncovered price would be less than the purchase price, and thus not covered. So it cannot cover leftward. Therefore, to cover the smallest uncovered price, we must purchase a marble with price ≤ that price. As argued, purchasing a marble with price < that price is suboptimal, so purchasing that marble itself is optimal. So the greedy algorithm is optimal: repeatedly pick the smallest uncovered price, purchase that marble, and cover all prices up to that price+4.
Thus answer is the number of such purchases.
Let's test on sample: prices: 7,3,6,12,18. Sorted: 3,6,7,12,18. Step1: smallest uncovered =3, purchase it, cover up to 7 (3+4=7). So 3,6,7 covered. Next uncovered =12, purchase it, cover up to 16 (12+4=16). This covers 12 only (since 18 >16). Next uncovered =18, purchase it. Total purchases=3. Matches sample.
Now consider edge cases: duplicate prices: e.g., prices: 5,5,5,9. Sorted:5,5,5,9. Greedy: smallest uncovered=5, purchase a 5, cover up to 9 (5+4=9). This covers 5 and 9. So all covered. Total purchases=1. That seems correct: buy one 5, get the other 5s and the 9 free because 9 is within [5,9]? 9 is exactly 5+4, so yes. So answer=1.
Another: prices: 1,2,10. Sorted:1,2,10. Step1: smallest uncovered=1, purchase 1, cover up to 5. Covers 1 and 2. Next uncovered=10, purchase 10. Total=2. Check if alternative: purchase 2 instead of 1: purchase 2 covers [2,6], covers 1? No, because 1 <2, not covered. So need also purchase 1 or some other? Could purchase 2 and then 10, but 1 remains uncovered. Could purchase 1 and 10 =2. Could purchase 10 only? 10 covers [10,14], doesn't cover 1,2. So need at least 2. So greedy is optimal.
What about scenario where prices are 1,5,6,10. Sorted:1,5,6,10. Greedy: smallest uncovered=1, purchase 1, cover up to 5 (1+4=5). Covers 1 and 5. Next uncovered=6, purchase 6, cover up to 10 (6+4=10). Covers 6 and 10. Total=2. Check alternative: purchase 5 (covers [5,9] covering 5,6) and purchase 10? Then 1 remains uncovered, need purchase 1 as well ->3. Purchase 1 and 5? That covers 1,5,6? 1 covers up to 5, so 6 not covered. So need another. So greedy is best.
But is there ever a case where skipping a marble and purchasing a later marble could reduce count? No, because the smallest uncovered must be covered by some purchase with price ≤ it. The only candidate prices are ≤ that uncovered price. Among those, picking the uncovered price itself yields the maximum rightward coverage (since right endpoint = price+4). So it is optimal to select that marble.
Thus algorithm: Sort the array. Then iterate i from 0 to N-1, but we need to skip covered ones. Simpler: Let current index i=0. purchases=0. While i<N: let start = P[i]; purchases++; then move i forward while P[i] <= start+4 (i.e., covered). After that continue. This works even with duplicates because if there are multiple marbles with same price as start, they are covered.
Check: P sorted: [3,6,7,12,18] i=0, start=3, purchases=1, then while i<N and P[i] <= 7, i becomes 3 (since index0=3, index1=6<=7, index2=7<=7, then index3=12>7). Then i=3, start=12, purchases=2, while i<N and P[i]<=16, covers 12, i becomes 4 (since 12<=16, then 18>16). i=4, start=18, purchases=3, while i<N and P[i]<=22, covers 18, i becomes 5. Done. Works.
So the answer is simply the number of "clusters" when grouping marbles such that within each group, the difference between the smallest and largest price <=4? Not exactly; because the group is defined by being covered by a single purchase at the smallest price of the group. The greedy groups marbles such that each group has prices within an interval of length 4, and the groups are determined by taking the smallest uncovered as the start, and including all that are ≤ start+4. This is essentially the classic "minimum number of intervals to cover points when intervals have fixed length and can start at points" – the greedy yields optimal solution.
But is there any nuance regarding the requirement that the purchased marble must be exactly one of the marbles? Since we are using the smallest uncovered marble as the purchased one, that is fine. Also, note that there might be a case where the smallest uncovered price is p, but there is no marble with price exactly p? That can't be because p is from the array; it's a marble's price. So it exists.
Thus algorithm is straightforward: sort array, then iterate as described. Complexity O(N log N) due to sorting.
Constraints: N up to 1e5, values up to 1e4, but that's fine.
However, we must double-check the constraints: They said 40 ≤ p_i ≤ 10^4, but sample has 3,6,7,12,18. Possibly the actual test cases respect constraints. But our algorithm works regardless.
Edge Cases: All marbles same price, e.g., [50,50,50]: sorted:50,50,50. i=0 start=50, purchases=1, then while i< N and P[i]<=54, all covered, i becomes 3. Answer=1. Good.
What about prices that are far apart: [1,10,20,30] each >4 apart: each purchase covers only itself. So purchases=4.
Now check scenario: prices: 1,2,3,4,5,6,7,8,9,10. Greedy: start at 1, covers up to 5 (1-5). Then start at 6, covers up to 10. Purchases=2. Could we do better with 2 purchases? Possibly purchase 3 and 8? 3 covers up to 7, 8 covers 8-12, so covers 1? No, 1 not covered. So must start with 1 or maybe 2? Purchase 2 covers 2-6, then 7? Not covered. Need purchase for 1. So must include 1. Then after covering up to 5, next uncovered is 6. Purchase 6 covers 6-10. That's 2. So minimal is 2. Could we do with 1? No, because interval length is only 5 numbers (p to p+4), but we have 10 numbers spread across 10, so one interval covers at most 5 distinct price values, but there are 10 values, so need at least ceil(10/5)=2. So greedy gives optimal.
But what if values are not contiguous? Like 1,2,3,4,5,10,11,12,13,14. Greedy: 1 covers 1-5, then 10 covers 10-14. Purchases=2. OK.
Now think about the nuance: The interval is inclusive of both endpoints, so it covers 5 distinct integer values if they exist. But if there are gaps, it's fine.
Thus answer is simple.
But wait: The problem statement: "If he buys a marble with price p', then he will get all other marbles whose price lies between [p′,p′+4] (both inclusive) in free of cost." The phrase "all other marbles" might imply that if he buys a marble, he does not get that marble for free; he pays for it. But he still gets it obviously, it's just not free. The free ones are others. So covering includes the purchased marble itself because it's not free but we count it as purchased. So we count it as one unit. So our covering model stands: each marble must be either purchased or free. The purchased marble is counted. So if we have a marble that is purchased, it's covered by its own purchase. That's fine.
Now, the greedy algorithm as described counts a purchase for the smallest uncovered, and then covers it and all within range. That includes that marble. So number of purchases is number of such groups.
Now check if there might be a more optimal solution where we purchase a marble that is not the smallest uncovered but still covers that smallest uncovered by being less than it (i.e., from earlier, but we argued it's suboptimal because it would be an extra purchase). However, is it possible that by purchasing a marble with price less than the smallest uncovered, we could avoid a purchase later that would otherwise be needed? For example, consider prices: 1, 2, 6, 7. Sorted: 1,2,6,7. Greedy: purchase 1, covers up to 5, covering 1,2. Then purchase 6, covers up to 10, covering 6,7. Total=2. Could we do better? Try purchase 2 (covers 2-6) covering 2,6; then purchase 7? 7 would cover 7-11 covering 7; but 1 is uncovered, so need purchase 1 =>3. Not better. Purchase 1 and 7? 1 covers 1-5 covers 1,2; 7 covers 7-11 covers 7; 6 uncovered => need purchase 6 =>3. Purchase 2 and 6? 2 covers 2-6 covers 2,6; 7 uncovered, need purchase 7; 1 uncovered, need purchase 1 =>3. So greedy is best.
What about a scenario where there is a cluster that overlaps in a way that picking a slightly larger starting point could cover more? For example: prices: 1,5,9. Greedy: purchase 1 covers up to 5, covers 1 and 5; then purchase 9. Total=2. Could we purchase 5 to cover 1? No, because 5 covers [5,9], not 1. So need 1 anyway. So greedy.
Consider: 1,2,3,4,5,6,7,8,9,10. We already did. Greedy gives 2. Could we do 2 purchases but different: purchase 3 covers 3-7, purchase 8 covers 8-12. That leaves 1,2 uncovered. So need third. So not better.
Maybe consider scenario where the smallest uncovered price is p, but there is another marble with price p+1, and buying that p+1 would also cover p? No, because interval starts at p+1, so it covers p+1 to p+5, does not cover p. So not.
Thus greedy is optimal.
Now consider the possibility of negative prices? Constraints say positive but not necessary.
Now confirm with more complicated cases: Suppose we have prices: 1,4,5,8,9,12. Sorted:1,4,5,8,9,12. Greedy: purchase 1, cover up to 5 (covers 1,4,5). Next uncovered=8, purchase 8, cover up to 12 (covers 8,9,12? 12 <= 12? Yes, 8+4=12, so 12 is covered). Total=2. Alternative: purchase 4, cover up to 8 (covers 4,5,8) then need to cover 1 and 9,12. Could purchase 1 and 12? That's 3. Not better. So greedy optimal.
Now test scenario where there is a large gap: 1,2,3, 10,11,12. Greedy: purchase1 covers up to5 =>1,2,3; purchase10 covers up to14 =>10,11,12 =>2.
All good.
Thus the problem is solved by that greedy.
But wait: Could there be scenario where we have multiple marbles with same price, and it's optimal to not purchase the smallest uncovered but purchase a marble with that same price? Actually the smallest uncovered is that price; so we would purchase one of those marbles. That's fine.
Now we need to output the minimum units (i.e., number of purchases).
Thus the solution: read N, read array, sort, then do greedy count.
But given constraints N up to 1e5, sorting O(N log N) is fine.
Now confirm sample: N=5, array:7 3 6 12 18 -> sorted:3,6,7,12,18 -> count as described ->3.
Thus answer is 3.
Now consider edge: N=0? Not possible because N>=1.
Now we must produce code.
But before finalizing, let's think if there is any twist: The offer says "If he buys a marble with price p', then he will get all other marbles whose price lies between [p′,p′+4] (both inclusive) in free of cost." The phrase "all other marbles" might be interpreted that he does not get the purchased marble for free, obviously. But when we consider covering, it's natural.
However, could there be a scenario where buying a marble gives free marbles, but the free marbles could also be used to get further free marbles? No, because the offer applies only at time of purchase; it's not transitive. So it's just as we modeled.
Now, is it allowed to purchase multiple marbles? Yes, he can choose any set to purchase. The goal is to minimize number purchased.
Thus the problem reduces to set cover with intervals of fixed length anchored at points. The greedy is optimal.
Now let's formalize proof: The set of points is sorted. Let the optimal solution purchase set be S sorted as s1 < s2 < ... < sk. Consider the smallest purchased marble in optimal, say s1. It must cover the smallest price point p_min. Since s1 ≥ p_min? Actually to cover p_min, we need s1 ≤ p_min ≤ s1+4. Since s1 is the smallest purchase, it can't be larger than p_min? It could be less than p_min if there is a price less than p_min? p_min is the smallest price, so s1 cannot be less than p_min because if s1 < p_min, then s1 is a purchase of a marble with price less than p_min, but there is no marble with that price? Wait, s1 is a purchase, which must be a marble that exists. So if s1 < p_min, that means there exists a marble with price less than p_min, which contradicts p_min being the smallest price. Therefore s1 = p_min. So the optimal must purchase the smallest price marble. Then after covering up to s1+4, the remaining uncovered marbles are those > s1+4. The problem reduces to the subproblem on those points, and by induction, the greedy is optimal. This proof holds if the smallest price marble is unique? If there are multiple marbles with the smallest price, we need to purchase at least one of them. But could the optimal purchase a marble with price equal to p_min? Yes, but it could purchase a marble with price > p_min but still cover p_min? That's impossible because to cover p_min, we need a purchase with price ≤ p_min and ≥ p_min-4. Since p_min is the smallest price, the only price ≤ p_min is p_min itself. So purchase must be at p_min. So optimal must include at least one purchase at p_min. So greedy picks one. So it's optimal.
Thus algorithm stands.
Now consider the possibility of negative prices? If p_min could be negative, then there might be purchase price less than p_min? No, p_min is smallest price, so any purchase must have price ≥ p_min. To cover p_min, need purchase price ≤ p_min, so purchase price = p_min. So same.
Thus code is trivial.
But wait: The constraints mention p_i between 40 and 10000. That might be irrelevant. But we can still sort.
Now output: print the count.
Now time to code.
But careful: Input format: first line N, second line N integers. So we need to parse.
We must also consider that there might be multiple test cases? The problem statement doesn't indicate multiple test cases. So single input.
Thus final solution:
Read N.
Read list P of N integers.
Sort P.
Initialize count = 0, i = 0.
While i < N:
count += 1
current = P[i]
while i < N and P[i] <= current + 4:
i += 1
Print count.
Edge: If N=0 (not possible), but fine.
Complexity O(N log N).
Now let's think about potential pitfalls: The array might have large values but that's fine. The while loop correctly skips all covered.
But what about if there are duplicate values exactly at the boundary? For example, current=3, current+4=7. If there are multiple 7s, they will be covered. That's correct.
Now test with some random cases using mental simulation.
Case: [1,2,3,4,5,6,7,8,9,10] -> i=0, current=1, cover up to5, i moves to index5 (value6) -> count=1; then current=6, cover up to10, i moves to end -> count=2. Good.
Case: [1,5,6,7,11] -> i=0, current=1 cover up to5 covers 1,5? 5 is <=5, yes covers 5. i moves to index2? Sorted:1,5,6,7,11. i=0: current=1; while i<N and P[i]<=5: i=1 (value5<=5), i=2 (value6>5) stop. So i=2. count=1. Next: current=6 cover up to10 covers 6,7. i moves to index4 (value11>10). count=2. Next: current=11 cover up to15, i moves to end. count=3. Is that optimal? Could we do better? Alternative: purchase 5 (covers 5-9) covers 5,6,7; then purchase 1 covers 1; then purchase 11. That's 3. Could we purchase 1 and 6? 1 covers 1-5 (covers 1,5); 6 covers 6-10 (covers 6,7); 11 uncovered. So 3. Purchase 1 and 5 and 11? That's 3. So optimal is 3. Greedy gives 3. OK.
Case where the greedy might be non-optimal if we could "skip" a marble and purchase a later one that covers the skipped? But skipped marble would remain uncovered because it's to the left of the purchase, so can't. So fine.
Thus answer is done.
Now I'll craft a Python solution:
def solve():
import sys
data = sys.stdin.read().strip().split()
if not data:
return
n = int(data[0])
arr = list(map(int, data[1:1+n]))
arr.sort()
i = 0
count = 0
while i < n:
count += 1
upper = arr[i] + 4
i += 1
while i < n and arr[i] <= upper:
i += 1
print(count)
We should handle that data may have more than N numbers? Typically exactly N. But safe to slice.
That's it.
But wait: The problem might be from some coding challenge where they expect a C++ solution. But here we need to produce answer in Python? The user didn't specify language. But we are ChatGPT; we can provide explanation and code in Python or pseudocode. In the final answer, we should present the algorithm and code.
Given the instructions: "Peter visited Big Bazar ..." We need to help find an optimal way. So we output minimum units. So the answer is just the number.
For the sample, output 3.
Thus final answer: code as above.
But I'll also provide reasoning and complexity.
Now there is a nuance: The offer says "If he buys a marble with price p', then he will get all other marbles whose price lies between [p′,p′+4] (both inclusive) in free of cost." Could it be interpreted that when he buys a marble, he gets other marbles whose price lies in that range, but not the ones with price exactly p'? That is "other marbles" excludes the one he buys. So the purchased marble itself is not free, but it's obviously taken because he bought it. So coverage includes the purchased marble because it's taken. So the set of marbles taken is all marbles that are either purchased or free. So coverage as we used includes the purchased marble's price as well (since we count it as taken). So we need to ensure that each purchased marble is counted as a taken marble, but we don't need to cover it via free from others. In our model, we require that for each marble, there exists a purchase such that the marble's price is in [purchase_price, purchase_price+4]. For purchased marble, it's trivially satisfied because purchase_price equals its price, which lies in the interval. So correct.
One more nuance: Could a marble be obtained free from multiple purchases? That doesn't matter.
Thus answer stands.
Now, I'll produce final output.
But before finalizing, consider if there is any scenario where we can purchase a marble that is not in the current set of marbles? No, must be from the array.
Thus done.
Now write final answer with explanation and code.
I'll include that the greedy algorithm works.
Check edge: N=1, any price, count=1. Because you must buy that one. Greedy: i=0, count=1, upper = p+4, i increments to 1, loop ends, prints 1. Correct.
Thus final.
Now let's produce final response. The problem asks for the minimum number of marbles Peter needs to purchase so that every marble is either purchased or obtained for free via the offer: when a marble of price (p') is bought, all other marbles with prices in ([p', p'+4]) become free.
We can model this as covering all given price points using intervals of length 4 that start at chosen prices (the purchased marbles). Since an interval only covers prices to the right (including its start), an optimal strategy is to process the marbles in increasing order of price and repeatedly buy the smallest‑priced marble that is not yet covered; this purchase then covers all marbles with prices up to that price + 4. This greedy approach is optimal because the smallest uncovered price must be purchased itself (no other purchase can cover it from the left), and buying it gives the farthest possible reach to the right.
Algorithm
Read (N) and the array (P) of (N) integers.
Sort the array in non‑decreasing order.
Initialize count = 0 and an index i = 0.
While i < N:
Increment count (we buy the marble at P[i]).
Set limit = P[i] + 4.
Move i forward while i < N and P[i] <= limit (these marbles are now covered).
Print count.
Complexity
Sorting takes (O(N \log N)).
The linear scan is (O(N)).
Overall (O(N \log N)), which is efficient for (N \le 10^5).
Sample Execution
Input:
code
5
7 3 6 12 18
Sorted prices: [3, 6, 7, 12, 18]
First purchase: 3 → cover up to 7 (3,6,7).
Second purchase: 12 → cover up to 16 (12 only).
Third purchase: 18 → cover up to 22 (18).
Output: 3
Python Implementation
python
import sys
def solve() -> None:
data = sys.stdin.read().strip().split()
if not data:
return
n = int(data[0])
prices = list(map(int, data[1:1 + n]))
prices.sort()
i = 0
count = 0
while i < n:
count += 1
limit = prices[i] + 4
i += 1
while i < n and prices[i] <= limit:
i += 1
print(count)
if __name__ == "__main__":
solve()