Found 33676 Articles for Programming

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

285 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 get maximum value of power of a list by rearranging elements in Python

Arnab Chakraborty
Updated on 22-Dec-2020 06:44:36

658 Views

Suppose we have a list nums of N positive numbers. Now we can select any single value from the list, and move (not swap) it to any position. We can also not move any to position at all. So we have to find what is the maximum possible final power of the list? As we know the power of a list is the sum of (index + 1) * value_at_index over all indices i.$$\displaystyle\sum\limits_{i=0}^{n-1} (i+1)\times list[i]$$So, if the input is like nums = [6, 2, 3], then the output will be 26, as we can move the 6 to the ... Read More

Program to find length of longest increasing subsequence with at least k odd values in Python

Arnab Chakraborty
Updated on 22-Dec-2020 06:40:36

246 Views

Suppose we have a list of numbers called nums and another value k, we have to find the size of the longest increasing subsequence with at least k odd elements.So, if the input is like nums = [12, 14, 16, 5, 7, 8] k = 2, then the output will be 3, as the longest increasing subsequence with at least 2 odd values is [5, 7, 8].To solve this, we will follow these steps −best := 0Define a function dp() . This will take i, j, odd, takenif odd >= k, thenbest := maximum of best and takenif j is ... 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

205 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

Program to get next integer permutation of a number in C++

Arnab Chakraborty
Updated on 22-Dec-2020 06:36:12

445 Views

Suppose we have a number n, we have to find the next bigger permutation of its digits. When n is already in its largest permutation, then rotate it down to the smallest permutation.So, if the input is like n = 319, then the output will be 391.To solve this, we will follow these steps −Define a function makeArray(), this will take x, Define an array retwhile x is non-zero, do −insert x mod 10 at the end of retx := x / 10reverse the array retreturn retDefine a function combine(), this will take an array v, ret := 0for each ... Read More

Program to check whether we can get N queens solution or not in Python

Arnab Chakraborty
Updated on 22-Dec-2020 06:32:49

256 Views

Suppose we have a binary matrix where 0 is representing empty cell and 1 is representing a chess queen at that cell. We have to check whether we can fill this board and get a valid nqueen solution or not. As we know the n queens puzzle asks to place n queens on an n × n chessboard so that no two chess queens can attack each other.So, if the input is like1000000000000010000000010then the output will be True, as one solution is like −1000000100000010100000010To solve this, we will follow these steps −Define a function isSafe() . This will take board, ... Read More

Program to find length of longest valid parenthesis from given string in Python

Arnab Chakraborty
Updated on 22-Dec-2020 06:27:58

130 Views

Suppose we have a string s. This s consists of opening and closing parenthesis only. We have to find the length of the longest valid (well-formed) parentheses substring. So if the input is like “))(())())”, then the result will be 6, as the valid string is “(())()”.To solve this, we will follow these steps −Make a stack, and insert -1., set ans := 0for i in range 0 to length of stack – 1if s[i] is opening parentheses, then insert i into stackotherwiseif stack is not empty and top of stack is not -1 and s[stack top] is opening parentheses, ... Read More

Program to find maximum price we can get by holding items into a bag in Python

Arnab Chakraborty
Updated on 22-Dec-2020 06:26:20

358 Views

Suppose we have two lists of numbers. One is called weights and another is called values. These are of same length, We also have two values called capacity and count. Here weights[i] and values[i] represent the weight and value of the ith item. We can hold at most capacity weight and at most count items in total, and we can take only one copy of each item, so we have to find the maximum amount of value we can get.So, if the input is like weights = [2, 2, 4, 6] values = [15, 15, 20, 35] capacity = 8 ... Read More

Program to get final position of moving animals when they stops in Python

Arnab Chakraborty
Updated on 22-Dec-2020 06:24:50

210 Views

Suppose we have a string s that is representing the initial conditions of some animals. Each animal can take one of three values: L, indicates the animal moved to left. R, indicates the animal moved to right. @, indicates the animal is standing still. Animals moving on a direction will pick up other animals unless the animal receives a force from the opposite direction. Then, it will stand still. We have to find the orientation of each animal when the animal stop moving.So, if the input is like s = "@@L@R@@@@L", then the output will be "LLL@RRRLLL"To solve this, we ... Read More

Program to find minimum number of flips required to have alternating values in Python

Arnab Chakraborty
Updated on 22-Dec-2020 06:22:54

421 Views

Suppose we have a binary string s. Now suppose we can take some prefix of s and move it to the back. then, find the minimum number of characters that need to be flipped such that there will be no consecutive characters of the same value.So, if the input is like s = "10010101111", then the output will be 2, as we can take prefix "10", then move it to back so string is "01010111110" then flip 3rd and 5th bit from right to 0 ("01010101010").To solve this, we will follow these steps −ans := size of SN := size ... Read More

Advertisements