A toy shop has a unique way of selling their toys to children. As the shop opens the toys prices keeps increasing for every sale he makes. After some time the shopkeeper prefers to lower the price every sale he makes. For example , say the starting prices of the toy is Rs. 10. He decides to increase the price by 5 for 5 first entries and reduce the price by 5 there after. The list would be 10 15 20 25 30 25 20 15 10 5 0. Given this kind of pricing, every day he wants to find what is the maximum price a child had paid for the toy. Write a program to help the shop keep find the toys price in less than O(n) time. You will be given the array. The number of entries for which the shopkeeper decides to increase is kept as a secret.
int max(int *arr) {
int n = sizeof(arr) / sizeof(int);
int m = null;
for (int i = 0; i < n; i++) {
if (m == null || m < arr[i]) {
m = arr[i];
}
}
return m;
}
Comments
Leave a comment