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 294 of 840
Maximum consecutive 1s after n swaps in JavaScript
We are required to write a JavaScript function that takes in a binary array (array that contains only 0 or 1) as the first argument, and a number as the second argument representing the maximum number of swaps allowed. We can change at most the specified number of 0s present in the array to 1s, and our function should return the length of the longest contiguous subarray that contains only 1s after making these changes. Problem Statement Given a binary array and a number of allowed swaps, find the maximum length of consecutive 1s we can achieve ...
Read MoreFinding the sum of floors covered by an elevator in JavaScript
Problem We need to write a JavaScript function that calculates the total number of floors covered by an elevator. The function takes an array representing the floor numbers where the elevator stopped, and returns the sum of distances traveled between consecutive stops. Understanding the Logic The elevator covers floors by moving between consecutive stops. If it goes from floor 7 to floor 1, it covers 6 floors (7-1). If it then goes from floor 1 to floor 7, it covers another 6 floors (7-1). The total distance is the sum of absolute differences between consecutive floors. ...
Read MoreGet values that are not present in another array in JavaScript
We are given two arrays: (arr1 and arr2) − arr1 contains some literal values. arr2 contains objects that map some literal values. We are required to write a JavaScript function that takes in two such arrays. Then the function should return an array of all the elements from arr1 that are not mapped by objects in arr2. Example The code for this will be − const arr1 = [111, 222, 333, 444]; const arr2 = [ { identifier: 111 }, ...
Read MoreList all duplicate values in a nested JavaScript object
When working with nested JavaScript objects, you may need to find duplicate values that appear at different levels of nesting. This tutorial shows how to recursively search through a nested object and identify all duplicate values. Problem Statement Consider a nested object containing pet information: const pets = { owner1: 'Frank', owner2: 'Curly', owner3: 'Maurice', dogs: { terriers: { name1: ...
Read MoreFind first duplicate item in array in linear time JavaScript
We are required to write a JavaScript function that takes in a read only array of n + 1 integers between 1 and n. The function should find one number that repeats in linear time and using at most O(n) space. For example If the input array is − const arr = [3, 4, 1, 4, 1]; Then the output should be − 4 If there are multiple possible answers (like above), we should output any one. If there is no duplicate, we should output -1. Using Set Data ...
Read MoreCommons including duplicates in array elements in JavaScript
We need to write a JavaScript function that finds all common characters across all strings in an array, including duplicates. If a character appears multiple times in every string, it should appear that many times in the result. Problem Statement Given an array of strings, return an array of characters that appear in all strings. The frequency of each character in the result should match the minimum frequency of that character across all strings. For example, with the input array ['door', 'floor', 'crook']: 'o' appears 2 times in "door", 2 times in "floor", and 2 times ...
Read MoreFinding a number, when multiplied with input number yields input number in JavaScript
We need to write a JavaScript function that takes a positive integer n and a positive integer p, then finds a value k such that the sum of digits raised to successive powers equals k * n. Problem Statement Given a number n with digits a, b, c, d... and a power p, we want to find integer k where: (a^p + b^(p+1) + c^(p+2) + d^(p+3) + ...) = n * k If such k exists, return k; otherwise return -1. Example Walkthrough For number 695 and p = 2: 6^2 + ...
Read MoreDynamic Programming: return all matched data in JavaScript
Dynamic programming in JavaScript can be used to efficiently search and retrieve data from nested structures. This article demonstrates how to search for all matching cities in a complex JSON object containing country and province information. The Data Structure We have a nested JSON object with countries, provinces, and cities: const countryInfo = { country: [{ name: "Bangladesh", province: [{ name:"Dhaka", ...
Read MoreFinding upper elements in array in JavaScript
We are required to write a JavaScript function that takes in an array of numbers as the first argument and a single number as the second argument. The function should return an array of all the elements from the input array that are greater than or equal to the number taken as the second argument. Therefore, let's write the code for this function — Using for Loop The most straightforward approach uses a for loop to iterate through the array and check each element: const arr = [56, 34, 2, 7, 76, 4, 45, 3, ...
Read MoreFinding all possible combined (plus and minus) sums of n arguments JavaScript
We need to write a JavaScript function that takes any number of arguments (all numbers) and finds the sum closest to zero from all possible combinations of addition and subtraction. For example, with arguments 1, 2, 3, the possible combinations are: 1 + 2 + 3 = 6 1 - 2 - 3 = -4 1 + 2 - 3 = 0 1 - 2 + 3 = 2 The function should return the sum closest to 0, which in this case is 0. Algorithm Approach We use a Set to store all ...
Read More