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
Web Development Articles
Page 262 of 801
Reversing 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 least number of elements to convert array into increasing sequence using JavaScript
We need to write a JavaScript function that removes the minimum number of elements from an array to make it strictly increasing. This is essentially finding the Longest Increasing Subsequence (LIS) and removing all other elements. Problem Analysis The goal is to find the longest possible increasing subsequence and remove the fewest elements. A strictly increasing sequence means each element is greater than the previous one. Example with Corrected Algorithm The original approach has flaws - it doesn't find the optimal solution. Here's a correct implementation using dynamic programming: const arr = [1, 100, ...
Read MoreFinding all possible prime pairs that sum upto input number using JavaScript
Problem We need to write a JavaScript function that takes a number n and returns an array of all prime number pairs that sum to n. Both numbers in each pair must be prime numbers. Understanding Prime Numbers A prime number is a natural number greater than 1 that has no positive divisors other than 1 and itself. For example: 2, 3, 5, 7, 11, 13, 17, 19, 23. Algorithm Approach The solution involves three main steps: Create a function to check if a number is prime Generate all possible prime numbers up to ...
Read MoreFinding nth element of the Padovan sequence using JavaScript
Padovan Sequence The Padovan sequence is a sequence of integers P(n) defined by the initial values: P(0) = P(1) = P(2) = 1 and the recurrence relation: P(n) = P(n-2) + P(n-3) The first few values of P(n) are: 1, 1, 1, 2, 2, 3, 4, 5, 7, 9, 12, 16, 21, 28, 37, 49, 65, 86, 114, 151, 200, 265, ... Problem We need to write a JavaScript function that takes in a number n and returns the nth term of the Padovan sequence. Using Iterative Approach The most efficient approach uses iteration to calculate the sequence step by step: const padovan = (num = 1) => { // Handle base cases if (num
Read MoreFinding the k-prime numbers with a specific distance in a range in JavaScript
K-Prime Numbers A natural number is called k-prime if it has exactly k prime factors, counted with multiplicity. For example, 4 is a 2-prime number because 4 = 2 × 2, and both instances of 2 are counted separately. Similarly, 8 is a 3-prime number because 8 = 2 × 2 × 2, giving us three prime factors. Problem We need to write a JavaScript function that takes a number k, a distance, and a range. The function should return an array of pairs containing k-prime numbers within the range where the distance between them equals ...
Read MoreConverting an unsigned 32 bit decimal to corresponding ipv4 address in JavaScript
Converting a 32-bit unsigned integer to an IPv4 address involves extracting the four bytes that represent each octet of the IP address. Each IPv4 address consists of four octets (0-255), which can be extracted using bitwise operations. Understanding the Problem Consider the IPv4 address: 128.32.10.1 When converted to binary, each octet becomes an 8-bit binary number: 10000000.00100000.00001010.00000001 Combining these four 8-bit segments creates a 32-bit binary number, which equals the decimal value: 2149583361 Our task is to reverse this process: convert the 32-bit unsigned integer back ...
Read MoreCounting the number of IP addresses present between two IP addresses in JavaScript
We are required to write a JavaScript function that takes in two IPv4 addresses, and returns the number of addresses between them (including the first one, excluding the last one). This can be done by converting them to decimal and finding their absolute difference. How IP Address Conversion Works An IPv4 address consists of 4 octets (0-255). To convert to decimal, we use the formula: IP: a.b.c.d converts to: Decimal = a×256³ + b×256² + c×256¹ + d×256⁰ Example: 20.0.0.10 = 20×16777216 + 0×65536 + 0×256 + 10×1 ...
Read MoreGenerating the sequence of first n look and say numbers in JavaScript
Problem In mathematics, the look-and-say sequence is the sequence of integers beginning as follows: 1, 11, 21, 1211, 111221, 312211, ... To generate a member of the sequence from the previous member, we read off the digits of the previous member, counting the number of digits in groups of the same digit. For instance, the next number to 1211 is: 111221 Because if we read the digits of 1211 aloud it will be: One one, one two, two one which gives us 111221 We are required to write a JavaScript ...
Read MoreRectifying the time string in JavaScript
In JavaScript, we often need to rectify invalid time strings where the minutes or seconds exceed their maximum values (59). This function converts an invalid time format like "08:11:71" into a valid one by carrying over excess values to the next time unit. Problem We need to write a JavaScript function that takes in a time string in "HH:MM:SS" format. However, many time strings are broken, meaning the MM part may exceed 60, and SS part may exceed 60 as well. Our function should make required changes and return the rectified string. For instance: "08:11:71" ...
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 More