File tree Expand file tree Collapse file tree 1 file changed +45
-0
lines changed Expand file tree Collapse file tree 1 file changed +45
-0
lines changed Original file line number Diff line number Diff line change 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+ };
You can’t perform that action at this time.
0 commit comments