Found 6710 Articles for Javascript

Finding Mode in a Binary Search Tree in JavaScript

AmitDiwan
Updated on 04-Mar-2021 12:54:42

318 Views

Mode:Mode of a set of data is simply the number that occurs for most number of times in that set of data. For instance, 3 is the mode of dataset 2, 3, 1, 3, 4, 2, 3, 1 as it occurs for most number of times.Binary Search TreeA tree DS is a valid Binary Search Tree if it fulfils the following conditions −The left subtree of a node contains only nodes with keys less than or equal to the node's key.The right subtree of a node contains only nodes with keys greater than or equal to the node's key.Both the ... Read More

Next Greater Element in Circular Array in JavaScript

AmitDiwan
Updated on 03-Mar-2021 07:38:24

295 Views

Circular ArrayAn array in which the next element of the last element is the first element of the array is often termed as circular.Obviously, there exists no such mechanism to store data like this, data will still be stored in continuous memory blocks and circular arrays are more like an idea than reality.ProblemWe are required to write a JavaScript function that takes in a circular array of Integers, arr, as the first and the only argument.The function should then construct and return an array that contains the next greater element for each corresponding element of the original array. The Next ... Read More

Determining rank on basis of marks in JavaScript

AmitDiwan
Updated on 03-Mar-2021 07:41:00

874 Views

ProblemWe are required to write a JavaScript function that takes in an array, arr, of numbers as the only argument.The array basically consists of the marks scored by some students, based on the array of marks, our function should prepare and return an array of ranks which should contain the rank of corresponding students on the basis of how high their marks are in the marks array arr.For instance, for the highest entry in array arr, the corresponding entry in output array should be 1, 2 for second highest and so on.For example, if the input to the function is ... Read More

Implementing a Binary Search Tree in JavaScript

AmitDiwan
Updated on 03-Mar-2021 07:43:09

4K+ Views

Tree Data StructureA tree is a collection of nodes connected by some edges. Conventionally, each node of a tree holds some data and reference to its children.Binary Search TreeBinary Search tree is a binary tree in which nodes that have lesser value are stored on the left while the nodes with a higher value are stored at the right.For instance, visual representation of a valid BST is − 25 / \   20   36  / \   / \ 10 22 30 40Let’s now implement our very own Binary Search ... Read More

Finding the longest substring uncommon in array in JavaScript

AmitDiwan
Updated on 03-Mar-2021 07:44:53

205 Views

SubsequenceFor the purpose of this problem, we define a subsequence as a sequence that can be derived from one sequence by deleting some characters without changing the order of the remaining elements. Any string is a subsequence of itself and an empty string is a subsequence of any string.ProblemWe are required to write a JavaScript function that takes in an array of strings as the only argument. Our function needs to find the length of the longest uncommon subsequence among them.By longest uncommon subsequence we mean, longest subsequence of one of the strings in the array and this subsequence should ... Read More

Subarray sum with at least two elements in JavaScript

AmitDiwan
Updated on 03-Mar-2021 07:47:25

194 Views

ProblemWe are required to write a JavaScript function that takes in an array of Integers, arr, as the first argument and a single Integer, target as the second and first argument. Our function should check whether there exists a continuous subarray of size at least 2 that sums up to the multiple of k, that is, sums up to n*k where n can be any integer.We return true if it exists, false otherwise.For example, if the input to the function is −const arr = [23, 2, 6, 4, 7]; const target = 6;Then the output should be −const output = ... Read More

Forming the longest word in JavaScript

AmitDiwan
Updated on 03-Mar-2021 07:49:42

291 Views

ProblemWe are required to write a JavaScript function that takes in a random English alphabet string, str, as the first argument and an array of strings, arr, as the second argument.The task of our function is to try deleting some characters from the string str and check which longest word can be formed that exists in the array arr as well. Our function should return the longest possible string.If there exists no such string, we should return an empty string.For example, if the input to the function is −const str = 'sdgfdfghdjh'; const arr = ['sdf', 'fghj', 'gfdfg', 'absc', 'a', ... Read More

Contiguous subarray with 0 and 1 in JavaScript

AmitDiwan
Updated on 03-Mar-2021 07:58:04

295 Views

Problem:We are required to write a JavaScript function that takes in a binary array, arr, (an array that only consists of 0 or 1). Our function should return the length of the contiguous subarray from the array that consists of the same number of 1 and 0.For example, if the input to the function is −const arr = [1, 0, 0, 1, 0, 1, 0, 0];Then the output should be −const output = 6;Output ExplanationThe first 6 elements of the array are 1, 0, 0, 1, 0, 1 (three 1s and three 0s)ExampleThe code for this will be − Live Democonst ... Read More

Beautiful Arrangement of Numbers in JavaScript

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

375 Views

Beautiful Arrangement:Suppose we have num integers from 1 to num. We define a beautiful arrangement as an array that is constructed by these num numbers successfully if one of the following is true for the ith position (1 ≤ i ≤ N) in this array −The number at the ith position is divisible by i.i is divisible by the number at the ith position.ProblemWe are required to write a JavaScript function that takes in a number, num, and returns the count of beautiful arrangements we can construct for num.For example, if the input to the function is −const input = ... Read More

Finding the sum of two numbers without using '+', '-', '/', '*' in JavaScript

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

928 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