Maximum Sum of Products of Two Arrays in C++

Ayush Gupta
Updated on 09-Sep-2020 13:18:27

233 Views

In this tutorial, we will be discussing a program to find maximum Sum of Products of Two Arrays.For this we will be provided with two arrays of same size. Our task is to find the maximum sum by multiplying exactly one element from first element with one element from the second array.Example Live Demo#include using namespace std; //calculating maximum sum by //multiplying elements int maximumSOP(int *a, int *b) {    int sop = 0;    int n = sizeof(a)/sizeof(a[0]);    sort(a,a+n+1);    sort(b,b+n+1);    for (int i = 0; i

Creating an Associative Array in JavaScript

AmitDiwan
Updated on 09-Sep-2020 13:17:38

1K+ Views

You can create an associative array in JavaScript using an array of objects with key and value pair.Associative arrays are basically objects in JavaScript where indexes are replaced by user defined keys.Examplevar customerDetails= [    {       "customerId":"customer-1",       "customerName":"David",       "customerCountryName":"US"    },    {       "customerId":"customer-2",       "customerName":"Bob",       "customerCountryName":"UK"    },    {       "customerId":"customer-3",       "customerName":"Carol",       "customerCountryName":"AUS"    } ] for(var i=0;i node demo115.js David Bob Carol

Maximum Sum of Pairwise Product in an Array with Negatives in C++

Ayush Gupta
Updated on 09-Sep-2020 13:16:30

207 Views

In this tutorial, we will be discussing a program to find maximum sum of pairwise product in an array with negative allowed.For this we will be provided with an array containing integers. Our task is to find the maximum sum while performing pairwise multiplications.Example Live Demo#include #define Mod 1000000007 using namespace std; //finding the maximum sum long long int findSum(int arr[], int n) {    long long int sum = 0;    //sorting the array    sort(arr, arr + n);    int i = 0;    while (i < n && arr[i] < 0) {       if (i ... Read More

Display JavaScript Array Items One at a Time in Reverse on Button Click

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

792 Views

Let’s say the following is our array −var listOfNames = [    'John',    'David',    'Bob' ];Following is our button −Click The Button To get the Reverse ValueNow, for array items in reverse, at first, reach the array length and decrement the length by 1. Then, print the contents of the particular index in reverse order.Example Live Demo Document Click The Button To get the Reverse Value    var listOfNames = [       'John',       'David',       'Bob'    ];    count=listOfNames.length-1;    function reverseTheArray(){       document.getElementById('reverseTheArray').innerHTML =       listOfNames[count];       count--;       if (count

Maximum Sum of Pairs with Specific Difference in C++

Ayush Gupta
Updated on 09-Sep-2020 13:13:07

188 Views

In this tutorial, we will be discussing a program to find maximum sum of pairs with specific difference.For this we will be provided with an array containing integers and a value K. Our task is to pair elements having difference less than K and finally find the maximum sum of the elements in disjoint sets.Example Live Demo#include using namespace std; //returning maximum sum of disjoint pairs int maxSumPairWithDifferenceLessThanK(int arr[], int N, int K){    sort(arr, arr+N);    int dp[N];    dp[0] = 0;    for (int i = 1; i < N; i++) {       dp[i] = dp[i-1]; ... Read More

Assign Multiple Variables to the Same Value in JavaScript

AmitDiwan
Updated on 09-Sep-2020 13:10:44

4K+ Views

To assign multiple variables to the same value, the syntax is as followsvar anyVariableName1, anyVariableName2, anyVariableName3……….N; yourVariableName1=yourVariableName2=yourVariableName3=.........N=yourValue;Let’s say the following are our variables and we are assigning same value −var first, second, third, fourth, fifth; first=second=third=fourth=fifth=100;Examplevar first, second, third, fourth, fifth; first=second=third=fourth=fifth=100; console.log(first); console.log(second); console.log(third); console.log(fourth); console.log(fifth); console.log("The sum of all values="+(first+second+third+fourth+fifth));To run the above program, you need to use the following command −node fileName.js.Here, my file name is demo114.js.OutputThis will produce the following output −PS C:\Users\Amit\JavaScript-code> node demo114.js 100 100 100 100 100 The sum of all values=500Read More

Maximum Sum of Nodes in Binary Tree Such That No Two Are Adjacent in C++

Ayush Gupta
Updated on 09-Sep-2020 13:10:07

243 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.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 subset are directly connected.Example Live Demo#include using namespace std; //binary tree node structure struct node {    int data;    struct node *left, *right; }; struct node* newNode(int data) {    struct node *temp = new struct node;    temp->data = data;    temp->left = temp->right = NULL;    return temp; ... Read More

Create JavaScript Object from Single Array and Define Key-Value

AmitDiwan
Updated on 09-Sep-2020 13:09:40

158 Views

To convert a JavaScript object to key value, you need to use Object.entries() along with map(). Following is the code −Examplevar studentObject={    101: "John",    102: "David",    103: "Bob" } var studentDetails = Object.assign({}, studentObject) studentDetails = Object.entries(studentObject).map(([studentId,studentName])=>({studentId ,studentName})); console.log(studentDetails);To run the above program, you need to use the following command −node fileName.js.Here, my file name is demo113.js.OutputThis will produce the following output −PS C:\Users\Amit\JavaScript-code> node demo113.js [    { studentId: '101', studentName: 'John' },    { studentId: '102', studentName: 'David' },    { studentId: '103', studentName: 'Bob' } ]

Formatting Text to Add New Lines in JavaScript

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

383 Views

For this, use map() along with join(‘’).Then ‘’ is for new line. Following is the code −ExamplestudentDetails = [    [101, 'John', 'JavaScript'],    [102, 'Bob', 'MySQL'] ]; var studentFormat = '||Id||Name||subjName||'; var seperate = ''; seperate = seperate + studentDetails.map(obj => `|${obj.join('|')}|`).join(''); studentFormat = studentFormat + seperate; console.log(studentFormat);To run the above program, you need to use the following command −node fileName.js.Here, my file name is demo112.js.OutputThis will produce the following output −PS C:\Users\Amit\JavaScript-code> node demo112.js ||Id||Name||subjName|| |101|John|JavaScript| |102|Bob|MySQL|

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

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

138 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

Advertisements