Maximum Sum of Nodes in Binary Tree (No Two Adjacent) - Dynamic Programming in C++

Ayush Gupta
Updated on 09-Sep-2020 13:08:04

151 Views

In this tutorial, we will be discussing a program to find maximum sum of nodes in Binary tree such that no two are adjacent using Dynamic Programming.For this we will be provided with a binary tree. Our task is to find the subset having maximum sum such that no two nodes in the subset are directly connected using Dynamic Programming.Example Live Demo#include using namespace std; //finding diameter using dynamic programming void dfs(int node, int parent, int dp1[], int dp2[], list* adj, int tree[]){    int sum1 = 0, sum2 = 0;    for (auto i = adj[node].begin(); i != adj[node].end(); ... Read More

Create New Array Without Impacting Values from Old Array in JavaScript

AmitDiwan
Updated on 09-Sep-2020 13:07:04

303 Views

Let’s say the following is our current array −var listOfNames=["John", "Mike", "Sam", "Carol"];Use JSON.parse(JSON.stringify()) to create new array and set values from the old array above.Examplefunction createNewArray(listOfNames) {    return JSON.parse(JSON.stringify(listOfNames)); } var listOfNames=["John", "Mike", "Sam", "Carol"]; var namesArray = listOfNames.slice(); console.log("The new Array="); console.log(namesArray);To run the above program, you need to use the following command −node fileName.js.Here, my file name is demo111.js.OutputThis will produce the following output −PS C:\Users\Amit\JavaScript-code> node demo111.js The new Array= [ 'John', 'Mike', 'Sam', 'Carol' ]Read More

Maximum Sum of Increasing Order Elements from N Arrays in C++

Ayush Gupta
Updated on 09-Sep-2020 13:05:50

194 Views

In this tutorial, we will be discussing a program to find maximum sum of increasing order elements from n arrays.For this we will be provided with N arrays of M size. Our task is to find the maximum sum by selecting one element from each array such that element from the previous array is smaller than the next one.Example Live Demo#include #define M 4 using namespace std; //calculating maximum sum by selecting //one element int maximumSum(int a[][M], int n) {    for (int i = 0; i < n; i++)       sort(a[i], a[i] + M);    int sum ... Read More

Display Office Status Based on Current Time with JavaScript Ternary Operator

AmitDiwan
Updated on 09-Sep-2020 13:04:42

375 Views

Let’s say, we are matching the current date and time with the business hours. We need to display whether the office is closed or open right now on the basis of current time.Get the hours from the current date and can use the ternary operator for close and open. Following is the code −Example Live Demo Document    const gettingHours = new Date().getHours()    const actualHours = (gettingHours >= 10 && gettingHours < 18) ? 'Open' : 'Closed';    document.querySelector('.closeOrOpened').innerHTML = actualHours; To run the above program, save ... Read More

Maximum Product of Indexes of Next Greater on Left and Right in C++

Ayush Gupta
Updated on 09-Sep-2020 13:03:39

197 Views

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

Count Elements of an Array Using Recursive Function in JavaScript

AmitDiwan
Updated on 09-Sep-2020 13:02:23

641 Views

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

Maximum Product of an Increasing Subsequence in C++

Ayush Gupta
Updated on 09-Sep-2020 13:01:04

126 Views

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

Get Random Value from a Range of Numbers in JavaScript

AmitDiwan
Updated on 09-Sep-2020 13:00:42

276 Views

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

Check If a String Has White Space in JavaScript

AmitDiwan
Updated on 09-Sep-2020 12:59:13

4K+ Views

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

Maximum Product of an Increasing Subsequence of Size 3 in C++

Ayush Gupta
Updated on 09-Sep-2020 12:59:02

156 Views

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

Advertisements