Skip to content

Commit d05c60f

Browse files
committed
Solution as on 23-03-2022 06:30 am
1 parent 1b7e335 commit d05c60f

File tree

1 file changed

+45
-0
lines changed

1 file changed

+45
-0
lines changed

0991. Broken Calculator.cpp

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
// 991.✅ Broken Calculator
2+
3+
class Solution
4+
{
5+
public:
6+
int brokenCalc(int startValue, int target)
7+
{
8+
9+
// res for counting number of operation
10+
int res = 0;
11+
12+
while (target > startValue)
13+
{
14+
// if target is odd we will make it even
15+
if (target % 2)
16+
++target;
17+
// if target is even divide by 2
18+
else
19+
target /= 2;
20+
21+
++res;
22+
}
23+
return res + startValue - target;
24+
}
25+
// for github repository link go to my profile.
26+
};
27+
28+
// Another Approach
29+
30+
class Solution
31+
{
32+
public:
33+
int brokenCalc(int startValue, int target)
34+
{
35+
if (startValue >= target)
36+
return startValue - target;
37+
38+
if (target & 1) // if odd
39+
return 1 + brokenCalc(startValue, target + 1);
40+
41+
// if even
42+
return 1 + brokenCalc(startValue, target / 2);
43+
}
44+
// for github repository link go to my profile.
45+
};

0 commit comments

Comments
 (0)