Posts

Showing posts from February, 2021

Dutch national flag problem in Javascript

Image
Dutch national flag problem and solution in Javascript Problem statement:   The Dutch national flag (DNF) problem is one of the most popular programming problems proposed by Edsger Dijkstra. The flag of the Netherlands consists of three colors: white, red, and blue. The task is to randomly arrange balls of white, red, and blue such that balls of the same color are placed together. Now, let's consider an array with 3 distinct values say 0, 1 and 2. We won't be using any sort method and we need to sort this array in 0(n). Input Array :  let   arr  = [ 0 ,  2 ,  1 ,  0 ,  1 ,  2 ,  0 ,  2 ]; Expected Output: [ 0, 0, 0, 1, 1, 2, 2, 2 ] Solution Approach : When we see expected output, we can clearly see that sorted array is divided into 3 sections having values 0 , 1 and 2. So, let's divide the array in 3 sections: a) from 0th index to left boundary b) from left boundary to right boundary c) from right boundary to last index. Now we will create 2 pointers : left (starting from 0

Javascript Codewars Problem: "Highest Rank Number in an Array"

  Codewars   Problem and solution in Javascript Objective:   In this challenge, we will solve the “ Highest Rank Number in an Array ”   Codewars  Kata  using  javascript .   Problem statement:   Complete the method which returns the number which is most frequent in the given input array. If there is a tie for most frequent number, return the largest number among them.   Examples:   Input  :  [12, 10, 8, 12, 7, 6, 4, 10, 12] Output  :  12 Input  :  [12, 10, 8, 12, 7, 6, 4, 10, 12, 10] Output :   12 Input  : [12, 10, 8, 8, 3, 3, 3, 3, 2, 4, 10, 12, 10] Output  :  3 Solution Approach :  we will create a hash-map/object from the given array and store the array elements as key of object and number of element's occurrence as value of that key in object.  For understanding hash-map, refer the blog  post . Once done with the object, we will traverse through the object to check which key has highest value and in case of equal values we need to check whose key is higher and accordingly retur

Create Telegram Bot using Nodejs in 10 minutes

Have you ever thought of creating a telegram bot for sending some useful information, news, alerts, images or even reminders, but didn't know where to start from. Well, in this tutorial we will create a Telegram BOT and will send a chat message from the BOT to user, and NodeJS will be used for the coding part. The best part is i will share the exact simple steps that you need to perform and it won't take more than 10 mins to setup your first telegram bot and to send first message to the user from the NodeJS code. Step 1: Create your first Telegram Bot Telegram has a bot to create the bots, just search the BotFather  and then follow the instructions to create a BOT. Use the /newbot command to create a new bot. The BotFather will ask you for a name and username, then generate an authorization token for your new bot. The name of your bot is displayed in contact details and elsewhere. The Username is a short name, to be used in mentions and t.me links. The token is a string along t