In this tutorial, we will be discussing a program to find maximum product of indexes of next greater on left and right.For this we will be provided with an array of integers. Our task is to find the element with maximum Left-Right product (L(i)*R(i) where L(i) is closest index on left side and greater than current element and R(i) is closest index on right side and greater than current element).Example Live Demo#include using namespace std; #define MAX 1000 //finding greater element on left side vector nextGreaterInLeft(int a[], int n) { vector left_index(MAX, 0); stack s; for (int ... Read More
The recursive function calls itself with some base condition. Let’s say the following is our array with marks −var listOfMarks=[56, 78, 90, 94, 91, 82, 77];Following is the code to get the count of array elements −Examplefunction countNumberOfElementsUsingRecursive(listOfMarks) { if (listOfMarks.length == 0) { return 0; } return 1 + countNumberOfElementsUsingRecursive(listOfMarks.slice(1)); } var listOfMarks=[56, 78, 90, 94, 91, 82, 77]; console.log("The array="); console.log(listOfMarks); var numberOfElements=countNumberOfElementsUsingRecursive(listOfMarks); console.log("The Number of elements = "+numberOfElements);To run the above program, you need to use the following command −node fileName.js.Here, my file name is demo110.js.OutputThis will produce the following ... Read More
In this tutorial, we will be discussing a program to find maximum product of an increasing subsequence.For this we will be provided with an array of integers. Our task is to find the maximum product of any subsequence of the array with any number of elements.Example Live Demo#include #define ll long long int using namespace std; //returning maximum product ll lis(ll arr[], ll n) { ll mpis[n]; //initiating values for (int i = 0; i < n; i++) mpis[i] = arr[i]; for (int i = 1; i < n; i++) ... Read More
Let’s say, first, we will set the start and end range and call the function:console.log(getRandomValueBetweenTwoValues(400, 480))We have passed start value 400 and end value 480. Let’s get the random value with Math.random() in JavaScript −Examplefunction getRandomValueBetweenTwoValues(startRange, endRange) { return Math.floor(Math.random() * (endRange - startRange + 1) + startRange); } console.log(getRandomValueBetweenTwoValues(400, 480))To run the above program, you need to use the following command −node fileName.js.Here, my file name is demo109.js.OutputThis will produce the following output −PS C:\Users\Amit\JavaScript-code> node demo109.js 401
To check whitespace in a string, use the concept of indexOf(‘ ’). Following is the code −Examplefunction stringHasTheWhiteSpaceOrNot(value){ return value.indexOf(' ') >= 0; } var whiteSpace=stringHasTheWhiteSpaceOrNot("MyNameis John"); if(whiteSpace==true){ console.log("The string has whitespace"); } else { console.log("The string does not have whitespace"); }To run the above program, you need to use the following command −node fileName.js.Here, my file name is demo108.js.OutputThis will produce the following output −PS C:\Users\Amit\JavaScript-code> node demo108.js The string has whitespace
In this tutorial, we will be discussing a program to find maximum product of an increasing subsequence of size 3.For this we will be provided with an array of positive integers. Our task is to find a subsequence of three elements with the maximum product.Example#include using namespace std; //returning maximum product of subsequence long long int maxProduct(int arr[] , int n) { int smaller[n]; smaller[0] = -1 ; setS ; for (int i = 0; i < n ; i++) { auto j = S.insert(arr[i]); auto itc = j.first; ... Read More
You can use match() with passing the variable name. The syntax is as follows −console.log(yourVariableName.match(yourMatchingVariableName));Examplevar senetence = 'My Name is John'; console.log("The actual value="); console.log(senetence); var matchWord = 'John'; console.log("The matching value="); console.log(matchWord); var matchRegularExpression = new RegExp(matchWord, 'g' ); console.log(senetence.match(matchWord));To run the above program, you need to use the following command −node fileName.js.Here, my file name is demo107.js.OutputThis will produce the following output −PS C:\Users\Amit\JavaScript-code> node demo107.js The actual value= My Name is John The matching value= JohnRead More
In this tutorial, we will be discussing a program to find maximum product of a triplet (subsequence of size 3) in array.For this we will be provided with an array of integers. Our task is to find the triplet of elements in that array with the maximum productExample Live Demo#include using namespace std; //finding the maximum product int maxProduct(int arr[], int n){ if (n < 3) return -1; int max_product = INT_MIN; for (int i = 0; i < n - 2; i++) for (int j = i + 1; j ... Read More
Let’s say we have the following records of studentId and studentName and want to check a specific student name −const studentDetails= [ { studentId:101, studentName:"John" }, { studentId:102, studentName:"David" }, { studentId:103, studentName:"Carol" } ]Create a custom function to find by name. Following is the code −Exampleconst studentDetails= [ { studentId:101, studentName:"John" }, { studentId:102, studentName:"David" }, { studentId:103, studentName:"Carol" } ] function findByName(name){ var flag=true; for(var i=0;i node demo106.js The name found=David
In this tutorial, we will be discussing a program to find maximum product of 4 adjacent elements in matrix.For this we will be provided with a square matrix. Our task is to find the maximum product of four adjacent elements which can be top, down, right, left, or diagonal.Example Live Demo#include using namespace std; const int n = 5; //finding maximum product int FindMaxProduct(int arr[][n], int n) { int max = 0, result; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { ... Read More