We are required to write a JavaScript function that takes in two arrays of numbers, let’s say arr1 and arr2. The function should find the intersection between the elements of the array. i.e., the elements that appear in both the arrays.The only condition is that if we encountered one element before as intersected, we should not consider it again even if appears again in both the arrays.For example −If the input arrays are −const arr1 = [1, 5, 7, 3, 1]; const arr2 = [1, 7, 3, 1, 6];Then the output array should be −const output = [1, 3, 7];However, ... Read More
We are required to write a JavaScript function that takes in an array of Numbers. The function should construct a new array based on the original array. Each corresponding element of the new array should be the product of all the elements of the original array including that element.For example −If the input array is −const arr = [1, 2, 3, 4, 5];Then the output array should be −const output = [120, 60, 40, 30, 24];We have to achieve this in linear time and constant space (obviously excluding the space used up in constructing the new array).ExampleFollowing is the code ... Read More
We are required to write a JavaScript function that takes in an array of Numbers as the first argument and a single number as the second argument.The function should find and return that number from the array which is closest to the number specified by the second argument.For example −const arr = [34, 67, 31, 53, 89, 12, 4]; const num = 41;Then the output should be 34.ExampleFollowing is the code −const arr = [34, 67, 31, 53, 89, 12, 4]; const num = 41; const findClosest = (arr = [], num) => { let curr = arr[0]; ... Read More
In this problem, we are given an array arr[] and a number M. Our task is to create a program to calculate the Maximum Weight Difference in C++.Problem descriptionWe will find M elements from the array such that the absolute difference between the sum and the sum of the rest elements is maximum.Let’s take an example to understand the problem, Inputarr[] = {3, 1, 6, 9, 4} M = 3Output15ExplanationWe will consider 4, 6, 9. The sum is 19. The absolute difference with the sum of rest numbers is|19 − 4| = 15Solution ApproachA simple solution to the problem is ... Read More
In this problem, we are given an array arr[] of size n consisting of positive values. Our task is to create a program to find the maximum subsequence sum in such a way that no two consecutive elements of the array.Problem Description − We need to find the sum of subarray which has elements of the array but no two adjacent elements of the array can be taken into consideration.ExampleLet’s take an example to understand the problem, Inputarr[] = {5, 2, 1, 9, 6}OutputExplanation −Subarray sum are : {5, 1, 6}, sum = 5 + 1 + 6 = 12 ... Read More
In this problem, we are given an array arr[] of size n and a number k. Our task is to create a program to find the maximum sum subsequence with atleast k distant elements.Problem Description − We need to find the sum of subarrays such that the elements of the subarray are taken from the array whose index is at k distance and the sum is maximized.Let’s take an example to understand the problem, Inputarr[] = {2, 3, 7, 9, 2, 8, 3}Output15ExplanationAll subsequences that satisfy the given condition, {2, 9, 3}, Sum = 14 {3, 2}, Sum = 5 ... Read More
In this problem, we are given an array arr[] of size n consisting of positive values. Our task is to create a program to find the maximum sum subarray such that start and end values are the same.Problem Description − Here, we need to find a subarray such that the elements at index i (starting index of subarray) and j (ending index of subarray) are the same i.e. arr[i] = arr[j]. And the sum of elements of the subarray is maximized.Let’s take an example to understand the problem, Inputarr[] = {2, 1, 3, 5, 6, 2, 4, 3}Output23ExplanationAll subarrays which ... Read More
In this problem, we are given an array arr[] of size n and an integer k. Our task is to create a program to find the maximum sum possible for a subsequence such that no two elements appear at a distance < K in the array.Problem Description − We need to find the maximum sum of sub−seqeunce that considers elements that are k distance from each other.Let’s take an example to understand the problem, Inputarr[] = {6, 2, 5, 1, 9, 11, 4} k = 2Output16ExplanationAll possible sub−sequences of elements that differ by k or more. {6, 1, 4}, sum ... Read More
In this problem, we are given a matrix mat[][] of size nXm. Our task is to create a program to find the maximum sum path in a matrix from top to bottom and back.Problem Description − We need to find the maximum path sum from topleft to bottom−right and then back.Valid movesFrom mat[0][0] to mat[n−1][m−1]: Right (mat[i][j] to mat[i][j+1]) and Down (mat[i][j] to mat[i+1][j]). From mat[n−1][m−1] to mat[0][0]: left (mat[i][j] to mat[i][j−1]) and up (mat[i][j] to mat[i−1][j]).One important thing is that both paths cannot be the same. There should be one or more elements different in both paths.Let’s take an ... Read More
In this problem, we are given two arrays arr1[] and arr2[] of size n. Our task is to create a program to find the maximum Sum of Products of Two Array.Problem Description − We need to find the maximum sum of products of two arrays. We need to find the maximum sum of the product of one element from arr1 and other elements from arr2.Let’s take an example to understand the problem, Inputarr1[] = {3, 5, 6} arr2[] = {1, 4, 2}Output37ExplanationMaximum sum of products: 6*4 + 5*2 + 3*1 = 24 + 10 + 3 = 37Solution ApproachA simple ... Read More