Found 7347 Articles for C++

Maximum difference of zeros and ones in binary string - (O(n) time) in C++

Sunidhi Bansal
Updated on 04-Aug-2020 12:41:03

90 Views

Given the task is to find a sub-string from a given binary string and then the maximum difference between the number of zeroes and the ones.Let’s now understand what we have to do using an example −Inputstr = “10010110”Output2ExplanationIn the sub-array from the position 1 to 4 (“0010”), the difference between the zeros and ones = 3 – 1 = 2 which is the maximum that can be found.Inputstr = “00000”Output5Approach used in the below program as followsIn main() function create a string str to store the binary string. Also declare an array int arr[str.length()+1];Set all elements of arr[] = ... Read More

Maximum decimal value path in a binary matrix in C++

Sunidhi Bansal
Updated on 04-Aug-2020 12:37:54

98 Views

Given the task is to find the maximum integer value that can be obtained while travelling in a path from the top left element to the bottom right element of a given square binary array, that is, starting from index [0][0] to index [n - 1][n - 1].While covering the path we can only move to right ([i][j + 1]) or to the bottom ([i + 1][j])The integer value will be calculate using the bits of the traversed path.Let’s now understand what we have to do using an example −Inputm = {    {1, 1, 1, 1},    {0, 0, ... Read More

Maximum count of substrings of length K consisting of same characters in C++

Sunidhi Bansal
Updated on 04-Aug-2020 12:32:59

282 Views

Given the task is to find the maximum count of substrings of length K consisting of same characters. Given a string s and another integer K, we have to count the occurrence of sub-strings of size K that have same characters.Out of the sub-strings that are found, we have to choose the sub-string occurring the maximum number of time.Let’s now understand what we have to do using an example −Inputs = ”tuuxyyuuc”, K = 2Output2ExplanationHere the sub-strings of length 2 and having same characters are: “uu” and “yy” but as it is seen that “yy” occurs only 1 time and ... Read More

Maximum number of squares that can fit in a right angle isosceles triangle in C++

Sunidhi Bansal
Updated on 03-Aug-2020 10:47:45

273 Views

Given the task is to find the maximum number of squares having side ‘a’ that can fit inside a give right angle isosceles triangle with base of ‘s’(An isosceles triangle has at least 2 equal sides).Let’s now understand what we have to do using an example:Inputs=5, a=1Output10Explanation − The number of squares in the base can be calculated by dividing s by a and subtracting 1. So the number of squares in base = 5/1 – 1 = 4.Similarly when the bottom 4 squares are placed then we get a new isosceles triangle with base (s-a) and then we repeat ... Read More

Maximum number of segments that can contain the given points in C++

Sunidhi Bansal
Updated on 03-Aug-2020 10:46:29

177 Views

Given the task is to find the maximum of segments that can contain the given points.Given an array a1[] with size n1 and two integers A and B are given. From the given array a1[], n1 line segments can be formed with starting and ending points as a1[i] – A and a1[i] + B respectively.Another array a2[] is given with n2 number of points. These points have to be assigned to the line segments such that the number of line segments than have been assigned a point is maximum. Note that a single point can be assigned only once to ... Read More

Maximum number of removals of given subsequence from a string in C++

Sunidhi Bansal
Updated on 03-Aug-2020 10:43:55

110 Views

Given the task is to find the maximum number of removals of given subsequence from a string. A string s is given and we have to find the maximum number of subsequence ‘abc’ that can be removed from the string.Let’s now understand what we have to do using an example:Inputs = ‘dnabcxy’Output1Explanation − Only one subsequence of ‘abc’ can be found in the given string (‘dnabcxy’), therefore the output is 1.Inputs = ‘zcabcxabc’Output2 (‘zcabcxabc’)Approach used in the below program as followsIn Max() function initialize variables i, a, ab, abc with value = 0 and of type int.Loop from i=0 till ... Read More

Maximum number of people that can be killed with strength P in C++

Sunidhi Bansal
Updated on 03-Aug-2020 10:41:53

139 Views

Given the task is to find the maximum number of people that can be killed with strength P. Consider a row with infinite people and each of them have an index number starting from 1.The strength of the sth person is represented by s2. After killing a person with s strength your strength also decreases by s.Let’s now understand what we have to do using an example −InputP = 20Output3ExplanationStrength of 1st person = 1 * 1 = 1 < 20, therefore 1st person can be killed. Remaining strength = P – 1 = 20 – 1 = 19 Strength ... Read More

Maximum length subsequence with difference between adjacent elements as either 0 or 1 | Set 2 in C++

Sunidhi Bansal
Updated on 03-Aug-2020 10:40:47

104 Views

We are given an array of any size and the task is to find the subsequence in the given array with the elements having difference between adjacent elements as 0 or 1.Input − int arr[] = { 2, 1, 5, 6, 3, 4, 7, 6}Output − Maximum length subsequence with difference between adjacent elements as either 0 or 1 is: 3Explanation − The subsequence of adjacent elements in an array with difference as 0 or 1 are {2, 1}. Therefore, the maximum length of subsequence is 2.Input − int arr[] = { 2, 1, 7, 6, 5}Output − Maximum length ... Read More

Maximum length subsequence with difference between adjacent elements as either 0 or 1 in C++

Sunidhi Bansal
Updated on 03-Aug-2020 10:38:53

747 Views

We are given an array of any size and the task is to find the subsequence in the given array with the elements having difference between adjacent elements as 0 or 1.Input − int arr[] = { 2, 1, 5, 6, 3, 4, 7, 6}Output − Maximum length subsequence with difference between adjacent elements as either 0 or 1 is − 3Explanation − The subsequence of adjacent elements in an array with difference as 0 or 1 are {2, 1}. Therefore, the maximum length of subsequence is 2.Input − int arr[] = { 2, 1, 7, 6, 5}Output − Maximum ... Read More

Maximum length subarray with difference between adjacent elements as either 0 or 1 in C++

Sunidhi Bansal
Updated on 03-Aug-2020 10:38:08

564 Views

We are given an array of any size and the task is to find the subarray of the given array with the elements having difference between adjacent elements as 0 or 1.Input − int arr[] = { 2, 1, 5, 6, 3, 4, 7, 6}Output − Maximum length subarray with difference between adjacent elements as either 0 or 1 are − 2Explanation − The adjacent elements in an array with difference as 0 or 1 are {2, 1}, {5, 6}, { 3, 4} and {7.6}. Therefore, the maximum length of subarray is 2.Input − int arr[] = { 2, 1, ... Read More

Advertisements