Skip to content
Programming101
Programmingoneonone

Learn everything about programming

  • Home
  • CS Subjects
    • IoT – Internet of Things
    • Digital Communication
    • Human Values
  • Programming Tutorials
    • C Programming
    • Data structures and Algorithms
    • 100+ Java Programs
    • 100+ C Programs
  • HackerRank Solutions
    • HackerRank Algorithms Solutions
    • HackerRank C problems solutions
    • HackerRank C++ problems solutions
    • HackerRank Java problems solutions
    • HackerRank Python problems solutions
Programming101
Programmingoneonone

Learn everything about programming

HackerRank Halloween Sale problem solution

YASH PAL, 31 July 2024

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.

#!/bin/python3

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)
    print(answer)

Problem solution in Java Programming.

import java.io.*;
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;
        while(s>=priceCurr){
            s=s-priceCurr;
            if(priceCurr>=m+d){
                priceCurr=priceCurr-d;
            }else{
                priceCurr = m;
            }
           
            i++;
        }
        return i;
    }

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

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;
    p=p+d;
    while(sum<=s){
        
        p=p-d;
        if(p<m)
            p=m;
        sum+=p;
        count++;
    }
    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("%dn", answer);
    return 0;
}

Problem solution in JavaScript programming.

process.stdin.resume();
process.stdin.setEncoding('ascii');

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");
    main();    
});

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;
        }
        i++;
    }
    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");

}

Algorithms coding problems solutions

Post navigation

Previous post
Next post
  • Automating Image Format Conversion with Python: A Complete Guide
  • HackerRank Separate the Numbers solution
  • How AI Is Revolutionizing Personalized Learning in Schools
  • GTA 5 is the Game of the Year for 2024 and 2025
  • Hackerrank Day 5 loops 30 days of code solution
How to download udemy paid courses for free

Pages

  • About US
  • Contact US
  • Privacy Policy

Programing Practice

  • C Programs
  • java Programs

HackerRank Solutions

  • C
  • C++
  • Java
  • Python
  • Algorithm

Other

  • Leetcode Solutions
  • Interview Preparation

Programming Tutorials

  • DSA
  • C

CS Subjects

  • Digital Communication
  • Human Values
  • Internet Of Things
  • YouTube
  • LinkedIn
  • Facebook
  • Pinterest
  • Instagram
©2025 Programmingoneonone | WordPress Theme by SuperbThemes