Found 7197 Articles for C++

Maximum area rectangle by picking four sides from array in C++

Sunidhi Bansal
Updated on 14-Aug-2020 07:50:40

561 Views

The area of the rectangle is calculated as a product of its sides. All rectangles have four sides such that opposite sides are equal. For calculating the area we require length and breadth as two sides. So that we can get desired result −Area rectangle = length X breadthWe are given an array such that it consists of sides of a rectangle. The array contains values for all four sides in random order. The task here is to find two highest pairs of sides from the array in order to get the maximum area possible for the rectangle.Input Arr[] = { ... Read More

Maximum and Minimum element of a linked list which is divisible by a given number k in C++

Sunidhi Bansal
Updated on 14-Aug-2020 07:48:58

192 Views

A linked list is a linear data structure in which elements are linked via pointers. Each element or node of a linked list has a data part and link, or we can say pointer to the next element in sequence. The elements can take noncontiguous locations in memory.We are given a singly linked list in which there is a data part and link to the next element. The other input is a number K. Task is to find the Maximum and Minimum element of a linked list which is divisible by the number K. The linear linked list can only ... Read More

Maximum adjacent difference in an array in its sorted form in C++

Sunidhi Bansal
Updated on 14-Aug-2020 07:45:48

1K+ Views

We are given with an array. The array need not be sorted. The task is to find the maximum difference between adjacent elements of that array in its sorted form. So the first thing is to sort the array in increasing or decreasing order. Then we will iterate the array and calculate the adjacent difference of Arr[i+1]-Arr[i]. Then for each iteration we will compare this difference with the one which is found maximum so far.Input − Arr[] = [ 1, 5, 10, 2, 7 ]Output − Maximum adjacent difference in array in its sorted form is 3.Explanation − Sorted Arr[] ... Read More

Maximizing Probability of one type from N containers in C++

Sunidhi Bansal
Updated on 14-Aug-2020 07:40:16

161 Views

Probability Pi= (No. of Favourable Outcomes) / (Total no. of Outcomes).Given is a number N which is the number of containers present. And we have N copies of two numbers X and Y. The task is to divide copies of one number X into N containers such that the probability of drawing a copy of X is maximum. From above it can be seen that to maximize Pi, we can either maximize the numerator ( No. of favourable outcomes) or minimize the denominator(Total no. of Outcomes). This can be done in a manner that only one container has a copy ... Read More

Maximize volume of cuboid with given sum of sides in C++

Sunidhi Bansal
Updated on 14-Aug-2020 07:38:44

259 Views

We are given with a sum of sides of a cuboid. A cuboid has three sides length, breadth and height. The volume of cuboid is calculated as a product of all three sides.Volume of Cuboid = Length X Breadth X HeightThe maximum volume can be achieved if all three sides are as close as possible.Let’s now understand what we have to do using an example −For Example The problem given here provides us with the sum of sides, say S. And let sides be L, B, H. In order to maximize the volume we have to find the sides as close ... Read More

Maximize the value of A by replacing some of its digits with digits of B in C++

Sunidhi Bansal
Updated on 14-Aug-2020 07:36:41

176 Views

The task is to maximize the value of number A by replacing some of its digits with digits present in another number B. No digits will be replaced if A’s value cannot be maximized.Note − a digit from B can be used only once.Let’s now understand what we have to do using an example −Input A = “1221” B = “1211”Output Maximum value of A possible 2221Explanation − We here select 2 from B and replace it with the first 1 of A. Here it is the only choice as replacing any other digit of A with either 2 or 1 will ... Read More

Maximize the sum of X+Y elements by picking X and Y elements from 1st and 2nd array in C++

Sunidhi Bansal
Updated on 14-Aug-2020 07:34:56

258 Views

For the given two arrays each of size N, the task is to find the maximum sum by choosing X elements from array 1 and Y elements from array 2.Let’s now understand what we have to do using an example −Input arr1 = {1, 2, 3, 4, 5} ; X=2 arr2 = {1, 3, 5, 2, 7}; Y=3Output Maximum sum here is : 24Explanation − We are selecting 2 number(s) from arr1 and 3 from arr2. Largest 2 of arr1 are 4, 5 and largest 3 of arr2 are 3, 5, 7. Total sum of these 5 elements is 24 which is ... Read More

Maximum difference of indices (i, j) such that A[i][j] = 0 in the given matrix in C++

Sunidhi Bansal
Updated on 14-Aug-2020 07:33:17

135 Views

We are given with a matrix of size n x n and a condition that a[i][j] = 0 and the task is to calculate the maximum difference of indices having a[i][j] = 0. So, we can clearly state that there must be at least one zero in a matrix.Input int matrix[][] = {    {0, 1, 1},    {0, 0, 0},    {4, 5, 1}}Output −Maximum difference of indices (i, j) such that A[i][j] = 0 in the given matrix is −Explanation − we have element 0 at matrix[0][0], matrix[1][0], matrix[1][1] and matrix[1][2]. So the maximum difference of indices will be ... Read More

Maximize the maximum among minimum of K consecutive sub-arrays in C++

Sunidhi Bansal
Updated on 14-Aug-2020 07:31:05

253 Views

Given the task is to divide an array arr[] into K consecutive sub-arrays and find the maximum possible value of maximum among the minimum of the K consecutive sub-srrays.Input arr[]={2, 8, 4, 3, 9, 1, 5}, K=3Output 9Explanation − The 3 consecutive sub arrays that can made are: {2, 8, 4, 3}, {9}, and {1, 5}The minimum values out of all these arrays are: (2, 9, 1)The maximum value out of these three is 9.Input arr[] = { 8, 4, 1, 9, 11}, K=1Output 11Approach used in the below program as followsIf we look at the task, it can be divided into 3 cases ... Read More

Maximize the number of sum pairs which are divisible by K in C++

Sunidhi Bansal
Updated on 14-Aug-2020 07:28:06

200 Views

Given the task is to calculate the maximum number of pairs arr[i] + arr[j] that are divisible by K where arr[] is an array containing N integers.Given the condition that a particular index number cannot be used in more than one pair.Inputarr[]={1, 2 ,5 ,8 ,3 }, K=2Output 2Explanation − The desired pairs are: (0, 2), (1, 3) as 1+5=6 and 2+8=10 . Both 6 and 10 are divisible by 2.Alternative answers could be the pairs: (0, 4), (1, 3) or (2, 4), (1, 3) but the answer remains the same, that is, 2.Input arr[]={1 ,3 ,5 ,2 ,3 ,4 }, K=3Output 3Approach ... Read More

Advertisements