```
public int integerReplacement(int n) {
if(n == 1) return 0;
return integerReplacementRecursive(n);
}
public int integerReplacementRecursive(long n){
if(n == 1) return 0;
if(n % 2 == 0)
return integerReplacementRecursive( n / 2 ) + 1 ;
else
return Math.min(integerReplacementRecursive(n + 1) + 1, integerReplacementRecursive(n - 1) + 1);
}
}
```