Javascript Articles

Page 141 of 534

Smallest prime number just greater than the specified number in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 444 Views

We are required to write a JavaScript function that takes in a positive integer as the first and the only argument. The function should find one such smallest prime number which is just greater than the number specified as argument. For example − If the input is − const num = 18; Then the output should be: const output = 19; Understanding Prime Numbers A prime number is a natural number greater than 1 that has no positive divisors other than 1 and itself. To check if a ...

Read More

Finding the smallest positive integer not present in an array in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 918 Views

We are required to write a JavaScript function that takes in array of integers as the first and the only argument. Our function should find and return that smallest positive integer which is not present in the array. For example, if the input array is: const arr = [4, 2, -1, 0, 3, 9, 1, -5]; Then the output should be: 5 Because 1, 2, 3, 4 are already present in the array and 5 is the smallest positive integer absent from the array. Using Sequential Search Approach ...

Read More

Searching in a sorted 2-D array in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 148 Views

We are required to write a JavaScript function that takes in an array of arrays of numbers as the first argument and a number as the second argument. The subarrays contain numbers sorted in an increasing order and no element of a preceding subarray is greater than any element of the succeeding subarray. The function should use the binary search algorithm to search for the element provided as the second argument in the sorted array of arrays. If the element exists the function should return true, false otherwise. For example − If the input array is ...

Read More

Finding words in a matrix in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 550 Views

We need to write a JavaScript function that takes a 2D character matrix and a string, then determines if the string can be formed by connecting adjacent characters in the matrix without reusing any position. The function searches for a path through the matrix where each character in the target string appears in sequence through adjacent cells (up, down, left, right). Each cell can only be used once per word search. Problem Example Given this matrix and target string: const arr = [ ['s', 'd', 'k', 'e'], ...

Read More

Finding all possible subsets of an array in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 2K+ Views

Finding all possible subsets of an array is a common algorithmic problem. A subset is any combination of elements from the original array, including the empty set and the full array itself. For an array of n elements, there are 2^n possible subsets. This is because each element can either be included or excluded from a subset. For example, if the input array is: const arr = [1, 2, 3]; The output should contain all possible subsets: [ [], [1], [2], [3], ...

Read More

Finding smallest element in a sorted array which is rotated in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 180 Views

We are required to write a JavaScript function that takes in an array of integers as the only argument. The array is first sorted and then rotated by any arbitrary number of elements. Our function should find the smallest element in the array and return that element. The only condition is that we have to do this in less than linear time complexity, using a modified binary search algorithm that achieves O(log n) time complexity. Problem Understanding In a rotated sorted array like [6, 8, 12, 25, 2, 4, 5], the original sorted array [2, 4, ...

Read More

Finding the length of second last word in a sentence in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 524 Views

A sentence is just a string which contains strings (called words) joined by whitespaces. We are required to write a JavaScript function that takes in one such sentence string and count the number of characters in the second to last word of the string. If the string contains no more than 2 words, our function should return 0. For example − If the input string is − const str = 'this is an example string'; Then the output should be − const output = 7; because the number of characters ...

Read More

Finding desired numbers in a sorted array in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 253 Views

We have an array of integers which is sorted in the increasing order. We are required to write a JavaScript function that takes in one such array as the first argument and a target sum number as the second argument. The function should find and return two such numbers from the array that when added gives the target sum. The condition for solving this problem is that we have to do this in linear time and using constant space. Two Pointer Approach The optimal solution uses the two-pointer technique. Since the array is sorted, we can place ...

Read More

Finding the first non-repeating character of a string in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 542 Views

We are required to write a JavaScript function that takes in a string as the first and the only argument. The function should find and return the index of first character it encounters in the string which appears only once in the string. If the string does not contain any unique character, the function should return -1. For example, if the input string is: const str = 'hellohe'; Then the output should be: const output = 4; Because the character 'o' at index 4 is the first character that ...

Read More

Prime digits sum of a number in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 355 Views

We are required to write a JavaScript function that takes in a number as the first and the only argument. The function should then sum all the digits of the number that are prime and return the sum as a number. For example − If the input number is − const num = 67867852; Then the output should be − const output = 21; because 7 + 7 + 5 + 2 = 21 − Understanding Prime Digits Prime digits are single-digit prime numbers: 2, 3, 5, and ...

Read More
Showing 1401–1410 of 5,340 articles
« Prev 1 139 140 141 142 143 534 Next »
Advertisements