Found 7347 Articles for C++

Program to count number of queries that are true in a graph with weighted path in C++

Arnab Chakraborty
Updated on 12-Dec-2020 09:35:09

56 Views

Suppose we have an edge list for an undirected graph where each edge has [u, v, w] fields, u and v are source and destination vertices and w is the weight. And also have a list of queries of the same form [u, v, w]. That represents the question of does there exist a path between u and v such that each edge in the path have weight of at most w. So find the number of queries that are true.So, if the input is like edges = [[0, 1, 6], [1, 2, 7], [2, 3, 8], [0, 3, 5]] ... Read More

Program to check whether first player can win a game where players can form string char by char in C++

Arnab Chakraborty
Updated on 12-Dec-2020 09:32:10

135 Views

Suppose we have a list of words. Now consider a ghost game where two players can participate into it. Here players alternate appending letters to a string. And the string that is being made must be a valid prefix of a word in the list, and the player who spells out any word in the list loses. We have to check whether the first player can win or not if both players are playing optimally.So, if the input is like words = ["manage", "manager", "min"], then the output will be True, as they can play like −m [Player 1]ma [Player ... Read More

Program to get operations to convert one string to another in C++

Arnab Chakraborty
Updated on 12-Dec-2020 09:22:27

151 Views

Suppose we have two strings S and T. We have to find the shortest sequence of operations that changes S to T. Here the operations are basically either deleting or inserting a character.So, if the input is like S = "xxxy" T = "xxyy", then the output will be ["x", "x", "-x", "y", "+y"], this means place first two x's, then remove 3rd x, then place y then add a new y.To solve this, we will follow these steps −make a table dp of size 505 x 505Define a function help(), this will take i, j, S, T, if i ... Read More

Program to show decimal number from rational number representation in C++

Arnab Chakraborty
Updated on 12-Dec-2020 08:54:06

316 Views

Suppose we have two numbers called numerator and denominator representing a rational number in the form (numerator / denominator). We have to find its decimal representation as a string. If there are some recurring numbers, then surround them with brackets.So, if the input is like numerator = 164 denominator = 3, then the output will be "54.(6)".To solve this, we will follow these steps −if numerator is same as 0, then −return "0"Define an array ansif numerator < 0 and denominator > 0 or numerator > 0 and denominator < 0, then −insert '-' at the end of ansdivisor := ... Read More

Maximum Weight Difference in C++ Program

sudhir sharma
Updated on 09-Dec-2020 13:47:35

180 Views

In this problem, we are given an array arr[] and a number M. Our task is to create a program to calculate the Maximum Weight Difference in C++.Problem descriptionWe will find M elements from the array such that the absolute difference between the sum and the sum of the rest elements is maximum.Let’s take an example to understand the problem, Inputarr[] = {3, 1, 6, 9, 4} M = 3Output15ExplanationWe will consider 4, 6, 9. The sum is 19. The absolute difference with the sum of rest numbers is|19 − 4| = 15Solution ApproachA simple solution to the problem is ... Read More

Maximum sum such that no two elements are adjacent Alternate Method in C++ program

sudhir sharma
Updated on 09-Dec-2020 13:45:22

307 Views

In this problem, we are given an array arr[] of size n consisting of positive values. Our task is to create a program to find the maximum subsequence sum in such a way that no two consecutive elements of the array.Problem Description − We need to find the sum of subarray which has elements of the array but no two adjacent elements of the array can be taken into consideration.ExampleLet’s take an example to understand the problem, Inputarr[] = {5, 2, 1, 9, 6}OutputExplanation −Subarray sum are : {5, 1, 6}, sum = 5 + 1 + 6 = 12 ... Read More

Maximum sum subsequence with at-least k distant elements in C++ program

sudhir sharma
Updated on 09-Dec-2020 13:43:39

231 Views

In this problem, we are given an array arr[] of size n and a number k. Our task is to create a program to find the maximum sum subsequence with atleast k distant elements.Problem Description − We need to find the sum of subarrays such that the elements of the subarray are taken from the array whose index is at k distance and the sum is maximized.Let’s take an example to understand the problem, Inputarr[] = {2, 3, 7, 9, 2, 8, 3}Output15ExplanationAll subsequences that satisfy the given condition, {2, 9, 3}, Sum = 14 {3, 2}, Sum = 5 ... Read More

Maximum sum subarray such that start and end values are same in C++ Program

sudhir sharma
Updated on 09-Dec-2020 13:41:11

354 Views

In this problem, we are given an array arr[] of size n consisting of positive values. Our task is to create a program to find the maximum sum subarray such that start and end values are the same.Problem Description − Here, we need to find a subarray such that the elements at index i (starting index of subarray) and j (ending index of subarray) are the same i.e. arr[i] = arr[j]. And the sum of elements of the subarray is maximized.Let’s take an example to understand the problem, Inputarr[] = {2, 1, 3, 5, 6, 2, 4, 3}Output23ExplanationAll subarrays which ... Read More

Maximum sum possible for a sub-sequence such that no two elements appear at a distance < K in the array in C++ program

sudhir sharma
Updated on 09-Dec-2020 13:38:21

218 Views

In this problem, we are given an array arr[] of size n and an integer k. Our task is to create a program to find the maximum sum possible for a subsequence such that no two elements appear at a distance < K in the array.Problem Description − We need to find the maximum sum of sub−seqeunce that considers elements that are k distance from each other.Let’s take an example to understand the problem, Inputarr[] = {6, 2, 5, 1, 9, 11, 4} k = 2Output16ExplanationAll possible sub−sequences of elements that differ by k or more. {6, 1, 4}, sum ... Read More

Maximum sum path in a matrix from top to bottom and back in C++ Program

sudhir sharma
Updated on 09-Dec-2020 13:36:20

201 Views

In this problem, we are given a matrix mat[][] of size nXm. Our task is to create a program to find the maximum sum path in a matrix from top to bottom and back.Problem Description − We need to find the maximum path sum from topleft to bottom−right and then back.Valid movesFrom mat[0][0] to mat[n−1][m−1]: Right (mat[i][j] to mat[i][j+1]) and Down (mat[i][j] to mat[i+1][j]). From mat[n−1][m−1] to mat[0][0]: left (mat[i][j] to mat[i][j−1]) and up (mat[i][j] to mat[i−1][j]).One important thing is that both paths cannot be the same. There should be one or more elements different in both paths.Let’s take an ... Read More

Advertisements