Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Articles by AmitDiwan
Page 356 of 840
Longest path in 2-D that contains increasing sequence in JavaScript
In JavaScript, finding the longest path in a 2-D array that contains an increasing sequence is a classic dynamic programming problem that can be solved efficiently using memoized depth-first search. Increasing Sequence A sequence of numbers in which each succeeding element is greater than the preceding element forms an increasing sequence. For instance: 4, 6, 8, 9, 11, 14 is an increasing sequence 1, 2, 3, 4, 5 is also an increasing sequence Problem Statement We need to write a JavaScript function that takes a 2-D array of numbers and returns the ...
Read MoreIsosceles triangles with nearest perimeter using JavaScript
Almost Isosceles Triangle An Almost Isosceles Integer Triangle is a triangle where all side lengths are integers and two sides are almost equal, with their absolute difference being exactly 1 unit of length. Problem Statement We need to write a JavaScript function that takes a number representing the desired perimeter of a triangle. The function should find an almost isosceles triangle whose perimeter is nearest to the input perimeter. For example, if the desired perimeter is 500, the almost isosceles triangle with the nearest perimeter will be [167, 166, 167] with perimeter 500. Understanding Almost ...
Read MoreFinding the only out of sequence number from an array using JavaScript
Problem We are required to write a JavaScript function that takes in an array of numbers. The array is sorted in ascending order and only one element in the array is out of order. Our function should find and return that element. Approach The solution works by checking each element against its neighbors. When we find an element that is greater than the next element AND the next element is also greater than the element after it, we've found our out-of-sequence number. Example Following is the code: const arr = [1, 2, 3, 4, 17, 5, ...
Read MoreProduct of numbers present in a nested array in JavaScript
We are required to write a JavaScript function that takes in an array of nested arrays of Numbers and some falsy values (including 0) and some strings as well and the function should return the product of number values present in the nested array. If the array contains some 0s, we should ignore them as well. Example The code for this will be − const arr = [ 1, 2, null, [ 2, 5, null, undefined, false, 5, [ ...
Read MoreHow to find inside an array of objects the object that holds the highest value in JavaScript?
Finding the object with the highest value in an array of objects is a common task in JavaScript. This example demonstrates how to find the student with the highest grade from an array of student objects. Sample Data Let's start with an array of student objects, where each student has a name and an array of grades: const arr = [ { name: "Student 1", grades: [ 65, 61, 67, 70 ] ...
Read MoreElements that appear twice in array in JavaScript
We are required to write a JavaScript function that takes in an array of literal values. Our function should pick all those values from the array that appear exactly twice in the array and return a new array of those elements. Basic Approach Using Helper Function The first approach uses a helper function to count occurrences of each element: const arr = [0, 1, 2, 2, 3, 3, 5]; const findAppearances = (arr, num) => { let count = 0; for(let i = 0; i < arr.length; ...
Read MoreFinding the common streak in two arrays in JavaScript
We are required to write a JavaScript function that takes in two arrays of literals, let's call them arr1 and arr2. The function should find the longest common streak of literals in the arrays. The function should finally return an array of those literals. For example − If the input arrays are − const arr1 = ['a', 'b', 'c', 'd', 'e']; const arr2 = ['k', 'j', 'b', 'c', 'd', 'w']; Then the output array should be − ['b', 'c', 'd'] Algorithm Overview This problem uses dynamic programming to ...
Read MoreCounting pairs with range sum in a limit in JavaScript
In JavaScript, counting pairs with range sum within a limit involves finding all possible subarrays whose sum falls within a specified range. This is a common array processing problem that requires careful handling of cumulative sums. Range Sum Range sum rangeSum(i, j) is defined as the sum of the elements in an array between indices i and j (i ≤ j), inclusive. Problem We are required to write a JavaScript function that takes in an array of Integers, arr, as the first argument and two numbers, lower and upper as the second and third element. ...
Read MoreDetermining sum of array as even or odd in JavaScript
Problem We are required to write a JavaScript function that takes in an array of integers arr. Our function should return the string 'odd' if the sum of all the elements of the array is odd or 'even' if it's even. Example Following is the code − const arr = [5, 1, 8, 4, 6, 9]; const assignSum = (arr = []) => { const sum = arr.reduce((acc, val) => { return acc + val; }, 0); ...
Read MoreFinding the sum of minimum value in each row of a 2-D array using JavaScript
Problem We are required to write a JavaScript function that takes in a 2-D array of numbers. Our function should pick the smallest number from each row of the 2-D array and then finally return the sum of those smallest numbers. Example Following is the code − const arr = [ [2, 5, 1, 6], [6, 8, 5, 8], [3, 6, 7, 5], [9, 11, 13, 12] ]; const sumSmallest = (arr = []) => { const findSmallest = array => ...
Read More