Server Side Programming Articles - Page 1945 of 2650

Difference between strlen() and sizeof() for string in C Program

Mahesh Parahar
Updated on 25-Feb-2020 06:57:24

330 Views

As we know that in programming string can be defined as the collection of characters. Now for the requirement of finding how many characters are being used to create a string, C provides two approaches which are strlen() and sizeof().As mentioned in above point both of these methods are used to find out the length of target operand but on the basis of their internal implementation following are some basic differences between both.Sr. No.Keystrlen()sizeof()1Definitionstrlen() is a predefined function defined in a Header file named string.h in C.On other hand sizeof() is a Unary operator and not a predefined function.2Implementationstrlen is ... Read More

Maximum path sum in an Inverted triangle in C++

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

200 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

384 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

304 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

219 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

312 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

200 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

346 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

363 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

Maximum number of 3-person teams formed from two groups in C++

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

1K+ Views

In this problem, we are given two integers N and M, N is the number of people in group 1, and M is the number of people in group 2. Our task is to create a program to find the maximum number of 3-person teams formed from two groups.We will be creating teams of 3 people by selecting a person from these groups such that the maximum teams can be made. Each team must have at least one person from each group.Let’s take an example to understand the problem, Input − N = 5, M = 3Output − 2Explanation −The ... Read More

Advertisements