Found 7197 Articles for C++

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

298 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

269 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

255 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

196 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

266 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

128 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

Queries to check if substring[L…R] is palindrome or not in C++ Program

sudhir sharma
Updated on 22-Dec-2020 08:41:07

510 Views

In this problem, we are given string str, Q number of queries each consisting of two values L and R for substring[L...R]. Our task is to create a program to solve Queries to check if substring[L…R] is palindrome or not.Problem Description − For solving each query, we need to check whether the substring created within the range L to R is a palindrome or not.Let’s take an example to understand the problem, Inputstr = “abccbeba” , Q = 3 Query[][] = {{1, 4}, {0, 6}, {4, 6}}OutputPalindrome Not Palindrome PalindromeExplanationCreating all substring for the given substrings : Substring[1...4] = “bccb”, ... Read More

Program to count number of square submatix of 1s in the given matrix in C++

Arnab Chakraborty
Updated on 22-Dec-2020 08:38:59

183 Views

Suppose we have a 2d binary matrix, we have to find the total number of submatrices with all 1 s.So, if the input is like110110001then the output will be 10, as there five 1 x 1 matrix, two 2 x 1 matrix. two 1 x 2 matrices. And one 2 x 2 matrix.To solve this, we will follow these steps −Define a function getAns(), this will take an array a, ret := 0n := size of aDefine an array v of size nDefine one stack stfor initialize i := 0, when i < size of a, update (increase i by ... Read More

Queries to check if it is possible to join boxes in a circles in C++ Program

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

116 Views

In this problem, we are given a number n that denotes n boxes that lie on the edge of a circle. And there are Q queries each consisting of two integers, a and b. Our task is to create a program to solve queries to check if it is possible to join boxes in a circles.Problem Description − To solve each query, we need to check the possibility of connecting box a and box b by a rod in such a way that the intersection of boxes from the last query cannot be disturbed. We need to print possible or ... Read More

Queries to check if a number lies in N ranges of L-R in C++ Program

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

192 Views

In this problem, we are given a 2-D matrix arr[][2] that consists of n ranges (L, R), L-R. And Q queries each consisting of an integer value. Our task is to create a program to solve the Queries to check if a number lies in N ranges of L-R.Problem Description − Here, we solve each query such that each element of the query lies in any one of the ranges.their cannot be overlapping of ranges.Let’s take an example to understand the problem, Inputarr[n][2] = { {5, 7}, {1, 3}, {9, 12} } n = 3 Q = 2, query = ... Read More

Advertisements