Narendra Kumar has Published 196 Articles

Maximum subarray sum in O(n) using prefix sum in C++

Narendra Kumar

Narendra Kumar

Updated on 30-Jan-2020 12:05:28

513 Views

Problem statementGiven an Array of Positive and Negative Integers, find out the Maximum Subarray Sum in that ArrayExampleIf input array is − {-12, -5, 4, -1, -7, 1, 8, -3} then output is 9AlgorithmCalculate the prefix sum of the input array.Initialize− min_prefix_sum = 0, res = -infiniteMaintain a loop for ... Read More

Maximum size subset with given sum in C++

Narendra Kumar

Narendra Kumar

Updated on 21-Jan-2020 11:30:13

594 Views

Problem statementGiven an array of N elements and sum. We need to find size of maximum size subset whose sum is equal to given sumExampleIf input array is arr = { 2, 3, 5, 10 } and sum = 20 then output will be 4 as −2 + 3 + ... Read More

Maximum profit from sale of wines in C++

Narendra Kumar

Narendra Kumar

Updated on 21-Jan-2020 11:17:53

110 Views

Problem statementGiven n wines in a row, with integers denoting the cost of each wine respectively. Each year you can sale the first or the last wine in the row. The price of wines increases over time. Let the initial profits from the wines be P1, P2, P3…Pn. On the ... Read More

Maximum prefix-sum for a given range in C++

Narendra Kumar

Narendra Kumar

Updated on 21-Jan-2020 11:10:19

528 Views

Problem statementGiven an array of n integers and q queries, each query having a range from l to r. Find the maximum prefix-sum for the range l – r.ExampleIf input array is arr[] = {-1, 2, 3, -5} and queries = 2 and ranges are: l = 0, r = ... Read More

Maximum Perimeter Triangle from array in C++

Narendra Kumar

Narendra Kumar

Updated on 21-Jan-2020 10:58:36

223 Views

Problem statementGiven an Array of non-negative integers. Find out three elements from the array which form a triangle of maximum perimeterExampleIf input array is {5, 1, 3, 5, 7, 4} then maximum perimeter is (7 + 5 + 5) = 17AlgorithmSort the array in non-increasing order. So, the first element ... Read More

Maximum OR sum of sub-arrays of two different arrays in C++

Narendra Kumar

Narendra Kumar

Updated on 10-Jan-2020 08:04:03

130 Views

Problem statementGiven two arrays of positive integers. Select two sub-arrays of equal size from each array and calculate maximum possible OR sum of the two sub-arrays.ExampleIf arr1[] = {1, 2, 4, 3, 2} andArr2[] = {1, 3, 3, 12, 2} then maximum result is obtained when we create following two ... Read More

Maximum number of fixed points using at most 1 swaps in C++

Narendra Kumar

Narendra Kumar

Updated on 10-Jan-2020 07:58:37

75 Views

Problem statementGiven a permutation of N elements from 0 to N-1. A fixed point is an index at which the value is same as the index i.e. arr[i] = i. You are allowed to make at most 1 swap. Find the maximum number of fixed points that you can get.ExampleIf ... Read More

Maximum number of edges to be added to a tree so that it stays a Bipartite graph in C++

Narendra Kumar

Narendra Kumar

Updated on 10-Jan-2020 07:56:14

242 Views

Problem statementA tree is always a Bipartite Graph as we can always break into two disjoint sets with alternate levels.In other words, we always color it with two colors such that alternate levels have same color. The task is to compute the maximum no. of edges that can be added ... Read More

Maximum number of edges in Bipartite graph in C++

Narendra Kumar

Narendra Kumar

Updated on 10-Jan-2020 07:53:40

239 Views

Problem statementGiven an integer N which represents the number of Vertices. The Task is to find the maximum number of edges possible in a Bipartite graph of N vertices.Bipartite GraphA Bipartite graph is one which is having 2 sets of vertices.The set are such that the vertices in the same ... Read More

Maximum length of segments of 0’s and 1’s in C++

Narendra Kumar

Narendra Kumar

Updated on 10-Jan-2020 07:40:21

158 Views

Problem statementGiven a string comprising of ones and zeros. The task is to find the maximum length of the segments of string such that a number of 1 in each segment is greater than 0ExampleIf input string is “10111000001011” the answer will 12 as follows −First segment is of length ... Read More

Previous 1 ... 4 5 6 7 8 ... 20 Next
Advertisements