Found 7347 Articles for C++

Convert all substrings of length ‘k’ from base ‘b’ to decimal in C++

Ayush Gupta
Updated on 16-Jan-2020 06:45:26

67 Views

In this tutorial, we will be discussing a program to convert all substrings of length ‘k’ from base ‘b’ to decimal.For this we will be provided with a string of some certain length. Our task is to take the substrings from the given string of size ‘k’ and get it converted into the decimal numbers from being in base ‘b’.Example Live Demo#include using namespace std; //converting the substrings to decimals int convert_substrings(string str, int k, int b){    for (int i=0; i + k = 0; i--){          sum = sum + ((sub.at(i) - '0') * pow(b, ... Read More

Difference between float and double in C/C++

Mahesh Parahar
Updated on 24-Feb-2020 08:04:39

2K+ Views

As we know that in C/C++ we require float and double data type for the representation of Floating point numbers i.e the numbers which have decimal part with them.Now on the basis of precision provided by both of these data types we can differentiate between both of them.In simple words it could be state that double has 2x more precision as compare than float which means that double data type has double precision than as compare to that of float data type.In terms of number of precision it can be stated as double has 64 bit precision for floating point ... Read More

Maximum path sum in an Inverted triangle in C++

Narendra Kumar
Updated on 03-Jun-2020 08:27:16

105 Views

In this problem, we are given numbers in the form of an inverted triangle. Our task is to create a program that will find the maximum path sum in an inverted triangle.Inverted triangle form of number is an arrangement when the first row contains n elements, second n-1, and so on.Here, we have to find the maximum sum that can 3 be obtained by adding one element from each row.Let’s take an example to understand the problem −Input  −5 1 9  3 6   2Output − 17Explanation − Here, I have found the path from the last row to the ... Read More

Maximum path sum in a triangle in C++

Narendra Kumar
Updated on 03-Jun-2020 08:29:07

266 Views

In this problem, we are given numbers that are in the form of a triangle. Our task is to create a program that will find the maximum path sum in a triangle.The elements are arranged starting from the 1st row with 1 one element and then next rows with an increasing number of elements till there are elements in the nth row.So, the program will find the path that will provide the maximum sum of elements in the triangle. So, we have to find the path that will provide the maximum sum.Let’s take an example to understand the problem −Input ... Read More

Maximum Path Sum in a Binary Tree in C++

Narendra Kumar
Updated on 03-Jun-2020 08:35:04

202 Views

In this problem, we are given a binary tree with each node containing a value. Our task is to create a program to find the maximum path sum between two leaves of a binary tree.Here, we have to find the path form one leaf node to another leaf node that will provide the maximum sum of values. This maximum sum path can/cannot include the root node.Binary Tree is a tree data structure in which each node can have a maximum of two child nodes. These are called a left child and right child.Example −Let’s take an example to understand the ... Read More

Maximum OR sum of sub-arrays of two different arrays in C++

Narendra Kumar
Updated on 10-Jan-2020 08:04:03

142 Views

Problem statementGiven two arrays of positive integers. Select two sub-arrays of equal size from each array and calculate maximum possible OR sum of the two sub-arrays.ExampleIf arr1[] = {1, 2, 4, 3, 2} andArr2[] = {1, 3, 3, 12, 2} then maximum result is obtained when we create following two subarrays −Subarr1[] = {2, 4, 3} andSubarr2[] = {3, 3, 12}AlgorithmWe can use below formula to gets result −f(a, 1, n) + f(b, 1, n)Example Live Demo#include using namespace std; int getMaximumSum(int *arr1, int *arr2, int n) {    int sum1 = 0;    int sum2 = 0;    for ... Read More

Maximum number of Unique integers in Sub- Array of given sizes in C++

Narendra Kumar
Updated on 03-Jun-2020 08:38:55

221 Views

In this problem, we are given an array of size n and a number M. Our task is to create a program that will find the maximum number of unique integers in Sub-array of given size.Here, we will have to find the sub-array of size M that has the maximum number of unique elements.Let’s take an example to understand the problem, Input − array = {4, 1, 2, 1 , 4, 3}. M = 4Output − 4Explanation −All possible combinations of sub-arrays of size 4. {4, 1, 2, 1} = 3 unique elements {1, 2, 1, 4} = 3 unique ... Read More

Maximum number of fixed points using at most 1 swaps in C++

Narendra Kumar
Updated on 10-Jan-2020 07:58:37

94 Views

Problem statementGiven a permutation of N elements from 0 to N-1. A fixed point is an index at which the value is same as the index i.e. arr[i] = i. You are allowed to make at most 1 swap. Find the maximum number of fixed points that you can get.ExampleIf input array is {0, 1, 2, 3, 4, 6, 5} then answer is 7.To adjust fixed point, we have to swap 6 and 5After this entire array becomes fixed point and maximum value of fixed point is 7.AlgorithmCreate an array pos which keeps the position of each element in the ... Read More

Maximum number of edges to be added to a tree so that it stays a Bipartite graph in C++

Narendra Kumar
Updated on 10-Jan-2020 07:56:14

253 Views

Problem statementA tree is always a Bipartite Graph as we can always break into two disjoint sets with alternate levels.In other words, we always color it with two colors such that alternate levels have same color. The task is to compute the maximum no. of edges that can be added to the tree so that it remains Bipartite Graph.ExampleTree edges are represented in vertex pairs as follows −{1, 2}{1, 3}{2, 4}{3, 5} then we need 2 more edges to keep it Bipartite GraphIn a coloring graph the graph {1, 4, 5} and {2, 3} from two different sets. Since, 1 ... Read More

Maximum number of edges in Bipartite graph in C++

Narendra Kumar
Updated on 10-Jan-2020 07:53:40

266 Views

Problem statementGiven an integer N which represents the number of Vertices. The Task is to find the maximum number of edges possible in a Bipartite graph of N vertices.Bipartite GraphA Bipartite graph is one which is having 2 sets of vertices.The set are such that the vertices in the same set will never share an edge between them.ExampleIf N = 10 then there will be total 25 edges −Both sets will contain 5 vertices and every vertex of first set will have an edge to every other vertex of the second setHence total edges will be 5 * 5 = ... Read More

Advertisements