Server Side Programming Articles - Page 1890 of 2646

Maximum XOR value in matrix in C++

Narendra Kumar
Updated on 03-Jun-2020 07:32:35

155 Views

In this problem, we are given a matrix of size n X n. Our task is to create a program that will calculate the maximum XOR value of a complete row or a complete column.Let’s take an example to understand the problem, Input −N = 3 mat[N][N] = {{4, 9, 1} {2, 8, 3} {10, 12, 11}}Output −13Explanation −Row1: 4^9^1 = 12 Row2: 2^8^3 = 9 Row3: 10^12^11 = 13 Col1: 4^2^10 = 12 Col2: 9^8^12 = 13 Col3: 1^3^11 = 9Here, we have calculated the XOR of all rows and columns and then the maximum of them is printed.To ... Read More

Maximum XOR value of a pair from a range in C++

Narendra Kumar
Updated on 10-Feb-2020 10:00:19

845 Views

Problem statementGiven a range [L, R], we need to find two integers in this range such that their XOR is maximum among all possible choices of two integersIf the given range is L = 1 and R = 21 then the output will be 31 as − 31 is XOR of 15 and 16 and it is maximum within range.Algorithm1. Calculate the (L^R) value 2. From most significant bit of this value add all 1s to get the final resultExample#include using namespace std; int getMaxXOR(int L, int R){    int LXR = L ^ R;    int msbPos = ... Read More

Maximum XOR using K numbers from 1 to n in C++

Narendra Kumar
Updated on 03-Jun-2020 07:33:54

391 Views

In this problem, we are given two positive integers n and k. Our task is to find maximum xor between 1 to n using maximum X numbersLet’s take an example to understand the problem, Input − n = 5, k = 2Output − 7Explanation −elements till 5 is 1, 2, 3, 4, 5 Selecting all XOR pairs: 1^2 = 3, 1^3 = 2, 1^4 = 5, 1^5 = 4 2^3 = 4, 2^4 = 6, 2^5 = 7 3^4 = 7, 3^5 = 6 4^5 = 1 The maximum here is 7.To solve this problem, the maximum XOR can be ... Read More

Maximum width of a binary tree in C++

Narendra Kumar
Updated on 10-Feb-2020 09:55:41

336 Views

Problem statementGiven a binary tree, write a function to get the maximum width of the given tree. The width of a tree is the maximum of widths of all levels.Consider below tree −      10      / \     7   4    / \   \   9   2   1          / \         2   5 1. Width at level 1: 1 2. Width at level 2: 2 3. Width at level 3: 3 4. Width at level 4: 2 For above tree answer is 3.Algorithm1. Use ... Read More

Maximum weight transformation of a given string in C++

Narendra Kumar
Updated on 10-Feb-2020 09:51:50

472 Views

Problem statementGiven a string consisting of only A’s and B’s. We can transform the given string to another string by toggling any character. Thus many transformations of the given string are possible. The task is to find the Weight of the maximum weight transformation.Weight of a sting is calculated using below formula −Weight of string = Weight of total pairs + weight of single characters - Total number of toggles.Two consecutive characters are considered a pair only if they are different.Weight of a single pair (both characters are different) = 4Weight of a single character = 1If the input string ... Read More

Maximum weight path ending at any element of last row in a matrix in C++

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

402 Views

In this problem, we are given an integer n and a matrix of size n X n which contains the weight of the cell. Our task is to create a program that will find the maximum weight path ending at any element of the last row in a matrix. While finding the path the traversal will start from top-left (0, 0) and the valid moves will be down and diagonal, no left move is allowed.Let’s take an example to understand the problem, Input −n = 3 Mat[3][3] ={    {4, 3, 1}    {5, 8, 9}    {6, 7, 2}}Output  ... Read More

Maximum value of XOR among all triplets of an array in C++

Narendra Kumar
Updated on 03-Jun-2020 07:37:31

201 Views

In this problem, we are given an array of integers. Our task is to create the maximum value of XOR among all triplets of an array.Let’s take an example to understand the problem, Input − array = {5, 6, 1, 2}Output − 6Explanation −All triplets are: 5^6^1 = 2 5^6^2 = 1 5^1^2 = 6 6^1^2 = 5To solve this problem, a direct approach will be to find the XOR of all possible triplets and print the maximum of all triplets. This will not be effective if we work with an array with a huge number of elements in the ... Read More

Maximum value of arr[i] % arr[j] for a given array in C++

Narendra Kumar
Updated on 03-Jun-2020 07:39:20

305 Views

In this problem, we are given an array of n elements. Our task is to create a program that will find the maximum value of arr[i]%arr[j] for a given array.So, basically we need to find the value of the maximum remainder while dividing two elements of the array.Let’s take an example to understand the problem, Input − array{3, 6, 9, 2, 1}Output − 6Explanation −3%3 = 0; 3%6 = 3; 3%9 = 3; 3%2 = 1; 3%1 = 0 6%3 = 0; 6%6 = 0; 6%9 = 6; 6%2 = 0; 6%1 =0 9%3 = 0; 9%6 = 3; 9%9 ... Read More

Maximum value of |arr[i] – arr[j] - + |i – j| in C++

Narendra Kumar
Updated on 03-Jun-2020 07:48:06

427 Views

In this problem, we are given an array of n integers. Our task is to create a program that will find the maximum value of |arr[i]-arr[j]| + |i-j|.Let’s take an example to understand the problem, Input − array = {4, 1, 2}Output − 4Explanation −|arr[0] - arr[1]|+|0-1| = |4-1| + |-1| = 3+1 = 4 |arr[0] - arr[2]|+|0-2| = |4-2| + |-2| = 2+2 = 4 |arr[1] - arr[2 ]|+|1-2| = |1-2| + |1-2| = 1+1 = 2To solve this problem, a simple approach will be using the brute force approach which will be using two loops and finding the ... Read More

Maximum value of |arr[0] – arr[1] - + |arr[1] – arr[2] - + … +|arr[n – 2] – arr[n – 1] - when elements are from 1 to n in C++

Narendra Kumar
Updated on 03-Jun-2020 07:40:53

226 Views

In this problem, we are given an array of n integers of range [1, n]. Our task is to create a program that will find the maximum value of |arr[0] – arr[1] - + |arr[1] – arr[2] - + … +|arr[n – 2] – arr[n – 1].Let’s take an example to understand the problem, Input − array= {1, 2, 3}Output − 3Explanation −max sum is |1-3|+|2-1| = 3To solve this problem, a simple approach is to create all permutations from the array. And find the maximum value of all the values from permutation. A more effective method is to generalize ... Read More

Advertisements