Found 7197 Articles for C++

Find median of BST in O(n) time and O(1) space in C++

Arnab Chakraborty
Updated on 25-Jul-2020 16:43:10

894 Views

ConceptWith respect of a given Binary Search Tree(BST), our task is to determine median of it.For even no. of nodes, median = ((n/2th node + (n+1)/2th node) /2 For odd no. of nodes, median = (n+1)/2th node.For given BST(with odd no. of nodes) is −       7       / \      4   9    / \   / \   2  5  8  10Inorder of Given BST will be : 2, 4, 5, 7, 8, 9, 10 So, here median will 7.For given BST(with even no. of nodes) is −         7   ... Read More

Find maximum sum of triplets in an array such than i < j < k and a[i] < a[j] < a[k] in C++

Ravi Ranjan
Updated on 01-Jul-2025 15:35:10

2K+ Views

In this article, we have an array of positive integers of size n. Our task is to calculate the maximum sum of triplet ( ai + aj + ak ) such that 0 sum = 12 3 6 10 => sum = 19 3 4 10 => sum = 17 4 5 10 => sum = 19 2 5 10 => sum = 17 Maximum sum = 19 Finding maximum sum of triplets in an array such than i < j < k and a[i] < a[j] < a[k]Here are the approaches ... Read More

Find maximum points which can be obtained by deleting elements from array in C++

Arnab Chakraborty
Updated on 25-Jul-2020 08:52:43

143 Views

ConceptWith respect of a given array A having N elements and two integers l and r where, 1≤ ax ≤ 105 and 1≤ l≤ r≤ N. We can select any element of the array (let’s say ax) and delete it, and also delete all elements equal to ax+1, ax+2 … ax+R and ax-1, ax-2 … ax-L from the array. This step will cost ax points. Our task is to maximize the total cost after deleting all the elements from the array.Input2 1 2 3 2 2 1 l = 1, r = 1Output8Here, we choose 2 to delete, then (2-1)=1 ... Read More

Find maximum operations to reduce N to 1 in C++

Arnab Chakraborty
Updated on 25-Jul-2020 08:49:31

131 Views

ConceptWith respect of given two numbers P and Q ( P and Q can be up to 10^6 ) which forms a number N = (P!/Q!). Our task is to reduce N to 1 by performing maximum number of operations possible. Remember, in each operation, one can replace N with N/X if N is divisible by X. Determine the maximum number of operations that can be possible.InputA = 7, B = 4Output4ExplanationN is 210 and the divisors are 2, 3, 5, 7InputA = 3, B = 1Output2ExplanationN is 6 and the divisor are 2, 3.MethodIt has been observed that factorization ... Read More

Find maximum N such that the sum of square of first N natural numbers is not more than X in C++

Arnab Chakraborty
Updated on 25-Jul-2020 08:45:08

464 Views

ConceptWith respect of a given integer X, our task is to determine the maximum value N so that the sum of first N natural numbers should not exceed X.InputX = 7Output22 is the maximum possible value of N because for N = 3, the sum of the series will exceed X i.e. 1^2 + 2^2 + 3^2 = 1 + 4 + 9 = 14InputX = 27Output33 is the maximum possible value of N because for N = 4, the sum of the series will exceed X i.e. 1^2 + 2^2 + 3^2 + 4^2 = 1 + 4 + ... Read More

Find maximum length Snake sequence in C++

Arnab Chakraborty
Updated on 25-Jul-2020 08:42:11

311 Views

ConceptWith respect of a given grid of numbers, determine maximum length Snake sequence and display it. It has been observed that if multiple snake sequences exist with the maximum length, display any one of them.Actually, a snake sequence is made up of adjacent numbers in the grid so that for each number, the number on the right or the number below it is either +1 or -1 its value. Here, for instance, if we are at location (a, b) in the grid, we can either move right i.e. (a, b+1) if that number is ± 1 or move down i.e. ... Read More

Find maximum distance between any city and station in C++

Arnab Chakraborty
Updated on 25-Jul-2020 08:36:46

453 Views

ConceptWith respect of the given number of cities N numbered from 0 to N-1 and the cities in which stations are located, our task is to determine the maximum distance between any city and its nearest station. It should be noted that the cities with stations can be given in any order.InputnumOfCities = 6, stations = [2, 4]Output2InputnumOfCities = 6, stations = [4]Output4The following figure indicates the first example containing 6 cities and the cities with stations highlighted with green color. So, in this case, the farthestcities from its nearest stations are 0 at a distance of 2. Hence maximum ... Read More

Find maximum difference between nearest left and right smaller elements in C++

Arnab Chakraborty
Updated on 24-Jul-2020 12:04:51

365 Views

ConceptWith respect of a given an array of integers, our task is to determine the maximum absolute difference between the nearest left and the right smaller element of every element in the array. It should be noted that if there is no smaller element on right side or left side of any element then we accept zero as the smaller element. Here, for example for the leftmost element, the nearest smaller element on the left side is set as 0. In the same way, for rightmost elements, the smaller element on the right side is set as 0.Inputarr[] = {3, ... Read More

Find lost element from a duplicated array in C++

Arnab Chakraborty
Updated on 24-Jul-2020 12:02:27

156 Views

ConceptWith respect of given two arrays which are duplicates of each other except one element, that means one element from one of the array is missing, our task to determine that missing element.Inputarr1[] = {2, 5, 6, 8, 10} arr2[] = {5, 6, 8, 10}Output22 is missing from second array.Inputarr1[] = {3, 4, 5, 6} arr2[] = {3, 4, 5, 6, 7}Output77 is missing from first array.MethodHere, we apply one simple solution where we iterate over arrays and verify element by element and mark the missing element when an unmatched is detected. But the drawback of this solution is that ... Read More

Find longest palindrome formed by removing or shuffling chars from string in C++

Arnab Chakraborty
Updated on 24-Jul-2020 11:56:17

210 Views

ConceptWith respect of a given string, determine the longest palindrome that can be formed by removing or shuffling characters from the string. Finally return only one palindrome if it hasbeen observed that there is multiple palindrome strings of longest length.InputpqrOutputp OR q OR rInputppqqrrOutputpqrrqp OR qprrpq OR rqppqr OR any other palindromic string of length 6.InputpqpOutputpqpMethodHere, We can partition any palindromic string into three parts – beg, mid and end. With respectof palindromic string of odd length say 2n + 1, here ‘beg’ consists of first n characters of the string, ‘mid’ consists of only 1 character that means (n ... Read More

Advertisements