Maximum Sum of Distinct Numbers with LCM Equal to N in C++

Ayush Gupta
Updated on 15-Oct-2020 12:16:27

217 Views

In this problem, we are a number N. Our task is to create a program to find the Maximum sum of distinct numbers such that LCM of these numbers is N in C++.Problem DescriptionWe need to find the sum of all factors of the number N. And add all distinct to find the maximum sum.Let’s take an example to understand the problem, InputN = 12Output28ExplanationAll distinct factors of N are 1, 2, 3, 4, 6, 12. Sum = 1 + 2 + 3 + 4 + 6 + 12 = 28Solution ApproachA simple solution will be finding all the factors ... Read More

Maximum Sum of Elements from Each Row in the Matrix in C++

Ayush Gupta
Updated on 15-Oct-2020 12:11:37

960 Views

In this problem, we are given a two matrix mat[][]. Our task is to create a program to find the maximum sum of elements from each row in the matrix in C++.Problem DescriptionHere, we will find the maximum sum by taking one element from each row of the matrix in such a way that the element at the current row is greater than the last row element to be considered as a sum. We will find the maximum sum of elements that follows the above condition and print -1 if it is not possible.Let’s take an example to understand the ... Read More

Maximum Sum of Non-Adjacent Elements in C++

Ayush Gupta
Updated on 15-Oct-2020 12:07:39

267 Views

In this problem, we are given an array arr[]. Our task is to create a program to find the Maximum sum such that no two elements are adjacent in C++.Problem DescriptionWe need to find the maximum sum of the sequence from the array such that no 2 numbers from the sum sequence are adjacent in the array.Let’s take an example to understand the problem, Inputarr[] = {5, 1, 3, 7, 9, 2, 5}Output22ExplanationTaking sum sequence from index 0 with alternate elements : 5 + 3 + 9 + 5 = 22 Taking sum sequence from index 1 with alternate elements ... Read More

Maximum Sum Such That No Two Elements Are Adjacent in C++

Ayush Gupta
Updated on 15-Oct-2020 12:05:22

231 Views

In this problem, we are given an array arr[]. Our task is to create a program to find the Maximum sum such that no two elements are adjacent in C++.Problem DescriptionWe need to find the maximum sum of the sequence from the array such that no 2 numbers from the sum sequence are adjacent in the array.Let’s take an example to understand the problem, Inputarr[] = {5, 1, 3, 7, 9, 2, 5}Output22ExplanationTaking sum sequence from index 0 with alternate elements : 5 + 3 + 9 + 5 = 22 Taking sum sequence from index 1 with alternate elements ... Read More

Maximum Trace Possible for Any Sub-Matrix in C++

Ayush Gupta
Updated on 15-Oct-2020 12:00:47

385 Views

In this problem, we are given a two-dimensional array arr[][]. Our task is to create a program to find the Maximum trace possible for any sub-matrix of the given matrix in C++.Problem Descriptionwe need to find the maximum trace for any sub-matrix. The trace is the sum of elements of the main diagonal of the matrix.Let’s take an example to understand the problem, Inputarr[][] ={{-2, 5, 3},           {1, 6, 2},           {4, 3, 9}}Output15ExplanationFor the sub-array: {1, 6} {9, 3}Solution ApproachA simple solution is to find the max sum using the ... Read More

Maximum Trains for Which Stoppage Can Be Provided in C++

Ayush Gupta
Updated on 15-Oct-2020 11:56:44

339 Views

In this problem, we are given a number N denoting the number of platforms a station has with two tracks each. And T trains will be passing the station whose arrival and departure time are given. Each train stops at a specific station. Our task is to create a program to find the Maximum trains for which stoppage can be provided in C++.Let’s take an example to understand the problem, InputN = 3, T = 5 Trains = {{0915, 0930, 2}, {0930, 0945, 1}, {0930, 1200, 1}, {0910, 0925, 3}, {0940, 1015, 1}}Output4ExplanationThe train schedules are, Train 1: Train will ... Read More

Maximum Value in an Array After M Range Increment Operations in C++

Ayush Gupta
Updated on 15-Oct-2020 11:52:25

310 Views

In this problem, we are given an array arr[] of N elements initialized with 0. Our task is to create a program to find the Maximum value in an array after m range increment operations in C++.Problem DescriptionOn the array, we will perform m range increment operation of the type, update[L, R, K] = Add value K, to all elements in the range.After performing m operations on the array. We need to find the element with maximum value in the array.Let’s take an example to understand the problem, InputN = 6, m = 4 Update[][] = {{1, 4, 12}, {0, ... Read More

Implement Bucket Sort in JavaScript

AmitDiwan
Updated on 15-Oct-2020 09:37:17

698 Views

The Bucket Sort works by splitting the array of size n into k buckets which holds a specific range of element values.Then, these buckets are then sorted using a sorting algorithm which can be chosen based on the expected input size.We can describe this algorithm as follows −Algorithm:Create the initial bucketSort function Create variables for i, min, max, and bucket size Find min and max value Create amount of buckets Push values to correct buckets Sort bucketsExampleThe code for this will be −const arr = [32, 6, 34, 4, 78, 1, 6767, 4, 65, 34, 879, 7]; const bucketSort = ... Read More

Algorithm for Matrix Multiplication in JavaScript

AmitDiwan
Updated on 15-Oct-2020 09:34:04

2K+ Views

We are required to write a JavaScript function that takes in two 2-D arrays of numbers and returns their matrix multiplication result.Let’s write the code for this function −ExampleThe code for this will be −const multiplyMatrices = (a, b) => {    if (!Array.isArray(a) || !Array.isArray(b) || !a.length || !b.length) {       throw new Error('arguments should be in 2-dimensional array format');    }    let x = a.length,    z = a[0].length,    y = b[0].length;    if (b.length !== z) {       // XxZ & ZxY => XxY       throw new Error('number of ... Read More

Alternate Casing a String in JavaScript

AmitDiwan
Updated on 15-Oct-2020 09:31:56

1K+ Views

We are required to write a JavaScript function that takes in a string and constructs a new string with all the uppercase characters from the original string converted to lowercase and all the lowercase characters converted to uppercase from the original string.For example: If the string is −const str = 'The Case OF tHis StrinG Will Be FLiPped';OutputThen the output should be −const output = 'tHE cASE of ThIS sTRINg wILL bE flIpPED';ExampleThe code for this will be −const str = 'The Case OF tHis StrinG Will Be FLiPped'; const isUpperCase = char => char.charCodeAt(0) >= 65 && char.charCodeAt(0) char.charCodeAt(0) ... Read More

Advertisements