In this HackerRank Halloween Sale problem, 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 will cost p dollars, and every subsequent game will cost d dollars less than the previous one. This continues until the cost becomes less than or equal to m dollars, after which every game will cost m dollars. How many games can you buy during the Halloween Sale?

HackerRank Halloween Sale problem solution

Problem solution in Python programming.


import sys

def howManyGames(p, d, m, s):
    ans = 0
    while s >= p:
        s -= p
        ans += 1
        p = max(m, p - d)
    return ans

if __name__ == "__main__":
    p, d, m, s = input().strip().split(' ')
    p, d, m, s = [int(p), int(d), int(m), int(s)]
    answer = howManyGames(p, d, m, s)

Problem solution in Java Programming.

import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;

public class Solution {

    static int howManyGames(int p, int d, int m, int s) {
        int priceCurr = p;
        int i=0;
                priceCurr = m;
        return i;

    public static void main(String[] args) {
        Scanner in = new Scanner(;
        int p = in.nextInt();
        int d = in.nextInt();
        int m = in.nextInt();
        int s = in.nextInt();
        int answer = howManyGames(p, d, m, s);

Problem solution in C++ programming.

#include <bits/stdc++.h>

using namespace std;

int howManyGames(int p, int d, int m, int s) {
    int res = 0;
    while (p <= s) {
        res++; s -= p;
        p = max(m, p - d);
    return res;

int main() {
    int p;
    int d;
    int m;
    int s;
    cin >> p >> d >> m >> s;
    int answer = howManyGames(p, d, m, s);
    cout << answer << endl;
    return 0;

Problem solution in C programming.

#include <math.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>
#include <limits.h>
#include <stdbool.h>

int howManyGames(int p, int d, int m, int s) {
    // Return the number of games you can buy
    int count=0,sum=0;
    return count-1;

int main() {
    int p; 
    int d; 
    int m; 
    int s; 
    scanf("%i %i %i %i", &p, &d, &m, &s);
    int answer = howManyGames(p, d, m, s);
    printf("%d\n", answer);
    return 0;

Problem solution in JavaScript programming.


var input_stdin = "";
var input_stdin_array = "";
var input_currentline = 0;

process.stdin.on('data', function (data) {
    input_stdin += data;

process.stdin.on('end', function () {
    input_stdin_array = input_stdin.split("\n");

function readLine() {
    return input_stdin_array[input_currentline++];

/////////////// ignore above this line ////////////////////

function howManyGames(p, d, m, s) {
    let i = 0;
    while (s >= 0) {
        p = p > m ? p : m;
        s = s - p;
        if (p - d < m) {
            p = m;
        } else {
            p = p - d;
    return i - 1;

function main() {
    var p_temp = readLine().split(' ');
    var p = parseInt(p_temp[0]);
    var d = parseInt(p_temp[1]);
    var m = parseInt(p_temp[2]);
    var s = parseInt(p_temp[3]);
    var answer = howManyGames(p, d, m, s);
    process.stdout.write("" + answer + "\n");