Halloween Sale

Halloween Sale

You wish to buy video games from the famous online video game store Mist.

Usually, all games are sold at the same price, p dollars. However, they are planning to have the seasonal Halloween Sale next month in which you can buy games at a cheaper price. Specifically, the first game you buy during the sale will be sold at p dollars, but every subsequent game you buy will be sold at exactly d dollars less than the cost of the previous one you bought. This will continue until the cost becomes less than or equal to m dollars, after which every game you buy will cost m dollars each.

For example, if p = 20, d = 3 and m = 6, then the following are the costs of the first 11 games you buy, in order:

20, 17, 14, 11, 8, 6, 6, 6, 6, 6, 6

You have s dollars in your Mist wallet. How many games can you buy during the Halloween Sale?

Input Format

The first and only line of input contains four space-separated integers p, d, m and s.

Constraints

  • 1 <= m <= p <= 100
  • 1 <= d <= 100
  • 0 <= s <= 104

Output Format

Print a single line containing a single integer denoting the maximum number of games you can buy.

Sample Input 0

1
20 3 6 80

Sample Output 0

1
6

Explanation 0

We have p = 20, d = 3 and m = 6, the same as in the problem statement. We also have s = 80 dollars. We can buy 6 games since they cost 20 + 17 + 14 + 11 + 8 + 6 = 76 dollars. However, we cannot buy a 7th game. Thus, the answer is 6.

Sample Input 1

1
20 3 6 85

Sample Output 1

1
7

Explanation 1

This is the same as the previous case, except this time we have s = 85 dollars. This time, we can buy 7 games since they cost 20 + 17 + 14 + 11 + 8 + 6 + 6 = 82 dollars. However, we cannot buy an 8th game. Thus, the answer is 7.


Solution

1
2
3
4
5
6
7
8
9
10
11
12
13
// Complete the howManyGames function below.
function howManyGames(p, d, m, s) {
// Return the number of games you can buy
let count = 0;

while (s >= p) {
count++;
s = s - p;
p = Math.max(p - d, m);
}

return count;
}
Buy Me A Coffee

Comments

10Days of JS 30Days of Code Algorithm Android Debug Bridge Android Debugging Basic for Web Blog Browsers Chrome으로 Android Debugging 방법 Correctness and the Loop Invariant hackerrank solution in javascript Debug Tools Development Environment in MacOS ES6 Front-End Funny String of Algorithms hackerrank solution in javascript Funny String of Algorithms hackerrank solution in typescript Generator Github Page with Hexo Github Pages HackerRank HackerRank in a String of Algorithms hackerrank solution in javascript HackerRank in a String of Algorithms hackerrank solution in typescript Hexo Hexo Icarus theme Hexo 블로그 만들기 Hexo+Github How Browsers work Insertion Sort - Part 1 hackerrank solution in javascript Insertion Sort - Part 2 hackerrank solution in javascript JS Library JavaScript Level1 Level2 Loops MacOS 개발 환경 설정하기 Mobile web Debugging Node.js Pangrams of Algorithms hackerrank solution in javascript Pangrams of Algorithms hackerrank solution in typescript Problem Solving Programmers Quicksort 1 - Partition hackerrank solution in javascript React RoadMap Running Time of Algorithms hackerrank solution in javascript Safari Debugging Safari Technology Preview Settings Sorting String Strings Strong Password of Algorithms hackerrank solution in javascript TypeScript blog iPhone Safari Debugging 방법 insertion sort 모바일 웹 디버깅 아이폰 사파리를 디버깅하는 방법 안드로이드 디버그 브리지 안드로이드 디버깅하는 방법
Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×