វិធីសាស្រ្តគួរឱ្យចាប់អារម្មណ៍ក្នុងការបង្កើតលេខគោលពីរពីលេខ 1 ដល់អិន


កម្រិតលំបាក មធ្យម
សួរញឹកញាប់ ក្រុមហ៊ុន Amazon បេឡាបាបា Mahindra Comviva ServiceNow ។ វីកឃើរ
ការស្វែងរកដំបូង ជួរ

សេចក្តីថ្លែងការណ៍បញ្ហា។

The problem “An Interesting Method to generate Binary Numbers from XNUMX to n” states that you are given a number n, print all the numbers from XNUMX to n in binary form.

ឧទាហរណ៍

3
1 10 11

 

6
1 10 11 100 101 110

ក្បួនដោះស្រាយ

The generation of binary numbers from XNUMX to n can be seen as a binary tree, where XNUMX is the root of the tree and every node has XNUMX children, left child is obtained by appending ‘XNUMX’ at the end of the number while right child is obtained by appending ‘XNUMX’ at the end of the number. See the image below.

វិធីសាស្រ្តគួរឱ្យចាប់អារម្មណ៍ក្នុងការបង្កើតលេខគោលពីរពីលេខ 1 ដល់អិន

To generate first n binary numbers, do a level order traversal នៃ ដើមឈើ and print first n nodes.

  1. Create a queue of string named q. Initialize a variable total as XNUMX.
  2. Push “XNUMX” to the queue, that is root of the tree.
  3. While the total is less than n, repeat step XNUMX and XNUMX.
  4. Pop out an element from ជួរ, print it and push its left child(element + “XNUMX”) and right child(element + “XNUMX”) to the queue.
  5. Increment total by XNUMX.

ការពន្យល់

Consider the example that we have to generate binary number from XNUMX to XNUMX.

First we create the tree as shown in image above, the root of the tree is XNUMX, and for every node its left child is (value of node + “XNUMX”) and right child is (value of node + “XNUMX”).

In the tree we can see that the root corresponds to the binary representation of decimal number XNUMX. The left child of root is the binary representation of XNUMX, the right child of root is the binary representation of XNUMX. Similarly, the left node of “XNUMX” is the binary representation of XNUMX, and right node of “XNUMX” is the binary representation of XNUMX and so on.

So to print the binary representation of numbers from XNUMX to XNUMX, all we have to do is to print the first XNUMX nodes of the tree, which can be done using a queue by traversing the tree in level order.

Hence, output is
1 10 11 100 101 110

លេខកូដ

កូដចាវ៉ាដល់វិធីសាស្រ្តគួរឱ្យចាប់អារម្មណ៍មួយដើម្បីបង្កើតលេខគោលពីរពីលេខ ១ ដល់លេខ n

import java.util.LinkedList;
import java.util.Queue;

class AnInterestingMethodToGenerateBinaryNumbersFrom1ToN {
    private static void generateBinaryNumbers(int n) {
        if (n == 0) {
            return;
        }

        // create a queue of strings
        Queue<String> q = new LinkedList<>();
        // push the root to the queue
        q.add("1");

        // initialize total as 0
        int total = 0;

        // while total is less than n
        while (total < n) {
            // remove an element from queue and print it
            String curr = q.poll();
            System.out.print(curr + " ");
            // add the left and right child of curr to the queue
            q.add(curr + "0");
            q.add(curr + "1");
            // increment total
            total++;
        }

        System.out.println();
    }

    public static void main(String[] args) {
        int n1 = 3;
        generateBinaryNumbers(n1);

        int n2 = 6;
        generateBinaryNumbers(n2);
    }
}
1 10 11 
1 10 11 100 101 110

C++ Code to An Interesting Method to generate Binary Numbers from XNUMX to n

#include<bits/stdc++.h> 
using namespace std; 

void generateBinaryNumbers(int n) {
    if (n == 0) {
        return;
    }
    
    // create a queue of strings
    queue<string> q;
    // push the root to the queue
    q.push("1");
    
    // initialize total as 0
    int total = 0;
    
    // while total is less than n
    while (total < n) {
        // remove an element from queue and print it
        string curr = q.front();
        q.pop();
        cout<<curr<<" ";
        // add the left and right child of curr to the queue
        q.push(curr + "0");
        q.push(curr + "1");
        // increment total
        total++;
    }
    
    cout<<endl;
}

int main() {
    int n1 = 3;
    generateBinaryNumbers(n1);
    
    int n2 = 6;
    generateBinaryNumbers(n2);
    
    return 0;
}
1 10 11 
1 10 11 100 101 110

ការវិភាគស្មុគស្មាញ

ស្មុគស្មាញពេលវេលា

O (N), since we are only traversing the elements until we reach our given target element. Thus the algorithm is linear in time.

ភាពស្មុគស្មាញនៃលំហ

O (N), as we have traversed through the elements which are less than the target element. We have been pushing these elements into the queue thus the space complexity is also linear.