Found 7197 Articles for C++

Maximum Bitwise AND pair from given range in C++

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

125 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

215 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

264 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

192 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

314 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

677 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

226 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

866 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

Maximum element in min heap in C++

Narendra Kumar
Updated on 31-Dec-2019 11:48:37

344 Views

Problem statementGiven a minimum heap find maximum element in that.ExampleIf input heap is −Then maximum element is 55AlgorithmIn minimum heap parent node will be lesser than its children. Hence we can conclude that a non-leaf node cannot be the maximum.Search maximum element in the leaf nodesExampleLet us now see an example − Live Demo#include using namespace std; int getMaxElement(int *heap, int n) {    int maxVal = heap[n / 2];    for (int i = n / 2 + 1; i < n; ++i) {       maxVal = max(maxVal, heap[i]);    }    return maxVal; } int main() {    int heap[] = {15, 27, 22, 35, 29, 55, 48}; int n = sizeof(heap) / sizeof(heap[0]);    cout

Maximizing Unique Pairs from two arrays in C++

Narendra Kumar
Updated on 31-Dec-2019 11:45:55

364 Views

Problem statementGiven two arrays of equal size N, form maximum number of pairs by using their elements, one from the first array and second from the second array, such that an element from each array is used at-most once and the absolute difference between the selected elements used for forming a pair is less than or equal to a given element K.ExampleIf input is −arr1[] = {3, 4, 5, 2, 1}arr2[] = {6, 5, 4, 7, 15}and k = 3 then we can form following 4 pairs whose absolute difference is less than or equal to 3 −(1, 4), (2, ... Read More

Advertisements