-
Notifications
You must be signed in to change notification settings - Fork 3
/
HalloweenSale.java
28 lines (22 loc) · 966 Bytes
/
HalloweenSale.java
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
// https://www.hackerrank.com/challenges/halloween-sale/problem
package implimentation;
import java.util.Scanner;
public class HalloweenSale {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int startingPrice = scanner.nextInt();
int difference = scanner.nextInt();
int threshold = scanner.nextInt();
int money = scanner.nextInt();
System.out.println(maximumGames(startingPrice, difference, threshold, money));
}
private static int maximumGames(int start, int difference, int threshold, int money) {
int games = 1 + (start - threshold) / difference;
int spent = games * (2 * start - (games - 1) * difference) / 2;
if (money >= spent) {
return games + (money - spent) / threshold;
}
int b = 2 * start + difference;
return (int) ((b - Math.sqrt(b * b - 8 * difference * money)) / (2 * difference));
}
}