forked from kaushal02/interview-coding-problems
-
Notifications
You must be signed in to change notification settings - Fork 0
/
minCostOddPrimeSum.cpp
54 lines (48 loc) · 1.33 KB
/
minCostOddPrimeSum.cpp
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
/*
Walmart Labs IITG 2018
Following operations are defined:
1. X -> X + 1
2. X -> X - 1
Cost of operation 1 and 2 are cost_inc and cost_dec respectively. Given a number
N, find the minimum cost to reach a number M such that M can be represented as a
sum of two different single-digit odd prime numbers with positive powers.
O(k*logn) where k is very small. For n <= 1E8, there are less than 500 possible M
*/
#include <set>
int minCostOddPrimeSum(int n, int cost_dec, int cost_inc) {
const int N = 1E6; // N represents the upper bound on input n. Adjust.
const int LIM = 2 * N + 10;
std::set<int> st;
for (int p : {3, 5, 7}) {
for (int q : {3, 5, 7}) {
if (p <= q) continue;
// p^i + q^j
int pi = p;
while (pi <= LIM) {
int qj = q;
while (pi + qj <= LIM) {
st.insert(pi + qj);
qj *= q;
}
pi *= p;
}
}
}
int lo = 0, hi;
for (int x : st) {
if (x == n) {
return 0;
}
if (x < n) {
lo = x;
}
else {
hi = x;
break;
}
}
int ans = (hi - n) * cost_inc;
int ans1 = (n - lo) * cost_dec;
if (lo and ans > ans1) ans = ans1;
return ans;
}