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 326 of 840
Flip the matrix horizontally and invert it using JavaScript
We are required to write a JavaScript function that takes in a 2-D binary array (an array that consists of only 0 or 1) as the first and only argument. Our function should first flip the matrix horizontally, then invert it, and return the resulting matrix. Understanding the Operations To flip the matrix horizontally means that each row of the matrix is reversed. For example, flipping [1, 1, 0] horizontally results in [0, 1, 1]. To invert a matrix means that each 0 is replaced by 1, and each 1 is replaced by 0. For example, ...
Read MoreFinding trailing zeros of a factorial JavaScript
Given an integer n, we have to write a function that returns the number of trailing zeroes in n! (factorial). For example: trailingZeroes(4) = 0 trailingZeroes(5) = 1 because 5! = 120 trailingZeroes(6) = 1 How It Works Trailing zeros are created by multiplying factors of 10, which come from pairs of 2 and 5. Since there are always more factors of 2 than 5 in any factorial, we only need to count factors of 5. We count multiples of 5, then 25 (5²), then 125 (5³), and so on. Example const num = 17; const findTrailingZeroes = num => { let cur = 5, total = 0; while (cur
Read MoreWriting a custom URL shortener function in JavaScript
URL shorteners convert long URLs into shorter, more manageable links. This tutorial shows how to build a simple URL shortener with JavaScript functions for encoding and decoding URLs. Problem Statement We need to create two JavaScript functions: First function takes a long URL and returns a corresponding short URL Second function takes the short URL and returns the original long URL How It Works Our approach uses an object to store mappings between short and long URLs. The short URL is generated by extracting letters from the original URL and taking the last ...
Read MoreImplementing circular queue ring buffer in JavaScript
A circular queue is a linear data structure that operates on the FIFO (First In First Out) principle, where the last position connects back to the first position forming a circle. It's also called a "Ring Buffer". The main advantage of a circular queue is efficient space utilization. Unlike regular queues, when elements are dequeued from the front, those positions become available for new elements, preventing wasted space. 0 1 ...
Read MoreCounting prime numbers from 2 upto the number n JavaScript
We are required to write a JavaScript function that takes in a number, say n, as the first and the only argument. The function should then return the count of all the prime numbers from 2 up to the number n (exclusive). For example: For n = 10, the output should be: 4 (2, 3, 5, 7) For n = 1, the output should be: 0 Understanding Prime Numbers A prime number is a natural number greater than 1 that has no positive divisors other than 1 and itself. The first few prime ...
Read MoreCutting off number at each digit to construct an array in JavaScript
We need to write a JavaScript function that takes a number and returns an array of strings, where each string represents the number cut off at each digit position. Problem Given a number like 246, we want to create an array containing: First digit: "2" First two digits: "24" All digits: "246" Example Here's the implementation: const num = 246; const cutOffEach = (num = 1) => { const str = String(num); const res = []; let temp = ''; ...
Read MoreCorresponding shortest distance in string in JavaScript
We are required to write a JavaScript function that takes in a string of English lowercase alphabets, str, as the first argument and a single character, char, which exists in the string str, as the second argument. Our function should prepare and return an array which, for each character in string str, contains its distance from the nearest character in the string specified by char. Problem Example For example, if the input to the function is: const str = 'somestring'; const char = 's'; The expected output should be: const output ...
Read MoreDetermining isomorphic strings JavaScript
Two strings are isomorphic if characters in one string can be mapped to characters in another string while preserving the structure. Each character must map to exactly one character, and no two characters can map to the same character. Understanding Isomorphic Strings For strings to be isomorphic: They must have the same length Characters at the same positions must follow a consistent mapping pattern The mapping must be bijective (one-to-one) Example const str1 = 'egg'; const str2 = 'add'; // Check if strings are isomorphic const isIsomorphic = (str1 = '', str2 ...
Read MoreCreating a Projectile class to calculate height horizontal distance and landing in JavaScript
We need to create a JavaScript class called Projectile that simulates projectile motion physics. This class calculates horizontal distance traveled by a projectile given initial conditions. Problem Statement Create a Projectile class that takes three parameters during initialization: Starting height (h0): 0 ≤ h0 < 200 Starting velocity (v0): 0 < v0 < 200 Launch angle (a): 0° < a < 90°, measured in degrees The class should include a horiz method that calculates horizontal distance traveled at time t. Physics Formula The ...
Read MoreGreatest sum of average of partitions in JavaScript
We are required to write a JavaScript function that takes in an array of numbers, arr, as the first argument, and a number, num, (num ≤ size of arr), as the second argument. Our function should partition the array arr into at most num adjacent (non-empty) groups in such a way that we leave no element behind. From all such partitions, our function should pick that partition where the sum of averages of all the groups is the greatest. Problem Statement Given an array and a maximum number of partitions, find the partition that maximizes the sum ...
Read More