Found 7197 Articles for C++

Merge k sorted arrays of different sizes in C++

Arnab Chakraborty
Updated on 27-Aug-2020 12:12:12

610 Views

Suppose we have different k sorted arrays. We have to merge these arrays and display the sorted result.So, if the input is like k = 3 and arrays are {2, 4}, {3, 5, 7}, {1, 10, 11, 12} , then the output will be 1 2 3 4 5 7 10 11 12To solve this, we will follow these steps −define one type of pair with first element is an integer and second element is another pair of integers, name it as ppi.Define an array opdefine one priority queue qfor initialize i := 0, when i < size of arr, ... Read More

Maximum Consecutive Increasing Path Length in Binary Tree in C++

Arnab Chakraborty
Updated on 27-Aug-2020 12:09:46

254 Views

Suppose we have a binary tree; we have to calculate the length of the longest path which consists of nodes with consecutive values in increasing order. Every node will be treated as a path of length 1.So, if the input is likethen the output will be 3 as (11, 12, 13) is maximum consecutive path.To solve this, we will follow these steps −Define a function solve(), this will take root, prev_data, prev_length, if not root is non-zero, then −return prev_lengthcur_data := val of rootif cur_data is same as prev_data + 1, then −return maximum of solve(left of root, cur_data, prev_length+1) ... Read More

Unique element in an array where all elements occur k times except one in C++

Arnab Chakraborty
Updated on 27-Aug-2020 12:05:14

427 Views

we have an array A. A has all elements occurring m times, but one element occurs only once. We have to find that unique element.So, if the input is like A = [6, 2, 7, 2, 2, 6, 6], m = 3, then the output will be 7.To solve this, we will follow these steps −INT_SIZE := 8 * size of an integer type variableDefine an array count of size: INT_SIZE. and fill with 0for initialize i := 0, when i < INT_SIZE, update (increase i by 1), do −for initialize j := 0, when j < size, update (increase ... Read More

Succinct Encoding of Binary Tree in C++

Arnab Chakraborty
Updated on 27-Aug-2020 11:11:38

383 Views

Suppose we have a binary tree. As we know the succinct encoding of Binary Tree performs close to lowest possible space. The n’th Catalan number is designated by the number of structurally different binary trees with n different nodes. If the n is large, this is about 4n; thus, we require minimum about log2(4) n = 2n bits to encode it. A succinct binary tree therefore would consume 2n + O(n) bits.So, if the input is likethen the output will beencoded −Structure List 1 1 1 0 0 1 0 0 1 0 1 0 0Data List 10 20 40 ... Read More

Maximum sum of elements from each row in the matrix in C++

Ayush Gupta
Updated on 15-Oct-2020 12:11:37

903 Views

In this problem, we are given a two matrix mat[][]. Our task is to create a program to find the maximum sum of elements from each row in the matrix in C++.Problem DescriptionHere, we will find the maximum sum by taking one element from each row of the matrix in such a way that the element at the current row is greater than the last row element to be considered as a sum. We will find the maximum sum of elements that follows the above condition and print -1 if it is not possible.Let’s take an example to understand the ... Read More

Maximum sum of distinct numbers with LCM as N in C++

Revathi Satya
Updated on 22-May-2024 11:56:22

343 Views

In this article, we are given a number N. Our task is to create a program to find the maximum sum of distinct numbers with LCM as N in C++. To achive this first of all we need to find the sum of maximum numbers that have N as the Lowest Common Multiple (LCM). For better understanding Let us go through 3 key concepts − Least Common Multiple (LCM): The lowest common multiple(LCM) of two or more integers is the smallest positive integer that is divisible for all of the integers. For example, the LCM of 4 and 5 ... Read More

Maximum sum of distinct numbers such that LCM of these numbers is N in C++

Ayush Gupta
Updated on 15-Oct-2020 12:16:27

189 Views

In this problem, we are a number N. Our task is to create a program to find the Maximum sum of distinct numbers such that LCM of these numbers is N in C++.Problem DescriptionWe need to find the sum of all factors of the number N. And add all distinct to find the maximum sum.Let’s take an example to understand the problem, InputN = 12Output28ExplanationAll distinct factors of N are 1, 2, 3, 4, 6, 12. Sum = 1 + 2 + 3 + 4 + 6 + 12 = 28Solution ApproachA simple solution will be finding all the factors ... Read More

Maximum sum of difference of adjacent elements in C++

Ayush Gupta
Updated on 15-Oct-2020 12:17:54

309 Views

In this problem, we are given a number N. Our task is to create a program to find the Maximum sum of difference of adjacent elements in C++.Problem DescriptionWe will find the Maximum sum of the absolute difference between the adjacent elements of all permutation arrays.Let’s take an example to understand the problem, InputN = 4Output7ExplanationAll permutations of size 4 are : {1, 2, 3, 4} = 1 + 1 + 1 = 3 {1, 2, 4, 3} = 1 + 2 + 1 = 4 {1, 3, 2, 4} = 2 + 1 + 2 = 5 {1, 3, ... Read More

Maximum sum of absolute difference of any permutation in C++

Ayush Gupta
Updated on 15-Oct-2020 12:19:26

591 Views

In this problem, we are given an array. Our task is to create a program to find the Maximum sum of absolute difference of any permutation in C++.Problem DescriptionWe will be finding all permutation of the elements of the given array. And then finding the sum of the absolute difference of adjacent elements of the array. Lastly we will return the maximum of all sums.Let’s take an example to understand the problem, Inputarr[] = {9, 1, 6, 3}Output17ExplanationAll permutations of the array with sum of absolute difference of adjacent elements. {9, 1, 6, 3}, sum= |9-1| + |1-6| + |6-3| ... Read More

Maximum Sum Increasing Subsequence using Binary Indexed Tree in C++

Ayush Gupta
Updated on 15-Oct-2020 12:21:46

145 Views

In this problem, we are given an array arr[] of N elements. Our task is to create a program to find the maximum Sum Increasing Subsequence using Binary Indexed Tree in C++.Let’s take an example to understand the problem, Inputarr[] = {4, 1, 9, 2, 3, 7}Output13ExplanationMaximum increasing subsequence is 1, 2, 3, 7. Sum = 13Solution ApproachTo solve the problem, we will use the Binary Indexed Tree in which we will insert values and map them to binary indexed tree. Then find the maximum value.ExampleProgram to illustrate the working of our solution,  Live Demo#include using namespace std; int ... Read More

Advertisements