Skip to content

Commit e957635

Browse files
committed
My Elegant solution based on Arythmetic Progression Formula with the old O(n) code and references
1 parent e191ebf commit e957635

File tree

1 file changed

+114
-0
lines changed

1 file changed

+114
-0
lines changed
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
package hackerRank_JavaProblemSolving;
2+
3+
import java.io.*;
4+
import java.math.*;
5+
import java.security.*;
6+
import java.text.*;
7+
import java.util.*;
8+
import java.util.concurrent.*;
9+
import java.util.regex.*;
10+
11+
// Task description, stored in cash
12+
// https://webcache.googleusercontent.com/search?q=cache:PSCXlPhXVqMJ:https://www.hackerrank.com/challenges/halloween-sale%3Fh_r%3Dprofile+&cd=1&hl=fr&ct=clnk&gl=lu
13+
// Never used solution from GitHub https://github.com/charles-wangkai/hackerrank/blob/master/halloween-sale/Solution.java
14+
// It means I never checked it, plus it has its' dumm cycle. No need O(1) is fine
15+
16+
/* Given task given test - cases
17+
20 3 6 80 :: Output 6
18+
20 3 6 85 :: OUtput 7
19+
20+
*/
21+
22+
/* Unlocked failed test case for 5 hackos
23+
24+
Input (stdin)
25+
Download
26+
16 2 1 9981
27+
Expected Output
28+
Download
29+
9917
30+
31+
*/
32+
33+
public class Problem101_HalloweenSale {
34+
35+
static int howManyGames_FirstTryIneficientComputationalGreedyCode(int p, int d, int m, int s) {
36+
int counter = 0;
37+
int sum = 0;
38+
int price = p;
39+
while ( sum <= s ) {
40+
sum += price;
41+
counter++;
42+
if (price > m) {
43+
price -= d;
44+
} else {
45+
price = m;
46+
}
47+
}
48+
/*if (sum == s) {
49+
return counter;
50+
}*/
51+
return counter-1;
52+
}
53+
54+
// Complete the howManyGames function below.
55+
static int howManyGames(int p, int d, int m, int s) {
56+
if (s < p) { // dummy user input
57+
return 0;
58+
} else if (s == p) { // less dummy input
59+
return 1;
60+
}
61+
// an=p - last element and a1 = first element that we don't know, but we have a limit m
62+
// Using arythm-progression smart math battery of simple formulas https://en.wikipedia.org/wiki/Arithmetic_progression
63+
// Let's figure out in a smart way a1 which is >= m (principal condition)
64+
int n = p/d;
65+
int a1 = p- n*d;
66+
//System.out.println("a1Tilda :"+ a1);
67+
if (a1 < m ) { // it does not mean that m in such way is the lowest member of arythm progression
68+
a1 = m + (p - m) % d;
69+
}
70+
int N = (p - a1) / d + 1;
71+
int sumOfEstimatedArythmProgression = ( p + a1 ) * N / 2;
72+
if ( sumOfEstimatedArythmProgression > s) { // Standard, dummy run because we don't know in terms of formula S(mn) two variable m and am and S(mn) is approximate (arround of s)
73+
// Usually we never go there, because we well estimated our a1 that is actually am in terms of formular
74+
return howManyGames_FirstTryIneficientComputationalGreedyCode(p, d, m, s);
75+
} else { // The rest
76+
int restOFS = s - sumOfEstimatedArythmProgression;
77+
if (restOFS < d) { // perfect match with arythm-progression
78+
return N;
79+
} else { // restOFS >= d
80+
return (N + (restOFS / m));
81+
}
82+
}
83+
} // end of howManyGames
84+
85+
private static final Scanner scanner = new Scanner(System.in);
86+
87+
public static void main(String[] args) throws IOException {
88+
BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(System.getenv("OUTPUT_PATH")));
89+
90+
String[] pdms = scanner.nextLine().split(" ");
91+
92+
int p = Integer.parseInt(pdms[0]);
93+
94+
int d = Integer.parseInt(pdms[1]);
95+
96+
int m = Integer.parseInt(pdms[2]);
97+
98+
int s = Integer.parseInt(pdms[3]);
99+
100+
int answer = howManyGames(p, d, m, s);
101+
102+
bufferedWriter.write(String.valueOf(answer));
103+
bufferedWriter.newLine();
104+
105+
bufferedWriter.close();
106+
107+
scanner.close();
108+
109+
// Debugging reasons
110+
//System.out.println(howManyGames(16, 2, 1, 9981));
111+
//System.out.println(howManyGames_FirstTryIneficientComputationalGreedyCode(16, 2, 1, 9981));
112+
113+
} // main
114+
}

0 commit comments

Comments
 (0)