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;ExampleFollowing is the code:const num = 18; const justGreaterPrime = (num) => { for (let i = num + 1;; i++) { let isPrime = true; for (let d = 2; d * d
We are required to write a JavaScript function that takes in a sentence as the first and the only argument.A sentence is a special kind of string of characters joined by finite number of whitespaces.The function should rearrange the words of the sentence such that the smallest word (word with least characters) appears first and then followed by bigger ones.For example −If the input string is −const str = 'this is a string';Then the output should be −const output = 'a is this string';ExampleFollowing is the code −const str = 'this is a string'; const arrangeWords = (str = []) ... Read More
We are required to write a JavaScript function that takes in an array of literals as the first and the only argument.The array is likely to contain many repeating values. Our function should sort the array such that the values that are unique or that have the least frequency are placed before the ones that have the most.For example −If the input array is −const arr = [4, 7, 3, 5, 5, 4, 7, 9, 2, 1, 5, 7, 5, 5, 9];Then the output array should be −const output = [ 3, 2, 1, 9, 9, 4, 4, ... Read More
We are required to write a JavaScript function that takes in a string, say str as the first argument and an array of positive integers, say arr of the same length as the second argument.Our function should shuffle the characters in the string such that the character at the ith position moves to arr[i] in the shuffled string.For example −If the input string and the array are −const str = 'example'; const arr = [5, 2, 0, 6, 4, 1, 3];Then the output should be −const output = 'alxepem';ExampleFollowing is the code −const str = 'example'; const arr = [5, ... Read More
We are required to write a JavaScript function that takes in a string as the first argument and a positive integer n as the second argument.The string is likely to contain some repeating characters. The function should find out and return the length of the longest substring from the original string in which all characters appear at least n number of times.For example −If the input string and the number are −const str = 'kdkddj'; const num = 2;Then the output should be −const output = 5;because the desired longest substring is 'kdkdd'ExampleFollowing is the code −const str = 'kdkddj'; ... Read More
Suppose we have a queue full of numbers. We have to check whether the consecutive elements in the queue are pairwise consecutive or not.So, if the input is like que = [3, 4, 6, 7, 8, 9], then the output will be True.To solve this, we will follow these steps −q := define a queue and insert all elements from given list into qtemp := a new listwhile q is not empty, doinsert front element of queue into temp and delete front element from queuetemp2 := a new listwhile temp is not empty, doinsert last element of temp into temp2delete ... Read More
Suppose we have a number n. We have to check whether the product of (1*2*...*n) is divisible by (1+2+...+n) or notSo, if the input is like num = 5, then the output will be True as (1*2*3*4*5) = 120 and (1+2+3+4+5) = 15, and 120 is divisible by 15.To solve this, we will follow these steps −if num + 1 is prime, thenreturn falsereturn trueExampleLet us see the following implementation to get better understanding − Live Demodef isPrime(num): if num > 1: for i in range(2, num): if num % i == 0: return False return True return False def solve(num): if isPrime(num + 1): return False return True num = 3 print(solve(num))Input5OutputTrue
Suppose we have a number n. We have to check whether the product of odd placed digits and the even placed digits are same or not.So, if the input is like n = 2364, then the output will be True as product of odd placed numbers are 2 * 6 = 12 and product of even placed numbers are 3 * 4 = 12 which are same.To solve this, we will follow these steps −if num < 10, thenreturn Falseodd_place := 1, even_place := 1while num > 0, dod := last digit of numodd_place := odd_place * dnum := quotient ... Read More
Suppose we have an array nums with all prime numbers. We have to check whether the product of all numbers present in nums is a perfect square or not.So, if the input is like nums = [3, 3, 7, 7], then the output will be True as product of all elements in nums is 441 which is a perfect square as 21^2 = 441.To solve this, we will follow these steps −m := a map containing all elements in nums and their frequenciesfor each key in nums, doif m[key] is odd, thenreturn Falsereturn TrueExampleLet us see the following implementation to ... Read More
Suppose we have two coordinates (sx, sy), and (tx, ty), we have to check whether we can move from starting point to ending point or not. Here we can move consists of taking a point (x, y) and transforming it to either (x, x+y) or (x+y, y).So if the inputs are (1, 1) and (4, 5), then the answer will be true, this is because move (1, 1) to (2, 1), then (3, 1), then (4, 1), then (4, 5).To solve this, we will follow these steps −while tx > sx and ty > sy, do −if tx > ty, ... Read More
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP