Found 7347 Articles for C++

Maximum edge removal from tree to make even forest in C++

Narendra Kumar
Updated on 31-Dec-2019 12:28:10

306 Views

Problem statementGiven an undirected tree which has even number of vertices, we need to remove the maximum number of edges from this tree such that each connected component of the resultant forest has an even number of vertices.ExampleIn above shown tree, we can remove at max 2 edges 0-2 and 0-4 shown in red such that each connected component will have even number of vertices.AlgorithmDo DFS from any starting node as tree is connectedInitialize count of nodes in subtree rooted under current node as 0Do following recursively for every subtree of current node −If size of current subtree is even, ... Read More

Maximum determinant of a matrix with every values either 0 or n in C++

Narendra Kumar
Updated on 31-Dec-2019 12:22:55

82 Views

Problem statementWe have given a positive number n, and we have to find a 3*3 matrix which can be formed with combination of 0 or n and has maximum determinants.ExampleIf n = 15 then we can create matrix as follows −{{15, 15, 0}{0, 15, 15}{15, 0, 0}}For any 3*3 matrix having elements either 0 or n, the maximum possible determinant is 2 *(n)3. Hence answer is −2 * (15)3 = 6750AlgorithmFor any 3*3 matrix having elements either 0 or n, the maximum possible determinant is 2 *(n)3ExampleLet us now see an example − Live Demo#include using namespace std; int getMaxDeterminant(int ... Read More

Maximum Bitwise AND pair from given range in C++

Narendra Kumar
Updated on 31-Dec-2019 12:19:58

85 Views

Problem statementGiven a range [L, R], the task is to find a pair (X, Y) such that L ≤ X < Y ≤ R and X & Y is maximum among all the possible pairs then print the bitwise AND of the found pair.ExampleIf L = 1 and R = 10 then maximum bitwise AND value is 8 which can be formed as follows −1000 # Binary representation of 8 Bitwise AND 1001 # Binary representation of 9 ---- 1000 # Final resultAlgorithmIterate from L to R and check the bitwise AND for every possible pair and print the maximum ... Read More

Maximum average sum partition of an array in C++

Narendra Kumar
Updated on 31-Dec-2019 12:17:51

137 Views

Problem statementGiven an array, we partition a row of numbers A into at most K adjacent (non-empty) groups, then the score is the sum of the average of each group. What is the maximum score that can be scored?ExampleIf input array is {9, 2, 5, 3, 10} then we can partition array as follows −{9} {2, 5, 3} and {10} then average sum of this is −9 + (2 + 5 + 3)/ 3 + 10 = 22.33AlgorithmWe can use memorization technique to solve this problem −Let memo[i][k] be the best score portioning A[i to n-1] into at most K ... Read More

Maximum average of a subarray of size of at least X and at most Y in C++

Narendra Kumar
Updated on 31-Dec-2019 12:13:14

177 Views

Problem statementGiven an array arr[] and two integers X and Y. The task is to find a sub-array of size of at least X and at most Y with the maximum averageExampleIf input array is {2, 10, 15, 7, 8, 4} and x = 3 and Y = 3 then we can obtain maximum average 12.5 as follows −(10 + 15) / 2 = 12.5AlgorithmIterate over every sub-array of size starting from X to size Y and find the maximum average among all such sub-arrays.To reduce the time complexity, we can use prefix sum array to get the sum of ... Read More

Maximum array from two given arrays keeping order same in C++

Narendra Kumar
Updated on 31-Dec-2019 12:10:02

105 Views

Problem statementGiven two same sized arrays A[] and B[]. Task is to form a third array of same size. The result array should have maximum n elements from both array. It should have chosen elements of A[] first, then chosen elements of B[] in same order as they appear in original arrays. If there are common elements, then only one element should be present in res[] and priority should be given to A[]ExampleIf input arrays are −arr1[] = {9, 17, 2, 25, 6} arr2[] = {17, 4, 8, 10, 1} then final array is: {9, 17, 25, 8, 10}Please note ... Read More

Maximum area of quadrilateral in C++

Narendra Kumar
Updated on 31-Dec-2019 12:05:22

247 Views

Problem statementGiven four sides of quadrilateral a, b, c, d, find the maximum area of the quadrilateral possible from the given sides.AlgorithmWe can use below Brahmagupta’s formula to solve this problem −√(s-a)(s-b)(s-c)(s-d)In above formula s is semi-perimeter. It is calculated as follows −S = (a + b + c + d) / 2ExampleLet us now see an example − Live Demo#include using namespace std; double getMaxArea(double a, double b, double c, double d) {    double s = (a + b + c + d) / 2;    double area = (s - a) * (s - b) * (s ... Read More

Maximum bitwise AND value of a pair in an array in C++

Narendra Kumar
Updated on 31-Dec-2019 12:02:53

517 Views

Problem statementGiven an array of n positive elements. we need to find the maximum bitwise AND value generated by any pair of element from the array.ExampleIf input array is {10, 12, 15, 18} then maximum value of bitwise AND is 12.AlgorithmThe result of bitwise AND operations on single bit is maximum when both bits are 1. Considering this property −Start from the MSB and check whether we have minimum of two elements of array having set valueIf yes, then that MSB will be part of our solution and be added to result otherwise we will discard that bitSimilarly, iterating from ... Read More

Maximum and Minimum in a square matrix in C++

Narendra Kumar
Updated on 31-Dec-2019 11:58:50

134 Views

Problem statementGiven a square matrix of order n*n, find the maximum and minimum from the matrixExampleIf given matrix is −{{15, 17, 19}, {5, 1, 7}, {14, 5, 16}} then Minimum number is 1 and maximum number is 19AlgorithmSelect two elements from the matrix one from the start of a row of the matrix another from the end of the same row of the matrixCompare them and next compare smaller of them to the minimum of the matrix and larger of them to the maximum of the matrix.We can see that for two elements we need 3 compare so for traversing ... Read More

Maximum 0’s between two immediate 1’s in binary representation in C++

Narendra Kumar
Updated on 31-Dec-2019 11:53:54

736 Views

Problem statementGiven a number n, the task is to find the maximum 0’s between two immediate 1’s in binary representation of given n. Return -1 if binary representation contains less than two 1’sExampleIf input number is 35 then its binary representation is −00100011In above binary representation there are 3 0’s between two immediate 1’s. Hence answer is 3.AlgorithmWe can use bitwise shift operator to solve this problem. We need to find the position of two immediate 1’s in binary representation of n and maximize the difference of these position.If number is 0 or power of 2 then return -1IInitialize variable ... Read More

Advertisements