In this post, we will solve HackerRank Halloween Sale Problem Solution.
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 & 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?
Example
p = 20 m = 6 8 = 70.
d = 3
The following are the costs of the first 11, in order:
20, 17, 14, 11, 8, 6, 6, 6, 6, 6, 6
Start at p = 20 units cost, reduce that by d = 3 units each iteration until reaching a minimum possible price, m = 6. Starting with $ = 70 units of currency in your Mist wallet, you can buy 5 games: 20 +17 +14 +11+ 8 = 70.
Function Description
Complete the howManyGames function in the editor below.
howManyGames has the following parameters:
- int p: the price of the first game
- int d: the discount from the previous game price
- int m: the minimum cost of a game
- int s: the starting budget
Input Format
The first and only line of input contains four space-separated integers p, d. m and 8.
Sample Input 0
20 3 6 80
Sample Output 0
6
Explanation 0
Assumptions other than starting funds, 8, match the example in the problem statement. With a budget of 80, you can buy 6 games at a cost of 20+17 +14 +11 +8 +6 = 76. A 7th game for an additional 6 units exceeds the budget.
Sample Input 1
20 3 6 85
Sample Output 1
7
Explanation 1
This is the same as the previous case, except this time the starting budget s = 85 units of currency. This time, you can buy 7 games since they cost 20+17 +14 +11+8+6+6=82. An additional game at 6 units will exceed the budget.

Halloween Sale C Solution
#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) {
int c=0;
while(s>0)
{
s=s-p;
p=p-d;
if(p<=m)
p=m;
if(s<0)
break;
c++;
}
return c;
}
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;
}
Halloween Sale C++ Solution
#include <bits/stdc++.h>
#ifndef ONLINE_JUDGE
#define gc getchar
#define pc putchar
#else
#define gc getchar_unlocked
#define pc putchar_unlocked
#endif
using namespace std;
#define vi vector<int>
#define si set<int>
#define vs vector<string>
#define pii pair<int,int>
#define vpi vector<pii>
#define pri priority_queue<int>
#define rev_pri priority_queue<int,vector<int>,greater<int> >
#define mpi map<int,int>
#define i64 long long int
#define endl '\n'
#define pi acos(-1)
#define all(v) v.begin(),v.end()
#define pb push_back
#define mp make_pair
#define mod 1000000007
#define inf INT_MAX/2
#define infll LLONG_MAX/3
#define For(i,n) for(int i=0;i<n;i++)
#define Fre(i,a,b) for(int i = a; i < b; i++)
#define sf(n) scanf("%d", &n)
#define sff(a,b) scanf("%d %d", &a, &b)
#define sfff(a,b,c) scanf("%d %d %d", &a, &b, &c)
#define pfn(n) printf("%d\n", n)
#define pfs(n) printf("%d ", n)
#define eps 1e-8
#define ff first
#define ss second
#define mem(a,b) memset(a,b,sizeof(a))
#define READ freopen("in.txt", "r", stdin)
#define WRITE freopen("out.txt", "w", stdout)
#define sz size()
#define dbg(i) printf("yo %d\n", i)
#define foreach(i,c) for(__typeof((c).begin()) i = (c).begin(); i != (c).end(); i++)
#define sqr(a) (a) * (a)
#define clr clear()
#define CASE(a) printf("Case %d: ",a)
//int dx[] = {0,1,0,-1,1,1,-1,-1};
//int dy[] = {1,0,-1,0,1,-1,-1,1};
//i64 gcd(i64 a,i64 b){if(!b)return a;return gcd(b,a%b);}
//inline void fastRead(int *a){register char c=0;while(c<33)c=gc();*a=0;while(c>33){*a=*a*10+c-'0';c=gc();}}
//inline void fastWrite(int a){char snum[20];int i=0;do{snum[i++]=a%10+48;a=a/10;}while(a!=0);i=i-1;while(i>=0)pc(snum[i--]);pc('\n');}
//i64 bigmod(i64 num,i64 n){if(n==0)return 1;i64 x=bigmod(num,n/2);x=x*x%mod;if(n%2==1)x=x*num%mod;return x;}
//i64 modinverse(i64 num){return bigmod(num,mod-2)%mod;}
//i64 po(i64 a,i64 b){i64 ans=1;while(b--)ans *= a;return ans;}
//i64 ncr(i64 n,i64 r){if(n==r)return 1;if(r==1)return n;if(dp[n][r]!=-1)return dp[n][r];return dp[n][r]=ncr(n-1,r)+ncr(n-1,r-1);}
// bit manipulations
//bool checkbit(int mask,int bit){return mask & (1<<bit);}
//int setbit(int mask,int bit){ return mask (1<<bit) ; }
//int clearbit(int mask,int bit){return mask & ~(1<<bit);}
//int togglebit(int mask,int bit){return mask ^ (1<<bit);}
int main()
{
int p,d,m,s;
cin >> p >> d >> m >> s;
int now = p;
int ans = 0;
while(1)
{
if(s<now)
break;
// cout << now << endl;
ans++;
s -= now;
now -= d;
if(now<m)
now = m;
}
cout << ans << endl;
return 0;
}
Halloween Sale C Sharp Solution
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
class Solution {
static int howManyGames(int p, int d, int m, int s) {
int res=0;
int x=p;
while(s>=0)
{
s-=x;
x-=d;
if(x<m)
x=m;
res++;
}
return res-1;
}
static void Main(String[] args) {
string[] tokens_p = Console.ReadLine().Split(' ');
int p = Convert.ToInt32(tokens_p[0]);
int d = Convert.ToInt32(tokens_p[1]);
int m = Convert.ToInt32(tokens_p[2]);
int s = Convert.ToInt32(tokens_p[3]);
int answer = howManyGames(p, d, m, s);
Console.WriteLine(answer);
}
}
Halloween Sale Java Solution
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 nextPrice = p;
int result = 0;
while(true) {
if (s<nextPrice) {
break;
}
result++;
s-=nextPrice;
nextPrice = nextPrice - d;
if (nextPrice < m) {
nextPrice = m;
}
}
return result;
}
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();
}
}
Halloween Sale JavaScript Solution
function howManyGames(p, d, m, s) {
let totalCost = p;
let gameCount = 0;
while(totalCost <= s){
p = p - d;
if(p <= m){
totalCost = totalCost + m;
gameCount++;
}else{
totalCost = totalCost + p;
gameCount++;
}
}
return gameCount;
}
Halloween Sale Python Solution
#!/bin/python3
import sys
def howManyGames(p, d, m, s):
# Return the number of games you can buy
tot = 0
i=0
while tot <= s:
tot += p
p-=d
i+=1
if p < m:
p=m
return i-1
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)
other Solutions