C++ Articles - Page 202 of 719

Find median in row wise sorted matrix in C++

sudhir sharma
Updated on 12-Mar-2021 06:44:06

304 Views

In this problem, we are given a 2D array mat[r][c] whose elements are sorted row-wise. Our task is to Find median in a row-wise sorted matrix.Description − we need to find the median of elements of the matrix.Let’s take an example to understand the problem, Inputmat = {    {2, 4, 7},    {5, 6, 8},    {4, 8, 9} }Output6ExplanationThe elements of the matrix stored in array are &minus{2, 4, 4, 5, 6, 7, 8, 8, 9} The median is 6.Solution ApproachA simple solution to the problem is by storing all elements of the array. Then finding the median ... Read More

Find mean of subarray means in a given array in C++

sudhir sharma
Updated on 12-Mar-2021 06:41:56

179 Views

In this problem, we are given an array arr[] of size n and an integer m. Our task is to Find mean of subarray means in a given array.Code Description − Here, we need to find the mean of array as the mean of means of subarray of size m.Let’s take an example to understand the problem, Inputarr[] = {2, 5, 3, 6, 1}, m = 3Output3.78ExplanationAll subarrays of size m are {2, 5, 3}, {5, 3, 6}, {3, 6, 1} Means of means of subarray of size m, $$(\left(\frac{2+5+3}{3}\right)+\left(\frac{5+3+6}{3}\right)+\left(\frac{3+6+1}{3}\right))/3=\left(\frac{10}{3}\right)+\left(\frac{14}{3}\right)+\left(\frac{10}{3}\right)/3=34/3/3=3.78$$Solution ApproachA simple solution to the problem is by finding all ... Read More

Find Maximum XOR value of a sub-array of size k in C++

sudhir sharma
Updated on 12-Mar-2021 06:38:36

560 Views

In this problem, we are given an array arr[] consisting of n elements and an integer k. Our task is to find the Maximum XOR value of a sub-array of size k.Let’s take an example to understand the problem, Inputarr[] = {3, 1, 6, 2 ,7, 9} k = 3Output12ExplanationAll subarray and the xor of all element of size k, {3, 1, 6} = 4 {1, 6, 2} = 5 {6, 2, 7} = 3 {2, 7, 9} = 12Solution ApproachA simple solution to the problem is by using two loops. One to iterate over the array and other to ... Read More

Find maximum XOR of given integer in a stream of integers in C++

sudhir sharma
Updated on 12-Mar-2021 06:36:47

198 Views

In this problem, we are given Q queries each of which is one of the following type, Type 1 − insertion (1, i) to add the element with value i, in your data structure.Type 2 − findXOR (2, i), to find the XOR of all elements of the data structure with the element i.The data structure should contain only 1 element initially which will be 0.Let’s take an example to understand the problem, InputQueries: (1, 9), (1, 3), (1, 7), (2, 8), (1, 5), (2, 12)Output15 15ExplanationSolving each query, (1, 9) => data structure => {9} (1, 3) => data ... Read More

Find maximum value of Sum( i*arr[i]) with only rotations on given array allowed in C++

sudhir sharma
Updated on 12-Mar-2021 06:32:48

188 Views

In this problem, we are given an array arr[] consisting of n elements. We need to Find maximum value of Sum( i*arr[i]) with only rotations on a given array allowed. For find the maximum sum of (i*arr[i]), we can perform any number of rotations.Let’s take an example to understand the problem, Inputarr[] = {4, 1, 3, 7, 2}Output43ExplanationWe will rotate the array once to get the maximum value, After rotation array will be {2, 4, 1, 3, 7}Sum = 0*2 + 1*4 + 2*1 + 3*3 + 4*7 = 0 + 4 + 2 + 9 + 28 = 43Solution ... Read More

Find Maximum value of abs(i – j) * min(arr[i], arr[j]) in an array arr[] in C++

sudhir sharma
Updated on 12-Mar-2021 06:30:15

497 Views

In this problem, we are given an array arr[] insisting of N integer values.Our task is to Find Maximum value of abs(i – j) * min(arr[i], arr[j]) in an array arr[].Problem description − we need to find the maximum product value of the minimum value of two elements and the absolute differences between their indexes. i.e. for two values i and j, we need to maximise, abs(i - j) * min(arr[i] , arr[j]).Inputarr[] = {5, 7, 3, 6, 4}Output16ExplanationThe maximum value is 16, between index 0 and 4 => abs(0 - 4)*min(arr[0], arr[4]) => 4*min(5, 4) => 4*4 = 16Solution ... Read More

Find maximum sum taking every Kth element in the array in C++

sudhir sharma
Updated on 12-Mar-2021 06:27:09

296 Views

In this problem, we are given an array arr[] and an integer k. Our task is to Find the maximum sum taking every Kth element in the array.Problem description: We need to find the maximum sum of the elements of the array such that they are k indexes apart. i.e. we need to maximize the sum, sum = arr[i] + arr[i+k] + arr[i + 2*k] + …. arr[i + p*k], such that (i + p*k) < nLet’s take an example to understand the problem, Inputarr[] = {5, 3, −1, 2, 4, −5, 6}, k = 4Output9ExplanationAll sums of every kth ... Read More

Find maximum sum array of length less than or equal to m in C++

sudhir sharma
Updated on 12-Mar-2021 06:23:44

170 Views

In the problem, we are given n arrays of different length. Our task is to Find the maximum sum array of length less than or equal to m.We need to find sub-arrays from the arrays to maximise the sum and make the length of all subarrays combined equal to m.Let’s take an example to understand the problem, Inputn = 3, m = 4 arrOfArr[][] = {    {5, 2, -1, 4, -3}    {3, -2, 1, 6}    {-2, 0, 5} }Output20ExplanationSubArrays are {5, 4}, {6}, {5}, length = 2 + 1 + 1 = 4 Sum = 5 + ... Read More

Find Maximum side length of square in a Matrix in C++

sudhir sharma
Updated on 12-Mar-2021 06:00:41

286 Views

In this problem, we are given a 2-D matrix mat[][] of size n, n being an odd number. Our task is to Find Maximum side length of a square in a Matrix.Problem Description − We need to find the length of the square matrix whose perimeter values are the same and it shares the same center as the matrix.Let’s take an example to understand the problem, Inputmat[][] = {    {2, 4, 6, 6, 5},    {1, 7, 7, 7, 3},    {5, 7, 0, 7, 1},    {3, 7, 7, 7, 1},    {2, 0, 1, 3, 2} }Output3Solution ... Read More

Find maximum possible stolen value from houses in C++

sudhir sharma
Updated on 12-Mar-2021 05:57:40

708 Views

In this problem, we are given n houses with some values in them. Our task is to Find the maximum possible stolen value from houses.Problem Description − We have an array houses[] that consist of the values that are in each house. A thief robs the houses but he cannot steal from two adjacent houses as neighbours know about the theft. We need to find the maximum possible value that can be stolen by the thief from the house without getting caught.Let’s take an example to understand the problem, Inputhouses[] = {5, 2, 1, 6, 7, 9, 4, 3}Output23ExplanationThe max ... Read More

Advertisements