aboutsummaryrefslogtreecommitdiff
path: root/broken_calculator.c
blob: 9afbe7c168e70d5686cf26085f20a612866db7ab (plain)
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
// 991. Broken Calculator
#include <stdio.h>
#include <stdlib.h>

/*
 * there is a broken calculator that has the integer 'start_value' on its
 * display initially. in one operation, you can
 * - multiply thenumber on display by 2
 * - subtract 1 from number on display
 * given two integers 'start_value' and 'target', return the minimum number of
 * operations needed to display 'target' on the calculator
 */

int brokenCalc(int start_value, int target) {
  int ans = 0;
  while (target > start_value) {
    target = !(target % 2) ? target / 2 : target + 1;
    ++ans;
  }
  return ans + start_value - target;
}

int main() {
  printf("%d\n", brokenCalc(2, 3));  // expect: 2
  printf("%d\n", brokenCalc(5, 8));  // expect: 2
  printf("%d\n", brokenCalc(3, 10)); // expect: 3
}