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 310 of 840
Partition N where the count of parts and each part are a power of 2, and part size and count are restricted in JavaScript
We need to write a JavaScript function that partitions a number into chunks following specific rules: The number of chunks must be a power of 2 (1, 2, 4, 8, 16, etc.) Each chunk size must also be a power of 2, with a maximum limit Understanding the Problem Let's examine how different numbers can be partitioned: For number 8: [8] This works because we have 1 chunk (power of 2) with size 8 (power of 2). For number 9: [8, 1] This works because we have 2 chunks (power of 2), each ...
Read MoreSum excluding one element in JavaScript
In JavaScript, when you need to find the minimum and maximum possible sums by excluding exactly one element from an array, you can solve this efficiently using a single loop approach. The key insight is that to get the minimum sum (excluding one element), you remove the largest element. To get the maximum sum (excluding one element), you remove the smallest element. Problem Statement Given an array of integers, return an array with two values: First integer: smallest possible sum excluding any one element Second integer: greatest ...
Read MoreFinding smallest number that satisfies some conditions in JavaScript
We are required to write a JavaScript function that takes in a number as the first argument, say n, and an array of numbers as the second argument. The function should return the smallest n-digit number which is a multiple of all the elements specified in the array. If there exist no such n-digit element then we should return the smallest such element. For example: If the array is − const arr = [12, 4, 5, 10, 9] For both n = 2 and n = 3, we need to find the smallest number ...
Read MorePush specific elements to last in JavaScript
Suppose we have an array of objects like this: const arr = [ {flag: true, other: 1}, {flag: true, other: 2}, {flag: false, other: 3}, {flag: true, other: 4}, {flag: true, other: 5}, {flag: true, other: 6}, {flag: false, other: 7} ]; We are required to write a JavaScript function that takes in one such array and sorts it based on the following conditions: If arr.flag === false, the matching ...
Read MoreJavaScript - find distance between items on array
In JavaScript, finding the distance between items in an array involves calculating the difference between each element and all succeeding elements. This creates a matrix of distances that can be useful for various algorithms and data analysis. Suppose we have a sorted (increasing order) array of Numbers like this: const arr = [2, 5, 7, 8, 9]; We need to write a JavaScript function that takes in one such array. The function should construct a new subarray for each element of the input array containing the differences between that element and all succeeding elements. ...
Read MoreFinding desired sum of elements in an array in JavaScript
Finding groups of elements in an array that sum to a desired value is a common programming problem. This involves checking combinations of consecutive elements to match both a target sum and group size. Problem Statement Given an array of numbers, we need to find how many consecutive groups of a specific length sum up to a target value. For example: const arr = [1, 2, 1, 3, 2]; const sum = 3; const num = 2; We want to find groups of 2 consecutive elements that sum to 3. The groups [1, 2] ...
Read MoreHow to implement multiple input checkbox in vanilla JavaScript?
We will learn how to implement multiple input checkbox functionality in vanilla JavaScript. The checkbox input selector will have the following features: Multiple options can be selected using checkboxes Chosen options will be displayed as a separate list with visual indicators Delete icon will be provided against each chosen option to uncheck/remove that option We will implement this functionality using only HTML, CSS, and JavaScript without any third-party libraries. Approach Create an object where keys represent checkbox labels and values (true/false) ...
Read MoreFinding the inclination of arrays in JavaScript
We are required to write a JavaScript function that takes in an array of numbers and returns true if it's either strictly increasing or strictly decreasing, otherwise returns false. In Mathematics, a strictly increasing function is that function in which the value to be plotted always increases. Similarly, a strictly decreasing function is that function in which the value to be plotted always decreases. Understanding the Concept An array has a consistent inclination when: Strictly increasing: Each element is greater than the previous one Strictly decreasing: Each element ...
Read MoreFinding a pair that is divisible by some number in JavaScript
We need to write a JavaScript function that finds all pairs of numbers from an array whose sum is divisible by a given number. The function takes an array of numbers and a divisor as parameters. Problem Statement Given an array of numbers and a divisor, find all pairs where: (arr[i] + arr[j]) % divisor === 0, and i < j For example, with array [1, 2, 3, 4, 5, 6] and divisor 4, we need pairs whose sum is divisible by 4. Input and Expected Output Input: const arr = ...
Read MoreVolume difference of cuboids in JavaScript
We are required to write a JavaScript function that takes in two arrays, specifying the lengths, widths, and heights of two cuboids. Our function should calculate the volume of both cuboids and return their absolute difference. Formula The volume of a cuboid is calculated as: Volume = length × width × height Example Following is the code: const h1 = 10; const w1 = 12; const l1 = 15; const h2 = 12; const w2 = 15; const l2 = 9; const findVolumeDifference = (l1, w1, h1, l2, w2, ...
Read More