Found 26504 Articles for Server Side Programming

Find minimum possible digit sum after adding a number d in C++

sudhir sharma
Updated on 12-Mar-2021 07:01:15

234 Views

In this problem, we are given two numbers n and d. Our task is to Find the minimum possible digit sum after adding a number d.Problem Description − we need to minimise the digits sum by adding kth multiple of d to n.Let’s take an example to understand the problem, Inputn = 5230, d = 54Output1ExplanationThe number will be 5230 + (2*54) = 5338Solution ApproachA simple approach to solve the problem would be to check all multiples of d from 1 to 8, as at 9th multiple the sum of digits will repeat. This is based on modulo 9, which ... Read More

Find minimum operations needed to make an Array beautiful in C++

sudhir sharma
Updated on 12-Mar-2021 06:58:26

585 Views

In this problem, we are given a binary array bin[] consisting n binary values which happen to be 0 and 1. Our task is to find minimum operations needed to make an Array beautiful.Beautiful array is a special type of binary array which consists of a pattern of alternate 0’s and 1’s.Problem Description − We need to find the number operations that are required to make the array beautiful. An operations consists of these steps −Step 1 − Cut the array into two halves.Step 2 − Reverse any one of the two halves.Step 3 − Join then halves back.We will ... Read More

Find minimum number of merge operations to make an array palindrome in C++

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

872 Views

In this problem, we are given an array arr[] consisting of n positive numbers. Our task is to Find minimum number of merge operations to make an array palindrome.Palindrome array are similar to palindrome strings, the elements at index i and n-i should be the same.Example{5, 1, 7, 2, 7, 1, 5}Problem Description − We need to make the array palindrome by performing operations on it. And the only operation which is valid on the array is the merge operation in which we will add elements at index i and i+1.We need to return the minimum number of such operations ... Read More

Find minimum in an array without using Relational Operators in C++

sudhir sharma
Updated on 12-Mar-2021 06:52:40

203 Views

In this problem, we are given an array arr[] consisting of n positive elements. Our task is to find minimum in an array without using Relational Operators.Relational operators in programming are those operators which are used to check the relationship between two values. Like == (equals), greater than (>), less than (

Find Minimum Depth of a Binary Tree in C++

sudhir sharma
Updated on 12-Mar-2021 06:50:39

476 Views

In this problem, we are given a binary tree. Our task is to Find Minimum Depth of a Binary Tree.Binary Tree has a special condition that each node can have a maximum of two children.The minimum depth of a binary tree is the shortest path between the root node to any leaf node.Let’s take an example to understand the problem, InputOutput2Solution ApproachThe solution to the problem is by traversing the binary tree and counting the heights. This can be done by recursively calling for the child node of the node for each node non-leaf node and return 1 for each ... Read More

Find middle point segment from given segment lengths in C++

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

173 Views

In this problem, we are given an array arr[] of size m representing the lengths of line segments.The line segments are from 0 to arr[0] , arr[0] to arr[1] and so on. Our task is to find the segment which is at the middle of all segments.Let’s take an example to understand the problem, Inputarr[] = {5, 7, 13}Output3ExplanationSegments are : (0, 5) , (5, 12), (12, 25)Solution ApproachTo solve the problem, we will find the middle point of the line by (arrSum/2). If this middle point is a starting or ending point of a line segment then print -1. ... Read More

Find median in row wise sorted matrix in C++

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

288 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

168 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

547 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

187 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

Advertisements