skip to Main Content

The problem goes like this:

There is a lock of 4-digit PIN. The digits have to be chosen from the digits 0-5 for the key to unlock.

We need to Find all possible combination like 0001, 0002, 0003, 0004, and so on.

3

Answers


  1. Chosen as BEST ANSWER

    Here's how to approach the problem recursively.

    We need to divide the problems into sub problems and find the solution to smallest subproblem we could solve.

    That gives our base case, i.e, for 0 digit pin we'll have ""

    Now lets break the problem into sub problems and attempt to solve that. We try the first digit of the pin with 0, then we only have to solve the problem for the remaining three digits. Same goes if the first digit we try with 1 and so on.

    So let's proceed with this

    function findAllCombo(){    
      var res=[];
      function computeRecursively(newStr="", locks, startIndex=0){
      
         //Base case
         if(startIndex === locks.length){
            res.push(newStr);
            return res;
          }
    
          // The digits have to be choosen form 0-5. So we recursively call for each of the 
          // digits. We can use loop as well.
    
          computeRecursively(newStr+0, locks, startIndex+1);
          computeRecursively(newStr+1, locks, startIndex+1);
          computeRecursively(newStr+2, locks, startIndex+1);
          computeRecursively(newStr+3, locks, startIndex+1);
          computeRecursively(newStr+4, locks, startIndex+1);
          computeRecursively(newStr+5, locks, startIndex+1);
          
          return res;
      
      }
    
      //This defines the lock- place to hold each digit
      var locks = new Array(4);
      return computeRecursively("", locks, 0)
    }
    
    console.log(findAllCombo())


  2. You can create an array for the digits and increment the earlier digits until you find one that was not 9, just like you increment numbers in general:

    let array = [0, 0, 0, 0];
    let output = [];
    do {
        output.push(array.join(""));
        let digitIndex = 3;
        while ((digitIndex >= 0) && (!(array[digitIndex] = (array[digitIndex] + 1) % 6))) digitIndex--;
    } while (array[0] + array[1] + array[2] + array[3]);
    
    console.log(output);
    Login or Signup to reply.
  3. Here is my approach, I’ve probably made this more complicated than it has to be. I’ve concentrated more on using recursion properly (no global variables and the function returns the final result or a call to itself).

    function recursion(digitsCount, range, currentCombo, results = []) {
      if (!currentCombo) {
        currentCombo = new Array(digitsCount).fill(0);
        results.push(currentCombo.join(''));
      }
      const newCombo = new Array(digitsCount);
    
      let flag = false;
      for (let i = digitsCount - 1; i >= 0; i--) {
        if (flag || i === digitsCount - 1 && !(i === 0 && currentCombo[0] === (range - 1))) {
          flag = currentCombo[i] === (range - 1);
          newCombo[i] = (currentCombo[i] + 1) % range;
        } else {
          newCombo[i] = currentCombo[i];
        }
      }
      results = [...results, newCombo.join('')];
      if (newCombo.every((n) => n === (range - 1))) {
        return results;
      }
      return recursion(digitsCount, range, newCombo, results);
    }
    
    
    const range = 6; // from 0 to 5 there are 6 digits
    const digitsCount = 4;
    
    console.log(recursion(digitsCount, range));

    The for loop in there is required (or, at least, another recursive function) if the digitsCount can be variable.

    The main driver of this approach is using modulus (%) and the flag variable to make the counting range based (in this case, 5 based).

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search