-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMinimumMultiplicationsWith_2_3_7.cpp
More file actions
126 lines (105 loc) · 2.31 KB
/
Copy pathMinimumMultiplicationsWith_2_3_7.cpp
File metadata and controls
126 lines (105 loc) · 2.31 KB
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#include <iostream>
#include <vector>
// QUESTION: https://www.geeksforgeeks.org/minimum-multiplications-with-2-3-7-to-make-two-numbers-equal/
/**
* If you need to multiply a number with 2, 3, or 7 to make it equal to another number,
* then, it must be a multiple of these numbers as well. That is:
*
* num1 = x * 2^x1 * 3^x2 * 7^x3
* num2 = y * 2^y1 * 3^y2 * 7^y3
*
* Only if x and y are equal, multiplying it with 2, 3, or 7 will translate to the other number.
* And, the minimum number os operations will be the sum of the absoluate difference of the powers
* Note that, x and y cannot be a multiple of 2, 3, or 7 because, if it were, we would pulled it out
* and added it to the exponent.
* So, we need to filter the number by dividing it by 2, 3, and 7 and return the result
*/
class ExponentCalculator
{
private:
std::vector<int> exps;
int remaining;
public:
ExponentCalculator(int number)
{
int count = 0;
// Test with 2
if (number % 2 == 0)
{
while (number % 2 == 0)
{
number /= 2;
count++;
}
exps.push_back(count);
} else {
exps.push_back(0);
}
count = 0;
// Test with 7
if (number % 7 == 0)
{
while (number % 7 == 0)
{
number /= 7;
count++;
}
exps.push_back(count);
} else {
exps.push_back(0);
}
count = 0;
// Test with 3
if (number % 3 == 0)
{
while (number % 3 == 0)
{
number /= 3;
count++;
}
exps.push_back(count);
} else {
exps.push_back(0);
}
remaining = number;
}
int getremainder() const
{
return remaining;
}
std::vector<int> getexponents()
{
return exps;
}
};
class MinimumMultiplicationCalculator
{
public:
int compute(int x, int y)
{
int result = 0;
ExponentCalculator obj1(x);
ExponentCalculator obj2(y);
if (obj1.getremainder() == obj2.getremainder() && obj1.getremainder() != 1)
{
std::vector<int> exps1 = obj1.getexponents();
std::vector<int> exps2 = obj2.getexponents();
for (size_t i = 0; i < 3; ++i)
{
result += std::abs(exps1[i] - exps2[i]);
}
}
return result;
}
};
int main()
{
MinimumMultiplicationCalculator obj;
int result = obj.compute(7, 2);
if (result == 0) {
std::cout << " The numbers cannot be made equal" << std::endl;
} else {
std::cout << " Minimum number of operatons is " << result << std::endl;
}
return 0;
}