Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class FewestOperationsToGetTo1 {
- public static void main(String[] args) {
- int n = 15;
- System.out.println(operations(n));
- }
- private static int operations(int n) {
- if (n == 0) {
- return 1;
- }
- if (n == 1) {
- return 0;
- }
- if (n % 2 == 0) {
- return 1 + operations(n/2);
- }
- int first = 1 + operations(n - 1);
- int second = 1 + operations(n + 1);
- return Math.min(first, second);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment