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 318 of 840
Nth smallest element in sorted 2-D array in JavaScript
In JavaScript, finding the Nth smallest element in a sorted 2D array requires an efficient approach. We'll use binary search on the value range rather than indices. Problem Statement Given a sorted 2D array (sorted in increasing order both row-wise and column-wise), find the Nth smallest element. const arr = [ [ 1, 5, 9], [10, 11, 13], [12, 13, 15] ]; For example, if we want the 5th smallest element from the above array, the answer would be 11. Approach: Binary Search on Values ...
Read MoreSum all perfect cube values upto n using JavaScript
Problem We are required to write a JavaScript function that takes in a number n and returns the sum of all perfect cube numbers smaller than or equal to n. Understanding Perfect Cubes Perfect cubes are numbers that can be expressed as n³ where n is an integer. For example: 1³ = 1, 2³ = 8, 3³ = 27, 4³ = 64, etc. Example Following is the code − const num = 23546; const sumPerfectCubes = (num = 1) => { let i = 1; let sum = 0; while(i * i * i
Read MoreImplementing Math function and return m^n in JavaScript
We are required to write a JavaScript function that takes in two numbers say m and n. Then function should calculate and return m^n. For example − For m = 4, n = 3, then power(4, 3) = 4^3 = 4 * 4 * 4 = 64 power(6, 3) = 216 Using Built-in Math.pow() Method JavaScript provides the built-in Math.pow() method for calculating powers: console.log(Math.pow(4, 3)); // 4^3 console.log(Math.pow(6, 3)); // 6^3 console.log(Math.pow(2, -2)); // 2^(-2) = 1/4 64 216 0.25 Custom Implementation Using Recursion ...
Read MoreFinding a number and its nth multiple in an array in JavaScript
We are required to write a JavaScript function that takes in an array of integers as the first argument and a number, say n, as the second argument. The function should check whether there exists two such numbers in the array that one is the nth multiple of the other. If there exists any such pair in the array, the function should return true, false otherwise. For example, if we have an array containing numbers 2 and 8, and n = 4, then 8 is the 4th multiple of 2 (8 = 2 × 4), so the ...
Read MoreAny possible combination to add up to target in JavaScript
We are required to write a JavaScript function that takes in an array of unique integers, arr, as the first argument, and target sum as the second argument. Our function should count the number of all pairs (with repetition allowed) that can add up to the target sum and return that count. Problem Example For example, if the input to the function is: const arr = [1, 2, 3]; const target = 4; Then the output should be: 7 Output Explanation The possible combination ways are: ...
Read MoreReversing the bits of a decimal number and returning new decimal number in JavaScript
We are required to write a JavaScript function that takes in a decimal number, converts it into binary and reverses its bits (1 to 0 and 0 to 1) and returns the decimal equivalent of the new binary thus formed. Problem Given a decimal number, we need to: Convert the decimal number to binary Flip each bit (0 becomes 1, 1 becomes 0) Convert the resulting binary back to decimal Example Let's implement the function to reverse bits and convert back to decimal: ...
Read MoreRemoving duplicate objects from array in JavaScript
Removing duplicate objects from arrays is a common requirement in JavaScript applications. Let's explore different approaches to eliminate duplicate objects while preserving the original data structure. The Problem Consider an array containing duplicate objects: const arr = [ {"title": "Assistant"}, {"month": "July"}, {"event": "Holiday"}, {"title": "Assistant"} ]; console.log("Original array:", arr); Original array: [ { title: 'Assistant' }, { month: 'July' }, { event: 'Holiday' }, { title: 'Assistant' ...
Read MoreFinding the sub array that has maximum sum JavaScript
We are required to write a JavaScript function that takes in an array of numbers. The array can contain both positive and negative numbers. The purpose of our function is to find the subarray (of any length) whose elements when summed give the maximum sum. This problem is known as the Maximum Subarray Problem and is commonly solved using Kadane's Algorithm. For example, if the input array is: const arr = [-2, 1, -3, 4, -1, 2, 1, -5, 4]; Then the output should be: 6 Because the subarray [4, ...
Read MoreFinding Number of Days Between Two Dates JavaScript
Finding the number of days between two dates is a common requirement in JavaScript applications. While you can implement complex date calculations manually, JavaScript's built-in Date object provides a much simpler and more reliable approach. The Simple Approach Using Date Objects The most straightforward method is to convert date strings to Date objects and calculate the difference in milliseconds, then convert to days: const daysBetweenDates = (date1, date2) => { const firstDate = new Date(date1); const secondDate = new Date(date2); ...
Read MoreUnique number of occurrences of elements in an array in JavaScript
We are required to write a JavaScript function that takes in an array of integers as the first and the only argument. The function should check whether all the integers that are present in the array appear for unique number of times or not. If they do, the function should return true, false otherwise. For example − If the input array is − const arr = [7, 5, 5, 8, 2, 4, 7]; Then the output should be − false because both the integers 7 and 5 appears ...
Read More