Suppose we have an array of positive integers that represents the number of citations a particular researcher has conducted over a period of time.We are required to write a JavaScript function that takes in one such array and the function should find the h-index of that researcher based on the citations data represented by the array.H-Index:Consider a researcher who performed N number of citations in his career. Then the researcher has index h if h of his/her N papers have at least h citations each, and the other N − h papers have no more than h citations each.For example ... Read More
We are required to write a JavaScript function that takes in three arguments, namely −arr --> an array of integers m --> a positive integer n --> a positive integerThe task of our function is to find out whether there exists two such elements (lets call them a1 and a2) such that −The absolute difference between a1 and a2 is at most mThe absolute difference between the indices of a1 and a2 is at most nExampleFollowing is the code −const arr = [1, 2, 3, 1, 7, 8]; const findSpecialElements = (arr = [], m, n) => { const ... Read More
We are required to write a JavaScript function that takes in an array of numbers as the first and the only argument.The function should string together the numbers present in the array such that form the greatest possible number that can be formed from those given set of numbers.For example −If the input array is −const arr = [5, 45, 34, 9, 3];Then the output should be −const output = '9545343';ExampleFollowing is the code −const arr = [5, 45, 34, 9, 3]; const largestNumber = (arr = []) => { if(arr.every( n => n === 0)){ ... Read More
For the purpose of this question, we define an interval as an array of two numbers where the first number is always smaller than the second number.For example −[4, 6], [2, 3], [6, 8], [2, 7], [1, 8] are all examples of valid intervals.Suppose, we have an array of intervals which is sorted according to the their start times (the first elements of each interval).The intervals in the array are non-overlapping which means that for any two arbitrary adjacent intervals, [m, n], [x, y] m < n < x < yTherefore, one such example of this array of intervals can ... Read More
Suppose we have an array of Numbers that contains any frequency of exactly three elements - 1, 0 and 1 like this −const arr = [1, 1, 0, -1, 1, 0, -1, 1, 0, 0, 1];We are required to write a JavaScript function that takes in one such array. The function should simply sort this special array in place i.e., without using any extra array to store the values.The only condition is that our function should be a linear time function (using only one iteration).ExampleFollowing is the code −const arr = [1, 1, 0, -1, 1, 0, -1, 1, 0, ... Read More
We are required to write a JavaScript function that takes in an array of numbers as the first and the only argument.The function should then try removing one such element from the array, upon removal of which, the sum of elements at odd indices is equal to the sum of elements at even indices. In the way, the function should count all the possible unique ways in which we can remove one element at a time to achieve the required combination.For example −If the input array is −const arr = [2, 6, 4, 2];Then the output should be 2 because, ... Read More
Suppose we have an array of objects like this −const arr = [{ name: 'Dinesh Lamba', age: 23, occupation: 'Web Developer', }, { address: 'Vasant Vihar', experience: 5, isEmployed: true }];We are required to write a JavaScript function that takes in one such array of objects. The function should then prepare an object that contains all the properties that exist in all the objects of the array.Therefore, for the above array, the output should look like −const output = { name: 'Dinesh Lamba', age: 23, occupation: 'Web Developer', address: 'Vasant ... Read More
We are required to write a JavaScript function that takes in an array of integers as the first argument and an integer as the second argument.The function should check whether we can create n (second argument) subarrays from the original array such that all the subarrays have the equal sum.For example −If the inputs are −const arr = [4, 3, 2, 3, 5, 2, 1]; const num = 4;The output should be true because the subarrays are: [5], [1, 4], [2, 3], [2, 3] all having sum equal to 5.ExampleFollowing is the code −const arr = [4, 3, 2, 3, ... Read More
Tribonacci Series:The tribonacci sequence is a generalization of the Fibonacci sequence where each term is the sum of the three preceding terms.For example, the first few terms of the tribonacci series are −0, 1, 1, 2, 4, 7, 13, 24, 44, 81, 149We are required to write a JavaScript function that takes in a number, say num, as the only argument.The function should then return an array of num elements, containing the first num terms of the tribonacci series.For example:f(6) = 0, ExampleFollowing is the code:const tribonacci = (num = 1) => { if (num === 0 || num ... Read More
The diameter of a binary tree is the (left_height + right_height + 1) for each node. So in this method we will calculate (left_height + right_height + 1) for each node and update the result . The time complexity here stays O(n).Let us first define the struct that would represent a tree node that contains the data and its left and right node child. If this is the first node to be created then it’s a root node otherwise a child node.struct Node { int data; struct Node *leftChild, *rightChild; };Next we create our newNode(int data) function that ... Read More
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP