-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathMinimum Purchase Count - Free Offer
72 lines (61 loc) · 2.37 KB
/
Minimum Purchase Count - Free Offer
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
A shop sells N items. Due to a festival offer, a customer if he purchases an item which is of price P, he will also get items whose price is either P+1 or P+2 or P+3 or P+4. Given the price of N items, the program must print the minimum number of times the customer must purchase to get one count purchased in all the available items making use of this offer.
Input Format:
The first line will contain N.
The second line will contain the prices of the N items, with the values separated by a space.
Output Format:
The first line will contain the minimum purchase count.
Boundary Conditions:
3 <= N <= 10000
The price of the items will be from 1 to 1000.
Example Input/Output 1:
Input:
4
1 2 5 5
Output:
1
Explanation:
If the customer buys the item with price as 1, then he will get the three items priced 2,5,5 also free as they are within the range P+4.
Hence the customer needs to buy only once.
Example Input/Output 2:
Input:
11
1 2 5 7 19 20 12 11 9 15 19
Output:
4
Explanation:
When the customer buys item with price as 1, he will get items with price as 2 and 5 free.
When he buys item with price 7, he will get items with price as 9 and 11 free.
When he buys item with price 12, he will get items with price as 15 free.
He then buys item with price 19 and gets the item with the price as 20 free.
Program coding:
#include<stdio.h>
int main(){
int n;
scanf("%d",&n);
int a[n],i,j,k,cnt=0;
for(i=0;i<n;i++)
scanf("%d",&a[i]);
for(i=0;i<n;i++){ ///sort the array
for(j=0;j<n;j++){
if(a[i]<a[j]){
int temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
}
j=1;
for(i=0;i<n;){ ///here check the P with bonus using j (increment j to chek nearby price)
for(k=0;k<5;k++){ /// current price + 1 or 2 or 3 or 4 written as [(current - next) < 5]
if((a[j]-a[i])<5)
j++;
else{
i=j;
cnt++; ///increment the count
break;
}
}
}
printf("%d",cnt);
return 0;
}