Found 33676 Articles for Programming

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

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

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

683 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

263 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

107 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

168 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 find minimum cost to pick up gold in given two locations in Python

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

152 Views

Suppose we have a 2d matrix and some other values like row, col, erow0, ecol0, erow1, and ecol1. If our current position is matrix [row, col] and we want to pick up gold that is at matrix [erow0, ecol0] and matrix [erow1, ecol1]. We can go up, down, left, and right but when we are at a cell (r, c), we have to pay cost matrix [r, c], although if we land at a cell more than once, we do not need to pay cost for that cell again. We have to find the minimum cost to pick up gold ... 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

451 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 size of each partition of a list where each letter appears at most one piece in Python

Arnab Chakraborty
Updated on 22-Dec-2020 08:24:27

217 Views

Suppose we have a lowercase string s, we can partition s into as many pieces as possible such that each letter appears in at most one piece and find the sizes of the partitions as a list.So, if the input is like s = "momoplaykae", then the output will be [4, 1, 1, 4, 1], as the string is split into ["momo", "p", "l", "ayka", "e"].To solve this, we will follow these steps −count := a map that contains characters in s and their occurrencesout := a new list, stk := an empty stacklength := 0for each char in s, ... Read More

Program to count number of paths with cost k from start to end point in Python

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

309 Views

Suppose we have a 2d binary matrix and another value k. Now starting from the top-left cell, we have to go to the bottom right cell. In one step, we can only go down, or right. Now the score of a path is the sum of the values on the cells on the path. We have to find the number of paths from the start cell to end cell with score k. If there are huge possible ways then return result mod 10^9+7.So, if the input is like001101010K = 2, then the output will be 4, as the paths with ... Read More

Program to find minimum cost to paint fences with k different colors in Python

Arnab Chakraborty
Updated on 22-Dec-2020 08:20:48

308 Views

Suppose we want to paint a row of N fences with K different colors. We want to minimize the cost while ensuring that no two neighboring fences are of the same color. So if we have an N x K matrix where the nth row and kth column represents the cost to paint the nth fence with kth color, we have to find the minimum cost which achieves this goal.So, if the input is like645327345544then the output will be 14, as we can select the following color indices (starting from the first fence) − 5 → 2 → 3 → ... Read More

Advertisements