In this problem, we are given a matrix mat of size mXn of integer values. Our task is to create a program to Print cells with same rectangular sums in a matrix.Problem description: We will be finding a cell in the matrix in such a way that the sum of sub-matrices that are starting and ending with the cell is equal to the sum of all the remaining elements. For a cell the sum of matrix (a, b) sub-matrix mat[0][0] to mat[a][b] and mat[a][b] to mat[m][n] is equal to the sum of all remaining elements.Let’s take an example to understand the problem, ... Read More
In this problem, we are given an array arr[] of size n consisting of positive and negative values and an integer k. Our task is to find the maximum average subarray of k length. Let’s take an example to understand the problem, Input: arr[] = {4, -1, 5, 6, -2, 4} k = 3Output: 10Explanation: The subarray of size 3 with max sum is -1, 5, 6 = 10Solution ApproachA solution to the problem is done by using an auxiliary array to store cumulative sum till the current index in the array.To find the sum of subarrays, we need to compute the difference between the indices ... Read More
In this problem, we are given an array arr[] of size n consisting of positive values. Our task is to find maximum in an array without using Relational Operators. Let’s take an example to understand the problem, Input: arr[] = {5, 1, 6, 7 , 8, 2}Output: 8Solution ApproachSince we need to compare values without using logical operators. For this we need to perform repeated subtraction, the number which will last longer will be the larger one.We will decrement all values by one till they become zero. We will start with the first two values of the array and find the greatest ... Read More
In this problem, we are given two values x and y. Our task is to find maximum among x^(y^2) or y^(x^2) where x and y are given. Let’s take an example to understand the problem, Input: x = 4, y = 3Output: 3^(4^2)Explanation: x^(y^2) = 4^(3^2) = 4^9 = 262144y^(x^2) = 3^(4^2) = 3^16 = 43046721Solution approachOne approach can be to calculate both values and then print the maximum of both. But this method does not work when the values are large.A simple and easy approach is using natural log (ln) which will be the solution easier.ln(x^(y^2)) = (y^2) * ln(x)ln(y^(x^2)) = (x^2) * ... Read More
In this problem, we are given a Binary Tree. Our task is to find maximum among all right nodes in Binary Tree. Problem Description: Here, we need to find the maximum value amongst all right child nodes of the binary Tree.Let’s take an example to understand the problem, Input: Output: 9Explanation: All right nodes are: {2, 8, 9}. Maximum of them is 9.Solution ApproachTo solve the problem, we need to traverse the tree and check if its right child exists. If it exists, compare with maxRight element and replace if it is greater.Program to illustrate the working of our solution, ExampleLive Demo#include using namespace ... Read More
In this problem, we are given an array arr[] and a number k. Our task is to Find the maximum (or minimum) sum of a subarray of size k. Let’s take an example to understand the problem, Input: arr[] = {55, 43, 12, 76, 89, 25, 99} , k = 2Output: 165Explanation:The subarray of size 2 has sum = 76 + 89 = 165Solution ApproachA simple approach to solve the problem is by finding all k sized subarrays and then return the sum with maximum value.Another Approach is using the sliding window, we will find the sum of k sized subarrayes. For this, the ... Read More
In this problem, we are given a binary tree. Our task is to Find maximum (or minimum) in Binary Tree. Problem Description: We need to find the nodes of the binary tree that have maximum and minimum value in the binary tree.Let’s take an example to understand the problem, Input: Output: max = 9 , min = 1Solution ApproachWe need to find the max node of the binary tree. We will do this by traversing the pointer until we reach the leaf node and then find the maximum node of the tree.Program to illustrate the working of our solution, ExampleLive Demo#include using namespace ... Read More
In this problem, we are given a string str consisting of character and alphabets only. Our task is to find the longest length number in a string. Problem Description: we need to find the length of the number i.e. consecutive numerical characters in the string.Let’s take an example to understand the problem, Input: str = “code001tutorials34124point”Output: 34124Explanation: Numbers in the string are001 - size 334124 - size 5Solution ApproachA simple solution to the problem is by traversing the sting and finding the number’s length and its starting index. We will store the starting position and count of characters in the string for each number ... Read More
In this problem, we are given two positive numbers N and M. Our task is to find the M-th number whose repeated sum of digits of a number is N. Problem description: Here, we need to find the Mth number whose sum of digits till the sum becomes single digit is equal to N.Let’s take an example to understand the problem, Input: N = 4 M = 6Output: 49Solution ApproachA simple solution of the problem, is by finding all numbers and count the number whose sum of digits is N, and return m-th number.Another solution to the problem is using formula to find M-th ... Read More
In this problem, we are given two Rational Numbers. Our task is to find max of two Rational numbers. Here, the rational numbers are in the form of p/q.Let’s take an example to understand the problem, Input: rat1 = 5/4, rat2 = 3/2Output: 3/2Explanation: 5/4 = 1.253/2 = 1.5Solution Approach −A simple solution to the problem is by using a method similar to the one we used to perform in school.For this, we will find the L.C.M of the denominator. And then multiply the numerator based on the denominators value. Then for the common denominator, the rational number with maximum numerator value is the ... Read More