Maximum XOR Value in Matrix in C++

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

136 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

Mean and Median of a Matrix in C++

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

1K+ Views

In this problem, we are given a 2D array of size n*n. Our task is to create a program that will print the mean and median of the matrix in C++.Mean is the average of the date set. In a matrix mean is the average of all elements of the matrix.Mean = (sum of all elements of the matrix)/(number of elements of the matrix)Median is the middlemost element of the sorted data set. For this, we will have to sort the elements of the matrix.Median is calculated as, If n is odd, median = matrix[n/2][n/2]If n is even, median = ... Read More

Mean of Range in Array in C++

Narendra Kumar
Updated on 03-Jun-2020 07:27:58

443 Views

In this problem, we are given an array of n integers and some m querries. Our task is to create a program that calculates the integral value(round down) of the mean of the ranges given by the querries.Let’s take an example to understand the problem, Input −array = {5, 7, 8, 9, 10} m = 2; [0, 3], [2, 4]Output −7 9To solve this problem, we have two methods one is direct and the other is using prefix sum.In the direct approach, for each query, we will loop from the start index to the end index of the range. And ... Read More

Measure One Litre Using Two Vessels in C++

Narendra Kumar
Updated on 03-Jun-2020 07:26:25

207 Views

In this problem, we are given two vessels with capacities x and y and a supply of infinite water. Our task is to create a program that will be able to calculate exactly 1 liter in one vessel. Given the condition that x and y are co-primes. Co-primes also is known as relatively prime, mutually prime are numbers two numbers that have 1 as their only common divisor. So, this implies that their gcd(greatest common divisor) is 1.Here, let’s suppose we have two vessels V1 with capacity x and V2 with capacity y. To measure 1 liter using these two ... Read More

Median After K Additional Integers in C++

Narendra Kumar
Updated on 03-Jun-2020 07:24:50

87 Views

In this problem, we are given an array of n integers and we are adding K elements to the array and then find the median of the resultant array. Given the condition, N+k is odd.Let’s take an example to understand the problem, Input −array = {23, 65, 76, 67} ; k =1Output −67To solve this problem, we will sort the given elements in ascending order and then add k elements at the end of the array i.e. we will take k greater elements.The condition is given that n+k is odd. So, the median can be calculated using the formula, (n+k)/2.ExampleProgram ... Read More

Median of Running Stream of Numbers in C++

Narendra Kumar
Updated on 03-Jun-2020 07:22:47

423 Views

In this problem, we are given a data stream that is continuously reading integers. Our task is to create a program that will read elements and calculate the medians for these elements.The Median of an array is the middle element from a sorted sequence(it can be ascending or descending).Calculating medianFor odd count, the median is the middle elementFor even count, the median is average of two middle elementLet’s take an example to understand the problem, Input − 3, 65, 12, 20, 1At each input, Input - 3 : sequence -(3) : median - 3 Input - 65 : sequence -(3, ... Read More

Minimum Insertion Steps to Make a String Palindrome in C++

Arnab Chakraborty
Updated on 02-Jun-2020 11:36:26

322 Views

Suppose we have a string s, we have to make this string palindrome. in each step we can insert any character at any position, we have to find minimum number of characters that require to make this palindrome. If the string is like “mad”, then the answer will be 2 as we can add “da” before “mad”, or “am” after “mad” to make this palindrome.To solve this, we will follow these steps −Define a function lcs(), this will take s, x := sn := size of sreverse the string xs := concatenate space before s, x := concatenate space before ... Read More

Minimum Number of Flips to Convert Binary Matrix to Zero Matrix in C++

Arnab Chakraborty
Updated on 02-Jun-2020 11:33:59

182 Views

Suppose we have a m x n binary matrix mat. In one step, we can choose one cell and flip its bit and all the four neighbors of it if they are present. We have to find the minimum number of steps required to convert mat to a zero matrix. If there is no solution, then return -1.So if the given input is like [[0, 0], [0, 1]], alteration will be like −So we need 3 steps, the output will be 3.To solve this, we will follow these steps −n := number of rows, m := number of columns, x ... Read More

Palindrome Partitioning III in C++

Arnab Chakraborty
Updated on 02-Jun-2020 11:30:53

233 Views

Suppose we have a string s that is containing lowercase letters and an integer k. We have to maintain some properties. These are −First, we have to change some characters (if needed) of s to other lowercase English letters.Then divide the string s into k substrings such that each substring is a palindrome.We have to find the minimal number of characters that we need to change to divide the string.So if the string is like “ababbc” and k = 2, then the answer will be 1, as we have to change one character to split this into two palindrome strings. ... Read More

Race Car in C++

Arnab Chakraborty
Updated on 02-Jun-2020 11:27:33

1K+ Views

Suppose we have a car, that starts at position 0 and speed +1 on an infinite number line. The car runs automatically according to a sequence of instructions A: for accelerate and R − for reverse. When we get an instruction "A", our car does the following −position := position + speed, then speed = speed * 2.When we get an instruction "R", our car does the following −if speed is positive then speed = -1, otherwise speed = 1.For example, after executing the instructions "AAR", our car goes to positions 0->1->3->3, and speed goes to 1->2->4->-1.Now suppose we have ... Read More

Advertisements