Check if a Number is Sum of Two Perfect Squares in JavaScript

AmitDiwan
Updated on 03-Mar-2021 08:50:55

288 Views

Perfect Square Numbers:A natural number in mathematics is called a perfect square if it can be obtained by multiplying any other natural number into that very number.For instance, 9, 16, 81, 289 are all perfect squares.We are required to write a JavaScript function that takes in a natural number, say num, as the only argument. The function should determine whether there exists two such number m and n such that −(m * m) + (n * n) = numIf there exists such numbers, our function should return true, false otherwise.For example −If the input number is −const num = 389;Then ... Read More

Finding Longest Line of 1s in a Matrix in JavaScript

AmitDiwan
Updated on 03-Mar-2021 08:49:14

205 Views

Suppose, we have a binary matrix (an array of array that contains only 0 or 1) like this −const arr = [    [0, 1, 1, 0],    [0, 1, 1, 0],    [0, 0, 0, 1] ];We are required to write a JavaScript function that takes in one such matrix as the first and the only argument.The task of our function is to find the longest line of consecutive ones in the matrix and return the count of 1s in it. The line could be horizontal, vertical, diagonal or anti-diagonal.For example, for the above array, the output should be ... Read More

Distance of Nearest 0 in Binary Matrix in JavaScript

AmitDiwan
Updated on 03-Mar-2021 08:47:05

225 Views

A binary matrix is an array of arrays containing only 0 or 1. We are required to write a JavaScript function that takes in a binary matrix as the only argument.Our function should create a new matrix containing the same number of rows and columns, and for each element of the original matrix the resulting matrix should contain that element's nearest distance from 0 in the original matrix.We have to keep in mind that while calculating distance it can move either horizontally or vertically and not diagonally. And it's guaranteed that the matrix contains at least one 0.For example −If ... Read More

Reversing Strings with a Twist in JavaScript

AmitDiwan
Updated on 03-Mar-2021 08:45:26

226 Views

We are required to write a JavaScript function that takes in a string str as the first argument and an integer num as the second argument.Our function should reverse the first num characters for every 2 * num characters counting from the start of the string. And if there are less than num characters left, we have to reverse all of them.If there are less than 2 * num but greater than or equal to num characters, then we have to reverse the first num characters and leave the other as original.For example −If the input string and the number ... Read More

Encoding and Decoding Algorithms for Shortening URLs in JavaScript

AmitDiwan
Updated on 03-Mar-2021 08:42:40

854 Views

We often come through services like bit.ly and tinyurl which takes in any url and (usually one bigger in length), performs some encryption algorithm over it and returns a very short url. And similarity when we try to open that tiny url, it again runs some decryption algorithm over it and converts the short url to the original one opens the link for us.We are also required to perform the same task. We are actually required to write two functions −encrypt() --> it will take in the original url and return to us a short unique ur.decrypt() --> it will take in ... Read More

Find Minimum Absolute Difference in a Binary Search Tree using JavaScript

AmitDiwan
Updated on 03-Mar-2021 08:40:44

219 Views

We are required to write a JavaScript function that takes in the root of a BST that holds some numerical data like this −1 \ 3 / 2The function should return the minimum absolute difference between any two nodes of the tree.For example −For the above tree, the output should be −const output = 1;because |1 - 2| = |3 - 2| = 1ExampleThe code for this will be − Live Democlass Node{    constructor(data) {       this.data = data;       this.left = null;       this.right = null;    }; }; class BinarySearchTree{    constructor(){ ... Read More

Find the Third Maximum Number in an Array using JavaScript

AmitDiwan
Updated on 03-Mar-2021 08:36:44

198 Views

We are required to write a JavaScript function that takes in an array of numbers as the first and the only argument.The task of our function is to pick and return the third maximum number from the array. And if the array does not contain any third maximum number then we should simply return the maximum number from the array.For example −If the input array is −const arr = [34, 67, 31, 87, 12, 30, 22];Then the output should be −const output = 34;ExampleThe code for this will be − Live Democonst arr = [34, 67, 31, 87, 12, 30, 22]; ... Read More

Kit Kat Array in JavaScript

AmitDiwan
Updated on 03-Mar-2021 08:34:54

161 Views

We are required to write a JavaScript function that takes in a natural number, num, as the first argument and two natural numbers m and n as the second and third argument. The task of our function is to return an array that contains all natural numbers from 1 to num (including num) in increasing order.But if any number is a multiple of m, we should replace it by 'kit' string, if any number is a multiple of n, we should replace it by 'kat', andif any number is a multiple of both m and n it should be replaced ... Read More

Finding Nth Digit of Natural Numbers Sequence in JavaScript

AmitDiwan
Updated on 03-Mar-2021 08:10:01

591 Views

Natural Number Sequence:1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12...This sequence extended infinitely is known as natural number sequence.We are required to write a JavaScript function that takes in a number, num, as the first and the only argument. The function should find and return the (num)th digit that will appear in this sequence when written, removing the commas and whitespaces.For example −If the input number is −const num = 13;Then the output should be −const output = 1;because '1234567891011' this string has its 13th number as 1ExampleThe code for this will be − Live Democonst num ... Read More

Finding the Sum of Two Numbers Without Using Plus in JavaScript

AmitDiwan
Updated on 03-Mar-2021 08:06:18

927 Views

We are required to write a JavaScript function add() that takes in two numbers m and n. The function should, without using the four basic arithmetic operations add the two numbers taken as input and return the sum.ExampleThe code for this will be − Live Democonst m = 67, n = 33; const add = (x, y) => {    while(y !== 0){       let carry = x & y;       x = x ^ y;       y = carry

Advertisements