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
A matrix is said to be diagonally dominant matrix if for every matrix row, the diagonal entry magnitude of the row is larger than or equal to the sum of the magnitudes of every other non-diagonal entry in that row.Let us first define a constant int variable N with value 3 which represents our matrix dimensions.const int N = 3;The isDDM(int mat[N][N], int n) is a Boolean function that takes a copy of our matrix and the size of our matrix. Inside we iterate the rows and columns of our matrix using nested for loop. We then find the sum ... Read More
To consider the nodes that are passing between lines of slope -1. The diagonal traversal of the binary tree will be to traverse and print all those nodes present between these lines.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 createNode(int data) function that takes an int value and assign it to the data ... Read More
To consider the nodes that are passing between lines of slope -1. The diagonal sum of the binary tree will be calculated by the sum of all nodes data that are present between these lines of reference.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 createNode(int data) function that takes an int value and ... Read More