In this problem, we are given an array arr[]. Our task is to create a program to find the Maximum product quadruple (sub-sequence of size 4) in array in C++.Code description − Here, we need to find a quadruple (sub-sequence of size 4) such that the product of all elements is maximum.Let’s take an example to understand the problem, Inputarr[] = {4, -2, 5, -6, 8}Output840ExplanationThe quadruple, (-3, 5, -7, 8) with product 840.Solution ApproachThere can be multiple solutions to a given problem.One simple solution is using the direct method by traversing the array. Then finding all possible quadruples in ... Read More
In this problem, we are given an array arr[] of integers. Our task is to create a program to find the Maximum Product Subarray - Using Two Traversals in C++.Problem description − Here in the array, we will find the maximum product subarray using two traversals one from index 0 and another index (n-1).Let’s take an example to understand the problem, Inputarr[] = {4, -2, 5, -6, 0, 8}Output240ExampleSubarray = {4, -2, 5, -6} Maximum product = 4 * (-2) * 5 * (-6) = 240Solution ApproachTo solve this problem using two traversals. Here, we will find the maximum product ... Read More
In this problem, we are given an array of integers(positive as well as negative). Our task is to create a program to calculate the Maximum Product Subarray in C++.Problem Solution − Here, we have an array that contains positive, negative, and zero numbers. We need to find the product of subarrays created by elements of the array. And maximize the product of the subarray.Let’s take an example to understand the problem, Inputarr[] = {-1, 2, -7, -5, 12, 6}Output5040ExplanationThe subarray with the maximum product is {2, -7, -5, 12, 6}Product = 5040Solution ApproachTo solve this problem, we are given an ... Read More
In this problem, we are given an array stkprice[] that denotes the price of a certain stock on i-th day. Our task is to create a program to calculate Maximum profit after buying and selling the stocks in C++.Problem Description − Here, we need to check when can be bought and sell the stock to gain profit. To gain profit, we need to buy the stock at a low price and sell it when the price goes up. And repeat the same when the drop is encountered again.Let’s take an example to understand the problem, Inputstkprice[] = {120, 310, 405, ... Read More
In this problem, we are given a 2-D array that contains rational numbers (one in each row). Our task is to create a program to calculate the maximum rational number (or fraction) from an array in C++.Problem Description − The 2-D array is of the form [n][2]. Each row has two integer values that denote the value of a and b in the equation of rational number, a/b. We need to find the greatest number out of all these rational numbers.Let’s take an example to understand the problem, Inputrat[][] = { {3, 2}, {5, 7}, {1, 9}, ... Read More
In this problem, we are given a 2-D array arr[] consisting of boolean values (i.e 0’s and 1’s) and an integer K. Our task is to create a program to find the Maximum score after flipping a Binary Matrix atmost K times in C++.Problem Description − Here, for the 2-D array, and the k moves, we need to find the number that is created by the elements of the array. In each move, we will take a row or column and flip all the elements of the row or column. The choice will be done to keep in mind that ... Read More
In this problem, we are given an array arr[] of integers. Our task is to create a program to calculate the Maximum set bit sum in array without considering adjacent elements in C++.Problem description − Here, we have an array arr[]. We have to find the number of set bits for each number. Then, we will find the maximum set bit sum in adjacent elements of the array. i.e. maximum sum for a[i] + a[i+2] ….Let’s take an example to understand the problem, Inputarr[] = {1, 4, 6, 7}Output4ExplanationArray with the element’s in binary formarr[] = {01, 100, 101, 111} ... Read More
Suppose, we have a sorted array of literals like this −const arr = [2, 2, 3, 3, 3, 5, 5, 6, 7, 8, 9];We are required to write a JavaScript function that takes in one such array and returns the first number that appears only once in the array. If there is no such number in the array, we should return false.For this array, the output should be 6ExampleFollowing is the code −const arr = [2, 2, 3, 3, 3, 5, 5, 6, 7, 8, 9]; const firstNonDuplicate = arr => { let appeared = false; for(let i ... Read More
We are required to write a JavaScript function that takes in a Number, say n, and we are required to check whether there exist such three consecutive natural numbers (not decimal/floating point) whose sum equals to n.If there exist such numbers, our function should return them, otherwise it should return false. Following is the code −Exampleconst sum = 54; const threeConsecutiveSum = sum => { if(sum < 6 || sum % 3 !== 0){ return false; } // three numbers will be of the form: // x + x + 1 + x ... Read More
We are required to write a JavaScript function that takes in a lowercase string and sorts it in the reverse order i.e., b should come before a, c before b and so on.For example: If the input string is −const str = "hello";Then the output should be −const output = "ollhe";ExampleFollowing is the code −const string = 'hello'; const sorter = (a, b) => { const legend = [-1, 0, 1]; return legend[+(a < b)]; } const reverseSort = str => { const strArr = str.split(""); return strArr .sort(sorter) .join(""); }; console.log(reverseSort(string));OutputFollowing is the output in the console −ollhe