Maximum Product of Two Numbers in a List of Integers in JavaScript

AmitDiwan
Updated on 23-Jan-2021 06:48:39

267 Views

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 find the maximum product that can be achieved multiplying any two elements of the array. The condition for us is that we have to do this in linear time and constant space.For example −If the input array is −const arr = [3, 9, 2, 1, 0];Then the output should be −const output = 27;because it’s the greatest product and can be achieved by multiplying 3 and 9.ExampleFollowing is the code −const arr = [3, 9, 2, ... Read More

Check If a String is Repeating in Itself in JavaScript

AmitDiwan
Updated on 23-Jan-2021 06:44:45

818 Views

We are required to write a JavaScript function that takes in a string as the first and the only argument.The function should detect if the string is a repetition of a same set of characters or not.If it is a repetition of the same set of characters then we should return true, false otherwise.For example −If the input string is −const str = 'carcarcarcar';Then the output should be −const output = true;because the string 'car' is getting repeated over and over again in the string.ExampleFollowing is the code −const str = 'carcarcarcar'; const isRepeating = (str = '') => { ... Read More

Count Palindromes Constructed from a String in JavaScript

AmitDiwan
Updated on 23-Jan-2021 06:42:58

340 Views

We are required to write a JavaScript function that takes in a string of characters as the first argument, say str, and a number, say num, as the second argument.The function should count the number of palindrome strings all exactly of length num can be constructed from the given string str. The function should then finally return the count.For example −If the input string and the number is −const str = 'ij'; const num = 4;Then the output should be −const output = 4;because those four possible palindrome strings are −'iiii', 'jjjj', 'ijji', 'jiij'Approach:We will first count the number of ... Read More

Returning Acronym Based on a String in JavaScript

AmitDiwan
Updated on 23-Jan-2021 06:38:41

1K+ Views

We are required to write a JavaScript function that takes in a string of characters as the only argument.The function should build and return the acronym based on the string phrase provided as input.While constructing the acronym the function should take only those words into consideration that starts with an uppercase letter.For example −If the input string is −const str = 'Polar Satellite Launch Vehicle';Then the output should be −const output = 'PSLV';ExampleFollowing is the code −const str = 'Polar Satellite Launch Vehicle'; const buildAcronym = (str = '') => {    const strArr = str.split(' ');    let res ... Read More

Counting Largest Numbers in Row and Column in 2D Array in JavaScript

AmitDiwan
Updated on 23-Jan-2021 06:36:48

430 Views

We are required to write a JavaScript function that takes in a two-dimensional array of integers as the only argument.The task of our function is to calculate the count of all such integers from the array that are the greatest both within their row and the column.The function should then return that count.For example −If the input array is −const arr = [    [21, 23, 22],    [26, 26, 25],    [21, 25, 27] ];Then the output should be −const output = 3;because those three numbers are 26, 26, 27ExampleFollowing is the code −const arr = [    [21, ... Read More

Sort Only Columns of a 2D Array in JavaScript

AmitDiwan
Updated on 23-Jan-2021 06:34:51

789 Views

We are required to write a JavaScript function that takes in a two-dimensional array of integers as the only argument.The function should sort the elements present at the column of the array in increasing or decreasing order.For example −If the input array is −const arr = [    [6, 2, 9],    [8, 1, 4],    [5, 3, 7] ];Then the array should be sorted like this −const output = [    [8, 3, 9],    [6, 2, 7],    [5, 1, 4] ];ExampleFollowing is the code −const arr = [    [6, 2, 9],    [8, 1, 4],   ... Read More

Substring in Infinitely Extended String in JavaScript

AmitDiwan
Updated on 23-Jan-2021 06:32:06

156 Views

We are required to write a JavaScript function that takes in a string of characters as the first argument and a start index and end index as second and third argument respectively. The function should find, had that string, provided as the first argument, been extended forever by appending the same string at end each time, what would have been the substring encapsulated by the start index and the end index.For example −If the input string and the indices are −const str = 'helloo'; const start = 11; const end = 15;Then the output should be −const output = 'hel';ExampleFollowing ... Read More

Find Index of Extra Element in Sorted Array in C++

sudhir sharma
Updated on 22-Jan-2021 14:08:10

709 Views

In this problem, we are given two sorted arrays arr1 and arr2 of size n  and n+1 with all elements the same except the extra element. Our task is to find index of an extra element present in one sorted array. Problem Description: We need to find the index of an element from the n+1 size array which is not present in an array of size n.Let’s take an example to understand the problem, Input:         arr1[n]  = {3, 5, 7, 8, 9, 12}                   arr2[n+1] = {3, 4, 5, 7, 8, 9, ... Read More

Find If Two People Ever Meet After Same Number of Jumps in C++

sudhir sharma
Updated on 22-Jan-2021 14:06:52

302 Views

In this problem, we are given four integers denoting the starting points and jumps taken by each in the race. Our task is to find if two people ever meet after same number of jumps.  Problem Description: Here, we need to check if two persons starting at points p1 and p2 taking jumps j1 and j2 will be at some point in the path or not.Let’s take an example to understand the problem, Input: p1 = 5, p2 = 9, j1 = 4, j2 = 2Output: YesExplanation:After first jump, p1 = 9, p2 = 11After second jump, p1 = 13, p2 = 13Solution Approach: For ... Read More

Find Subset of Size K with 0 Sum in Array of 1 and +1 in C++

sudhir sharma
Updated on 22-Jan-2021 14:06:39

168 Views

In this problem, we are given an array arr[] consisting of only 1 and -1 and an integer value k. Our task is to find if there is any subset of size K with 0 sum in an array of -1 and +1. Let’s take an example to understand the problem, Input: arr[] = {-1, 1, -1, -1, 1 , 1, -1}, k = 4Output: YESExplanation:Subset of size 4, {-1, 1, -1, 1}. Sum = -1 + 1 - 1 + 1 = 0Solution Approach: We need to check if there exists any subset of size k whose sum is equal to 0. As ... Read More

Advertisements