3
int leastOpsExpressTarget(int x, int target) {5
// if target is less then x then we can only have values like either6
// x - (x/x)-(x/x).... or (x/x)+(x/x)+(x/x)...8
return min((x - target) * 2, target * 2 - 1);11
if (x == target) return 0;13
long long int sum = x;16
// greedly get to the closest number such that x>target and then perform17
// either (x/x)+(x/x)+...+(x/x) or18
// x-(x/x)-(x/x)... etc20
while (sum < target) {25
// at this point sum is either greater than target or equal to target26
if (sum == target) return ans;28
// Now we have two choice to reduce the number either we make new number29
// ---> target = sum-target (but this can only work if target>sum-target)31
int op1 = INT_MAX, op2 = INT_MAX;32
if (sum - target < target) {33
op1 = leastOpsExpressTarget(x, sum - target) + ans;35
op2 = leastOpsExpressTarget(x, target - sum / x) + ans - 1;37
return min(op1, op2) + 1;