Found 33676 Articles for Programming

Find maximum in an array without using Relational Operators in C++

sudhir sharma
Updated on 25-Jan-2021 05:24:40

269 Views

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

Find maximum average subarray of k length in C++

sudhir sharma
Updated on 25-Jan-2021 05:25:01

170 Views

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

Find maximum among x^(y^2) or y^(x^2) where x and y are given in C++

sudhir sharma
Updated on 25-Jan-2021 05:22:01

124 Views

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

Find maximum among all right nodes in Binary Tree in C++

sudhir sharma
Updated on 25-Jan-2021 05:21:35

101 Views

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

Find maximum (or minimum) sum of a subarray of size k in C++

sudhir sharma
Updated on 25-Jan-2021 05:21:14

407 Views

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

Find maximum (or minimum) in Binary Tree in C++

sudhir sharma
Updated on 25-Jan-2021 05:20:42

1K+ Views

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

Find max of two Rational numbers in C++

sudhir sharma
Updated on 25-Jan-2021 05:12:19

196 Views

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

Find m-th summation of first n natural numbers in C++

sudhir sharma
Updated on 25-Jan-2021 05:11:42

461 Views

In this problem, we are given two integers m and n. Our task is to Find m-th summation of the first n natural numbers. Problem Description: we will find sum of sum of n natural numbers m times. The sum is given by the formula, if (m > 1),           sum(n, m) = sum( sum(n, (m-1)), 1 )if (m = 1)          sum(n, m) = sum(n, 1) = sum of n natural numbersLet’s take an example to understand the problem,  Input: m = 4, n = 2Output: 231Explanation: sum(2, 4)     = sum ( sum(2, 3), 1 ... Read More

Find m-th smallest value in k sorted arrays in C++

sudhir sharma
Updated on 25-Jan-2021 05:11:04

248 Views

In this problem, we are given k different arrays of different sizes. Our task is to find m-th smallest value in k sorted arrays. Problem Description: Here, we need to find m-th smallest element of the merged array of all k arrays.Let’s take an example to understand the problem,  Input:         m = 4                   arr[][] = { {4 , 7},                                     {2, 5, 6},                       ... Read More

Find M-th number whose repeated sum of digits of a number is N in C++

sudhir sharma
Updated on 25-Jan-2021 05:13:26

154 Views

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

Advertisements