We are required to write a JavaScript function that takes in an array of numbers and returns a subarray of two elements from the original array whose sum is closest to 0.If the length of the array is less than 2, we should return the whole array.For example: If the input array is −const arr = [4, 4, 12, 3, 3, 1, 5, -4, 2, 2];Here, the sum of pair [5, -4] is 1 which is closest 0 for any two adjacent elements of the array, so we should return [5, -4]ExampleFollowing is the code −const arr = [4, 4, ... Read More
We are required to write a JavaScript function that takes in an array of numbers of length N (N should be even) and divides the array into two sub-array (left and right) containing N/2 elements each and do the sum of the subarrays and then multiply both the subarrays.For example: If the input array is −const arr = [1, 2, 3, 4]Then the output should be −(2+1) * (3+4) = 21ExampleFollowing is the code −const arr = [1, 2, 3, 4] const subArrayProduct = arr => { const { length: l } = arr; const creds = arr.reduce((acc, ... Read More
We are required to write a JavaScript function that takes in one such array and counts the sum of all distinct elements of the array.For example: Suppose, we have an array of numbers like this −const arr = [1, 5, 2, 1, 2, 3, 4, 5, 7, 8, 7, 1];The output for the array mentioned above will be 20.ExampleFollowing is the code −const arr = [1, 5, 2, 1, 2, 3, 4, 5, 7, 8, 7, 1]; const distinctSum = arr => { let res = 0; for(let i = 0; i < arr.length; i++){ if(i === arr.lastIndexOf(arr[i])){ res += arr[i]; }; continue; }; return res; }; console.log(distinctSum(arr));OutputFollowing is the output in the console −30
We are required to write a JavaScript function that takes in a number and checks whether it falls in Fibonacci series or not. We should return a boolean.Following is the code to check for Fibonacci −Exampleconst num = 89; const isFib = query => { if(query === 0 || query === 1){ return true; } let prev = 1; let count = 2; let temp = 0; while(count
In this problem, we are given two values that denote the base and height of a parallelogram. Our task is to create a Program to find the Area of a Parallelogram in C++.Parallelogram is a four side closed figure that has opposite sides equal and parallel to each other.Let’s take an example to understand the problem, InputB = 20, H = 15Output300ExplanationArea of parallelogram = B * H = 20 * 15 = 300Solution ApproachTo solve the problem, we will use the geometrical formula for area of parallelogram, Area = base * height.Program to illustrate the working of our solution, ... Read More
In this problem, we are given the three variables that denote total monthly expenditure (E), selling price (S) of the product, overhead maintenance (M) on each product. Our task is to create a program to find the Break Even Point in C++.Break-Even Point is the total number of products that are required to be sold so that there should not be any loss or profit for the seller.Problem Description − We need to find the total no. of products to be sold to make sure there is no loss.Let’s take an example to understand the problem, InputE = 2400, S ... Read More
In this problem, we are given a 2D array that denotes coordinates of three vertices of the triangle. Our task is to create a program to find the Centroid of the Triangle in C++.Centroid of a triangle is the point at which the three medians of the triangles intersect.Median of a triangle is the line that connects the vertex of the triangle with the center point of the line opposite to it.Let’s take an example to understand the problem, Input(-3, 1), (1.5, 0), (-3, -4)Output(-3.5, -1)ExplanationCentroid (x, y) = ((-3+2.5-3)/3, (1 + 0 - 4)/3) = (-3.5, -1)Solution ApproachFor solving ... Read More
In this problem, we are given four numbers that define the totalPrice and the ratio of coins of 1 Rs, 50 paise, 25 paise in the bag. Our task is to create a program to find the count of coins of each type from the given ratio in C++.Code description − Here, we need to use the coins of 1 Rs, 50 paise, and 25 paise from the bag to give the total sum of coins to the given total.Let’s take an example to understand the problem, InputTotalPrice = 225, 1Rs = 2, 50P = 3, 25P = 4Output1 Rs ... Read More
In this problem, we are given a number that denotes the number of vertices of a Wheel Graph. Our task is to create a Program to find the diameter, cycles and edges of a Wheel Graph in C++.Problem description − Here, we need to find the number of cycles, number of edges, and the diameter of Wheel Graph with n vertices.First, let’s understand some basics about Wheel Graph −A wheel graph is obtained from a cycle graph Cn-1 by adding a new vertex. That new vertex is called a Hub which is connected to all the vertices of Cn.Example of ... Read More
In this problem, we are given two numbers that define the marked price(M) and selling price(S) of a certain product. Our task is to create a program to find the Discount Percentage in C++.Discount is the amount that is deducted from the actual price (marked price) on a product.The formula for discount is, discount = marked price - selling priceDiscount percentage is the percentage of the price that is deducted from the actual price of the product.The formula for discount percentage is, discount percentage = (discount / marked price ) * 100Let’s take an example to understand the problem, Input240, ... Read More
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP