Found 7347 Articles for C++

Program to find k where given matrix has k by k square of same value in C++

Arnab Chakraborty
Updated on 23-Dec-2020 06:21:22

101 Views

Suppose we have a 2d matrix, we have to find the largest k × k submatrix where all of its elements are containing the same value, then find the value of k.So, if the input is like1183155525554555then the output will be 3, as there is a 3 × 3 square matrix of value 5.To solve this, we will follow these steps −n := row count of matrixm := column count of matrixDefine one 2D array dp of size (n x m) and fill with 1ret := 1for initialize i := n - 1, when i >= 0, update (decrease i ... Read More

Program to count number of palindromes after minimum number of split of the string in C++

Arnab Chakraborty
Updated on 23-Dec-2020 06:18:41

110 Views

Suppose we have a lowercase string s, we have to split it into as few strings as possible such that each string is a palindrome and then find the number of strings.So, if the input is like s = "levelracecar", then the output will be 2, as there are two palindromes "level" and "racecar".To solve this, we will follow these steps −n := size of ADefine an array result of size (n + 1)result[n] := -1for initialize i := n - 1, when i >= 0, update (decrease i by 1), do −result[i] := n - i - 1for initialize ... Read More

Program to find minimum largest sum of k sublists in C++

Arnab Chakraborty
Updated on 23-Dec-2020 06:17:00

119 Views

Suppose we have a list of numbers called nums and another value k. We can split the list into k non-empty sublists. We have to find the minimum largest sum of the k sublists.So, if the input is like nums = [2, 4, 3, 5, 12] k = 2, then the output will be 14, as we can split the list like: [2, 4, 3, 5] and [12].To solve this, we will follow these steps −Define a function ok(), this will take array v, k, x,cnt := 0, sum := 0for each element i in v −if sum + i > x, then −sum := i(increase cnt by 1)Otherwisesum := sum + ireturn true when cnt

Program to find smallest difference between picked elements from different lists in C++

Arnab Chakraborty
Updated on 23-Dec-2020 06:08:58

111 Views

Suppose we have a list of lists, we have to find the smallest difference that can be formed by picking one value from each of the lists and taking the difference between the maximum and the minimum number of the picked element.So, if the input is like lists = [ [30, 50, 90], [85], [35, 70]], then the output will be 20, as we can take 90, 85, 70 and 90 - 70 = 20To solve this, we will follow these steps −maxVal := -infret := infdefine a priority queue pqn := size of listsfor initialize i := 0, when ... Read More

Program to find a triplet nums[i] < nums[k] < nums[j] from a list nums in C++

Arnab Chakraborty
Updated on 22-Dec-2020 08:57:29

222 Views

Suppose we have a list of numbers called nums, we have to check whether there are triplets (i, j, k) such that i < j < k and nums[i] < nums[k] < nums[j].So, if the input is like nums = [2, 12, 1, 4, 4], then the output will be True, as [2, 12, 4] matches the criteria because 2 < 4 < 12.To solve this, we will follow these steps −n := size of numsDefine an array left of size nleft[0] := nums[0]for initialize i := 1, when i < n, update (increase i by 1), do −left[i] := ... Read More

Query for ancestor-descendant relationship in a tree in C++ Program

sudhir sharma
Updated on 22-Dec-2020 08:48:39

167 Views

In this problem, we are given an N vertex tree and Q queries each consisting of two values i and j. Our task is to create a program to solve a Query for ancestor-descendant relationship in a tree.To solve each query, we need to check whether the node i is the ancestor of node j in the tree.Let’s take an example to understand the problem, InputQ = 2, query[][] = {{3, 5}, {1, 6}}OutputNo YesExplanationi = 3, j = 5: The node 3 is not the ancestor of node 5. Return NO. i = 1, j = 6: The node ... Read More

Queries to update a given index and find gcd in range in C++ Program

sudhir sharma
Updated on 22-Dec-2020 08:46:39

169 Views

In this problem, we are given an array arr[] of size N and Q queries which can be of two types. Our task is to create a program to solve the queries to update a given index and find GCD in the range.Queries are −Type 1 − {1, index, value} - increase the element at given index by value.Type 2 − {2, L, R} - find the GCD of elements in the index range [L, R].Problem Description − We need to find the GCD of the elements that are in the range [L, R] and return the value.Let’s take an ... Read More

Program to check we can replace characters to make a string to another string or not in C++

Arnab Chakraborty
Updated on 22-Dec-2020 08:45:22

118 Views

Suppose we have two lowercase strings s and t. Now consider an operation where we replace all occurrences of a character in s with another character. If we can perform this operation any number of times, we have to check whether s can be converted to t or not.So, if the input is like s = "eye" t = "pip", then the output will be True, as we can replace "e"s with "p"s then "y" by "i".To solve this, we will follow these steps −Define one map m1 and another map m2n := size of sfor initialize i := 0, ... Read More

Program to find number of operations required to remove palindromic sublists in C++

Arnab Chakraborty
Updated on 22-Dec-2020 08:43:21

184 Views

Suppose we have a list of numbers called nums. Now let us consider an operation where we delete some sublist which is a palindrome. We have to find the minimum number of operations required such that the list is empty.So, if the input is like nums = [6, 2, 4, 4, 2, 10, 6], then the output will be 2, as we can remove the sublist [2, 4, 4, 2] first then the list is like [6, 10, 6] this is also a palindrome, so remove it to make list empty.To solve this, we will follow these steps −Define an ... Read More

Queries to return the absolute difference between L-th smallest number and the R-th smallest number in C++ Program

sudhir sharma
Updated on 22-Dec-2020 08:44:35

65 Views

In this problem, we are given an array arr[] of size n and Q queries each consisting of 2 values L and R. Our task is to create a program to solve queries to return the absolute difference between L-th smallest number and the R-th smallest number.Problem Description − To solve each query, we need to find the index of Lth smallest and Rth smallest number. And find the difference between these indices.Let’s take an example to understand the problem, Inputarr[] = {8, 4, 1, 5, 2} Q = 2 Queries[][] = {{2, 4}, {1, 5}}Output1 2ExplanationFor {2, 4}: 2nd ... Read More

Advertisements