AmitDiwan

AmitDiwan

8,392 Articles Published

Articles by AmitDiwan

Page 371 of 840

Implementing a Binary Search Tree in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 4K+ Views

A tree is a collection of nodes connected by edges, where each node holds data and references to its children. Binary Search Trees (BST) are a special type of binary tree that maintains a sorted order. What is a Binary Search Tree? A Binary Search Tree is a binary tree where nodes with lesser values are stored on the left, and nodes with higher values are stored on the right. This property makes searching, insertion, and deletion operations efficient. 25 ...

Read More

Subarrays product sum in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 188 Views

We are required to write a JavaScript function that takes in an array of numbers of length N such that N is a positive even integer and divides the array into two sub arrays (say, left and right) containing N/2 elements each. The function should calculate the product of each subarray and then add both the results together. Example If the input array is: const arr = [1, 2, 3, 4, 5, 6] The calculation would be: (1*2*3) + (4*5*6) 6 + 120 126 Using Array.reduce() Method Here's ...

Read More

Array flattening using loops and recursion in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 687 Views

Array flattening converts nested arrays into a single-level array. JavaScript provides multiple approaches including loops, recursion, and built-in methods. Problem Overview Given a nested array with mixed data types including falsy values, we need to flatten it completely: const arr = [[1, 2, 3], [4, 5, [5, false, 6, [5, 8, null]]], [6]]; console.log("Input:", arr); Input: [ [ 1, 2, 3 ], [ 4, 5, [ 5, false, 6, [Array] ] ], [ 6 ] ] Expected output should be a flat array preserving all values including false and null: ...

Read More

Remove the duplicate value from array with images data in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 508 Views

When working with arrays of objects containing image data, you often need to remove duplicates based on a specific property. Here's how to remove duplicate objects based on the 'image' property value. Sample Data Consider an array of image objects where some images appear multiple times: const arr = [{ 'image': "jv2bcutaxrms4i_img.png", 'gallery_image': true }, { 'image': "abs.png", 'gallery_image': true }, { 'image': "acd.png", 'gallery_image': false }, { ...

Read More

Determining rank on basis of marks in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 966 Views

We are required to write a JavaScript function that takes in an array of numbers representing student marks and returns an array of ranks based on their performance. The function should assign rank 1 to the highest marks, rank 2 to the second highest, and so on. Each student's rank corresponds to their position when marks are sorted in descending order. Problem Statement Given an array of marks, we need to determine the rank of each student based on how their marks compare to others in the class. For example, if the input is: ...

Read More

Moving vowels and consonants using JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 335 Views

We are required to write a JavaScript function that takes in a string of English alphabets. Our function should construct a new string where every consonant is pushed forward 9 places through the alphabet, and every vowel is pushed backward 5 places. If they pass 'z', start again at 'a', and if they go before 'a', wrap around to 'z'. Problem Breakdown The transformation rules are: Vowels (a, e, i, o, u): Move backward 5 positions Consonants: Move forward 9 positions Non-alphabetic characters: Keep unchanged Wrapping: Use modulo arithmetic to handle alphabet boundaries Example ...

Read More

Pair of (adjacent) elements of an array whose sum is lowest JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 540 Views

We are required to write a JavaScript function that takes in an array of numbers. The function should return a subarray of two adjacent elements from the original array whose sum is the least amongst all adjacent pairs of the array. If the length of the array is less than 2, we should return boolean false. Example Input For example, if the input array is: const arr = [41, 44, -12, 13, -23, 1, 5, -4, 2, 2]; Here, the sum of pair [-23, 1] is -22 which is the least for any ...

Read More

JavaScript group array - find the sets of numbers that can be traveled to using the edges defined

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 147 Views

Consider the following input and output arrays: const input = ["0:3", "1:3", "4:5", "5:6", "6:8"]; const output = [ [0, 1, 3], [4, 5, 6, 8] ]; Considering each number as a node in a graph, and each pairing x:y as an edge between nodes x and y, we are required to find the sets of numbers that can be traveled to using the edges defined. In graph theory terms, we need to find the distinct connected components within such a graph. For instance, in the above arrays, there ...

Read More

Next Greater Element in Circular Array in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 361 Views

Circular Array An array in which the next element of the last element is the first element of the array is often termed as circular. This concept allows us to treat arrays as if they wrap around, where after the last index, we continue from the first index. 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 a logical concept than physical reality. Problem We need to find the next greater element for each element in a circular array. The Next ...

Read More

Transforming array of numbers to array of alphabets using JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 351 Views

Problem We need to write a JavaScript function that takes an array of numbers and transforms it into a string with four parts separated by hyphens. Each part creates a 4-character "word" from the first two and last two elements of the array under different conditions. The four parts are: Characters from the original array (first, second, second-last, last) Same as above, after sorting the array in ascending order Same as above, after sorting the array in descending order Same as above, after converting to ASCII characters and sorting alphabetically Example Implementation Here's ...

Read More
Showing 3701–3710 of 8,392 articles
« Prev 1 369 370 371 372 373 840 Next »
Advertisements