Bring number down to 1 in JavaScript

Problem

We need to write a JavaScript function that takes a number and finds the minimum operations to reduce it to 1 using these rules:

  • If the number is even, divide it by 2

  • If the number is odd, add 1 or subtract 1

The function should return the minimum number of operations required.

Example

For input number 7, the output should be 4 operations:

Input: 7
Output: 4

Output Explanation

The optimal path requires 4 steps:

7 ? 8 ? 4 ? 2 ? 1 (4 operations)
or
7 ? 6 ? 3 ? 2 ? 1 (4 operations)

Solution Using Breadth-First Search

We can solve this using BFS to explore all possible paths and find the minimum:

const downToOne = (num = 1) => {
    let min = Number.POSITIVE_INFINITY;
    let stack = [{ num: num, step: 0 }];
    let set = new Set();
    let next;
    let item;
    
    while (stack.length) {
        item = stack.shift();
        
        if (item.num === 1) {
            if (min > item.step) {
                min = item.step;
            }
            continue;
        }
        
        if (set.has(item.num) || item.step >= min) {
            continue;
        }
        
        set.add(item.num);
        next = item.step + 1;
        
        if (item.num % 2 === 0) {
            // Even number: divide by 2
            item.num /= 2;
            stack.push({ num: item.num, step: next });
        } else {
            // Odd number: try both +1 and -1
            stack.push({ num: item.num - 1, step: next });
            stack.push({ num: item.num + 1, step: next });
        }
    }
    
    return min;
};

// Test with example
const num = 7;
console.log(`Minimum operations for ${num}: ${downToOne(num)}`);

// Test with other numbers
console.log(`Minimum operations for 8: ${downToOne(8)}`);
console.log(`Minimum operations for 15: ${downToOne(15)}`);
Minimum operations for 7: 4
Minimum operations for 8: 3
Minimum operations for 15: 5

Optimized Dynamic Programming Solution

For better performance, we can use a more efficient approach:

const minOperations = (num) => {
    if (num === 1) return 0;
    
    let operations = 0;
    
    while (num !== 1) {
        if (num % 2 === 0) {
            num = num / 2;
        } else {
            // For odd numbers, choose the path that leads to fewer operations
            if (num === 3 || (num + 1) % 4 !== 0) {
                num = num - 1;
            } else {
                num = num + 1;
            }
        }
        operations++;
    }
    
    return operations;
};

// Test the optimized solution
console.log(`Optimized - Operations for 7: ${minOperations(7)}`);
console.log(`Optimized - Operations for 8: ${minOperations(8)}`);
console.log(`Optimized - Operations for 15: ${minOperations(15)}`);
Optimized - Operations for 7: 4
Optimized - Operations for 8: 3
Optimized - Operations for 15: 5

How It Works

The BFS solution explores all possible paths by:

  • Using a queue to store states (number and step count)

  • Tracking visited numbers to avoid cycles

  • Finding the minimum steps when reaching 1

The optimized solution uses the strategy that for odd numbers, we generally subtract 1 unless the number is 3 or adding 1 creates a number divisible by 4.

Conclusion

Both solutions effectively find the minimum operations to reduce any number to 1. The BFS approach guarantees the optimal solution, while the optimized version provides better performance for large numbers.

Updated on: 2026-03-15T23:19:00+05:30

129 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements