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
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
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
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
Given the task is to calculate the maximum product that can be obtained from four factors A, B, C, D of a given number N, given the condition −The sum of the four factors should be equal to the number N, that is, N=A+B+C+D.Input − N=10Output − 20Explanation − The factors of 10 are: 1, 2, 5, 10.Maximum product can be obtained by multiplying 5*2*2*1=20 and also it satisfies the given condition, that is, 5+2+2+1=10.Input − N=16Output − 256Explanation − The factors of 16 are: 1, 2, 4, 8, 16.Maximum product can be obtained by multiplying 4*4*4*4=256 and also it ... Read More
Given the task is to maximize a given number with ‘N’ number of digits by replacing its digit using another array that contains 10 digits as an alternative for all single-digit numbers 0 to 9, The given condition is that only a consecutive segment of numbers can be replaced and only once.Input N=1234, arr[]={3 ,0 ,1 ,5 ,7 ,7 ,8 ,2 ,9 ,4}Output 1257Explanation The number 3 can be replaced with its alternative 5= arr[3]The number 4 can be replaced with its alternative 7= arr[4]Input N=5183, arr[]={3 ,0 ,1 ,5 ,7 ,7 ,8 ,2 ,9 ,4}Output 7183Approach used in the below program as followsIn function ... Read More
Given the task is to calculate the maximum profit that can be made by selling at-most ‘M’ products.The total number of products are ‘N’ and the cost price and the selling price of each product is given in the lists CP[] and SP[] respectively.Input N=6, M=4 CP[]={1, 9, 5, 8, 2, 11} SP[]={1, 15, 10, 16, 5, 20}Output 28Explanation − The profit obtained from selling all the products are 0, 6, 5, 8, 3, 9 respectively.So, in order to make maximum profit by selling only 4 products, the products with the highest profit need to be chosen, that is, product number 2, ... Read More
According to the problem we are given a set arr[n] where n is the number of integer elements in the set, the task is to find the maximum difference elements which are to be added to obtain the elements in the set. In other words, the difference should be in form of |a-b| where 'a' and 'b' both are in the set and their difference should not be the least. So, we will count the maximum number of differences which are distinct and largest from a set. Let's understand the problem and its solution with help of an example.Input − ... Read More
According to the problem we are given a string str, we must count all the substrings in the given string. Substring is a string which is a part of an already existing string whose size may be smaller than or equal to the existing string.Let's understand the problem and its solution with the help of examples.Input − str = "wxyz";Output − count of distinct substring is: 10Explanation − Distinct substrings counted are −wxyz, wxy, wx, w, xyz, xy, x, yz, y, z so their count is 10Input − str = "zzzz"Output − count of distinct substring is: 4Explanation − Distinct ... Read More
The task is to find the greatest difference between the sum of m elements in an array. Suppose we have an array and a number m, then we will first find the sum of highest m numbers and then subtract the sum of lowest m numbers from it to get the maximum difference. So the main thing is to find two subsets of m numbers which have the highest sum and lowest sum.Let’s now understand what we have to do using an example −Input arr = {1, 2, 3, 4, 5} ; m=3Output Maximum difference here is : 6Explanation − Here the ... Read More