Server Side Programming Articles - Page 1395 of 2650

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

138 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

Program to check regular expression pattern is matching with string or not in Python

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

582 Views

Suppose we have a string s and a regular expression pattern. We have to check whether the given pattern matches with given string or not. In the regular expression, there are few rules −. (period) which matches any single character* (asterisk) which matches zero or more of the preceding element.So, if the input is like pattern = "h.l*o" s = "hello", then the output will be True, as We have ra and then a single characterTo solve this, we will follow these steps −n := size of sm := size of pDefine a function dp() . This will take i, ... 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

526 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

193 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

126 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

Program to count number of unique paths that includes given edges in Python

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

364 Views

Suppose we have a list of edges in the form (u, v) and these are representing a tree. For each edge we have to find the total number of unique paths that includes said edge, in the same order as given in the input.So, if the input is like edges = [[0, 1], [0, 2], [1, 3], [1, 4]]then the output will be [6, 4, 4, 4].To solve this, we will follow these steps −adj := adjacency list from given edgescount := an empty mapDefine a function dfs() . This will take x, parentcount[x] := 1for each nb in adj[x], ... 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

199 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

Program to count number of points that lie on a line in Python

Arnab Chakraborty
Updated on 22-Dec-2020 08:35:06

692 Views

Suppose we have a list of coordinates. Each coordinate has two values x and y, representing a point on the Cartesian plane. Now find the maximum number of points that lie on some line.So, if the input is like coordinates = [[6, 2], [8, 3], [10, 4], [1, 1], [2, 2], [6, 6], [7, 7]], then the output will be 4, as the points are [1, 1], [2, 2], [6, 6], [7, 7]] that lies on a line.To solve this, we will follow these steps −res := 0for i in range 0 to size of points list, do(x1, y1) := ... Read More

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

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

275 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

112 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

Advertisements