Found 7347 Articles for C++

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

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

406 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

108 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

57 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

141 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

Queries for number of distinct integers in Suffix in C++ Program

sudhir sharma
Updated on 22-Dec-2020 08:33:55

191 Views

In this problem, we are given an array arr[] of n integer values. And Q queries each having an integer k. Our task is to create a program to solve the Queries for number of distinct integers in Suffix.Problem Description − We need to solve queries for distinct integers in suffix. For each query we need to find the number of unique elements from k to n i.e. count unique elements from arr[k] to arr[n].The array taken is 1 indexed.Let’s take an example to understand the problem, Inputarr[ ] = {5, 1, 2, 1, 6 , 5}, n = 6, ... Read More

Program to count number of ways we can place nonoverlapping edges to connect all nodes in C++

Arnab Chakraborty
Updated on 22-Dec-2020 08:32:18

73 Views

Suppose we have a number n that is representing the number of nodes that are placed circularly. We have to find the number of ways we can place n / 2 edges such that every node is connected by an edge, and that edges does not intersect with each other. If the answer is very large then return result mod 10^9 + 7.So, if the input is like n = 4, then the output will be 2, as we can group them like below −To solve this, we will follow these steps −Define an array dp of size (n/2 + ... Read More

Maximums from array when the maximum decrements after every access in C++ Program

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

100 Views

In this problem, we are given an array arr[] of N integers and an integer m. Our task is to create a program to find the maximums from array when the maximum decrements after every access.Problem Description − We need to find the maximum sum of maximum elements of the array and decrease the max taken by one k times.Let’s take an example to understand the problem, Inputarr[] = {3, 6, 7, 8, 8}, k = 3OutputExplanationFirst iteration: array before = {3, 6, 7, 8, 8}, max = 8, sum = 8, array after update = {3, 6, 7, 7, ... Read More

Program to check whether given words are maintaining given pattern or not in C++

Arnab Chakraborty
Updated on 22-Dec-2020 08:26:07

378 Views

Suppose we have a pattern p and a string str, we have to check whether str follows the same pattern or not. Here follow means there is a bijection between a letter in pattern and a non-empty word in str.So, if the input is like pattern = "cbbc", str = "word pattern pattern word", then the output will be True.To solve this, we will follow these steps −strcin := strDefine an array wordsfor each word in strcininsert word at the end of wordsDefine one map p2ii := 0pat := empty stringfor c in pattern −if c is not member of ... Read More

Program to find maximum number of package that can be bought by buyers in C++

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

166 Views

Suppose we have two lists sales and buyers. Each element in sales contains two values in the form [day, price] this indicates the package is available for sale only on that day for that given price. And each element in buyers in the form [payday, amount] indicates that the buyer has that amount of money to spend on payday and afterward. If each buyer can buy at most one package, and each package can be sold to only one person, find the maximum number of packages that can be bought.So, if the input is like sales = [[0, 5], [0, ... Read More

Program to find number of operations needed to decrease n to 0 in C++

Arnab Chakraborty
Updated on 22-Dec-2020 06:38:57

144 Views

Suppose we have a number n. Now consider an operation where we can either 1. Decrement n by one 2. If n is even number, then decrement it by n / 2 3. If n is divisible by 3, then decrement by 2 * (n / 3) Finally find the minimum number of operations required to decrement n to zero.So, if the input is like n = 16, then the output will be 5, as n = 16 even then decreases it by n/2 4 times, it will be 1. Then reduce it by 1 to get 0. So total ... Read More

Advertisements