C++ Articles - Page 324 of 719

Maximum value in an array after m range increment operations in C++

Ayush Gupta
Updated on 15-Oct-2020 11:52:25

292 Views

In this problem, we are given an array arr[] of N elements initialized with 0. Our task is to create a program to find the Maximum value in an array after m range increment operations in C++.Problem DescriptionOn the array, we will perform m range increment operation of the type, update[L, R, K] = Add value K, to all elements in the range.After performing m operations on the array. We need to find the element with maximum value in the array.Let’s take an example to understand the problem, InputN = 6, m = 4 Update[][] = {{1, 4, 12}, {0, ... Read More

Maximum trains for which stoppage can be provided in C++

Ayush Gupta
Updated on 15-Oct-2020 11:56:44

318 Views

In this problem, we are given a number N denoting the number of platforms a station has with two tracks each. And T trains will be passing the station whose arrival and departure time are given. Each train stops at a specific station. Our task is to create a program to find the Maximum trains for which stoppage can be provided in C++.Let’s take an example to understand the problem, InputN = 3, T = 5 Trains = {{0915, 0930, 2}, {0930, 0945, 1}, {0930, 1200, 1}, {0910, 0925, 3}, {0940, 1015, 1}}Output4ExplanationThe train schedules are, Train 1: Train will ... Read More

Maximum trace possible for any sub-matrix of the given matrix in C++

Ayush Gupta
Updated on 15-Oct-2020 12:00:47

360 Views

In this problem, we are given a two-dimensional array arr[][]. Our task is to create a program to find the Maximum trace possible for any sub-matrix of the given matrix in C++.Problem Descriptionwe need to find the maximum trace for any sub-matrix. The trace is the sum of elements of the main diagonal of the matrix.Let’s take an example to understand the problem, Inputarr[][] ={{-2, 5, 3},           {1, 6, 2},           {4, 3, 9}}Output15ExplanationFor the sub-array: {1, 6} {9, 3}Solution ApproachA simple solution is to find the max sum using the ... Read More

Maximum sum such that no two elements are adjacent - Set 2 in C++

Ayush Gupta
Updated on 15-Oct-2020 12:05:22

219 Views

In this problem, we are given an array arr[]. Our task is to create a program to find the Maximum sum such that no two elements are adjacent in C++.Problem DescriptionWe need to find the maximum sum of the sequence from the array such that no 2 numbers from the sum sequence are adjacent in the array.Let’s take an example to understand the problem, Inputarr[] = {5, 1, 3, 7, 9, 2, 5}Output22ExplanationTaking sum sequence from index 0 with alternate elements : 5 + 3 + 9 + 5 = 22 Taking sum sequence from index 1 with alternate elements ... Read More

Maximum sum such that no two elements are adjacent in C++

Ayush Gupta
Updated on 15-Oct-2020 12:07:39

246 Views

In this problem, we are given an array arr[]. Our task is to create a program to find the Maximum sum such that no two elements are adjacent in C++.Problem DescriptionWe need to find the maximum sum of the sequence from the array such that no 2 numbers from the sum sequence are adjacent in the array.Let’s take an example to understand the problem, Inputarr[] = {5, 1, 3, 7, 9, 2, 5}Output22ExplanationTaking sum sequence from index 0 with alternate elements : 5 + 3 + 9 + 5 = 22 Taking sum sequence from index 1 with alternate elements ... Read More

Find pairs with given product in a sorted Doubly Linked List in C++

Arnab Chakraborty
Updated on 25-Jul-2020 11:12:21

279 Views

ConceptWith respect of a given sorted doubly linked list of positive distinct elements, our taskis to determine pairs in the doubly linked list whose product is equal to given value x, without consuming any extra space.InputList = 1 2 4 5 6 8 9 x = 8Output(1, 8), (2, 4)InputList1 = 1 2 3 4 5 6 7 x = 6Output(1, 4), (2, 3)MethodAccording to a Simple Approach for this problem, we traverse the linked list implementing two nested loops and determine all pairs and verify for the ... Read More

Find pair of rows in a binary matrix that has maximum bit difference in C++

Arnab Chakraborty
Updated on 25-Aug-2020 12:06:43

192 Views

Suppose we have a binary matrix; we have to find the pair of rows in the given matrix that has maximum bit difference.So, if the input is like matrix, then the output will be [2, 3] as bit difference between rows 2 and row 3 is 4, this is maximum.To solve this, we will follow these steps −Define Trie structure, with value and two children.Define a function get_max_bit_diff(), this will take root of a trie, matrix, n, row_index, temp := root, count := 0for initialize i := 0, when i < n, update (increase i by 1), do−if child[ matrix[row_index, ... Read More

Find pair for given sum in a sorted singly linked without extra space in C++

Arnab Chakraborty
Updated on 25-Aug-2020 12:11:04

181 Views

Suppose we have a singly linked list and a value x; we have to find a pair whose sum is same as x. We have to keep in mind that we cannot use any extra space and expected time complexity will be O(n).So, if the input is like 4→7→8→9→10→11→12, x = 19, then the output will be [(7, 12), (8, 11), (9, 10)]To solve this, we will follow these steps −Define a function convert_to_xor(), this will take start, prev := NULLwhile start is NULL, do −next_list_node := next of startnext of start := XOR of the address of next_list_node and ... Read More

Find original numbers from gcd() every pair in C++

Arnab Chakraborty
Updated on 25-Jul-2020 10:55:18

362 Views

ConceptWith respect of a given array array[] containing GCD of every possible pair of elements of another array, our task is to determine the original numbers which are used to compute the GCD array.Inputarray[] = {6, 1, 1, 13}Output13 6 gcd(13, 13) = 13 gcd(13, 6) = 1 gcd(6, 13) = 1 gcd(6, 6) = 6Inputarr[] = {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 6, 6, 6, 8, 11, 13, 3, 3}Output13 11 8 6 6MethodAt first, sort the array in decreasing order.Largest element will always be one of the ... Read More

Find number of edges that can be broken in a tree such that Bitwise OR of resulting two trees are equal in C++

Arnab Chakraborty
Updated on 25-Jul-2020 10:51:58

315 Views

ConceptWith respect of a given tree with m nodes and a number associated with every node, we canbreak any tree edge which will result in the formation of 2 new trees. Here, we have to count the number of edges in this way so that the Bitwise OR of the nodes present in the two trees constructed after breaking that edge are equal. It should be noted that the value ofevery node is ≤ 10^6.Inputvalues[]={1, 3, 1, 3}      1    / | \   2 3 4Output2Here, the edge between 1 and 2 can be broken, the Bitwise ... Read More

Advertisements