Server Side Programming Articles - Page 1502 of 2650

Program to find minimum number of intervals to be removed to remove overlaps in C++

Arnab Chakraborty
Updated on 20-Oct-2020 10:37:18

500 Views

Suppose we have a set of intervals; we have to find the minimum number of intervals that should be removed to make the rest of the intervals non-overlapping. So if the intervals are [[8, 10], [3, 5], [6, 9]], then the output will be 1, as we have to remove [6, 9] to make all others are non-overlapping.To solve this, we will follow these steps −n := size of arrayif n is 0, then return 0count := 1sort the array based on the end time of the intervalsend := end date of the first intervalfor i in range 1 to ... Read More

Program to remove duplicate entries in a linked list in Python

Arnab Chakraborty
Updated on 20-Oct-2020 10:34:42

219 Views

Suppose we have a linked list of numbers, we have to remove those numbers which appear multiple times in the linked list (hold only one occurrence in the output), we also have to maintain the order of the appearance in the original linked list.So, if the input is like [2 -> 4 -> 6 -> 1 -> 4 -> 6 -> 9], then the output will be [2 -> 4 -> 6 -> 1 -> 9].To solve this, we will follow these steps −if node is not null, thenl := a new settemp := nodeinsert value of temp into lwhile ... Read More

Program to update elements in a given range in Python

Arnab Chakraborty
Updated on 20-Oct-2020 10:31:20

351 Views

Suppose we have a list of numbers called nums and a list of operations. Here each operation has three fields [L, R, X], this indicated that we should increment by X all the elements from indices L to R (inclusive). We have to apply all operations and return the final list.So, if the input is like nums = [8, 4, 2, -9, 4] operations = [ [0, 0, 3], [1, 3, 2], [2, 3, 5] ], then the output will be [11, 6, 9, -2, 4], as the initial list was [8, 4, 2, -9, 4].Performing first operation [0, 0, ... Read More

Program to find how many total amount of rain we can catch in Python

Arnab Chakraborty
Updated on 20-Oct-2020 07:45:17

148 Views

Suppose we have an array of n non-negative integers. These are representing a height where the width of each bar is 1, we have to compute how much water it is able to catch after raining. So the map will be like −Here we can see there are 8 blue boxes, so the output will be 8.To solve this, we will follow these steps −Define a stack st, water := 0 and i := 0while i < size of heightif is stack is empty or height[stack top] >= height[i], then push i into stack, increase i by 1otherwisex := stack ... Read More

Program to find maximum product of contiguous subarray in Python

Arnab Chakraborty
Updated on 20-Oct-2020 07:43:33

1K+ Views

Suppose we have an array called nums, we have to find the product of elements of a contiguous subarray within an array (containing at least one number) which has the largest product. So if the array is [1, 9, 2, 0, 2, 5], the output will be 18, as contiguous subarray [1, 9, 2] has max product.To solve this, we will follow these steps −max_list := list of size nums, and fill with 0min_list := list of size nums, and fill with 0min_list := list of size nums, and fill with 0for i in range 1 to length of numsmax_list[i] ... Read More

Program to evaluate Postfix Notation in C++

Arnab Chakraborty
Updated on 20-Oct-2020 07:41:51

14K+ Views

Suppose we have postfix expression and we have to evaluate the value. Postfix expression is also known as Reverse polish notation. Here we have to use the stack data structure to solve the postfix expressions.So if the expression is “21+3*”, then the answer will be 9.Let us see the steps −for each character ch in the postfix expression, doif ch is an operator $\odot$ , thena := pop first element from stack, b := pop second element from the stackres := b $\odot$ apush res into the stackelse if ch is an operand, thenadd ch into the stackreturn element of ... Read More

Program to find maximum value we can get in knapsack problem by taking multiple copies in Python

Arnab Chakraborty
Updated on 20-Oct-2020 07:39:39

618 Views

Suppose we have two lists of same length they are called weights and values, and we also have another value capacity. Here weights[i] and values[i] represent the weight and value of the ith item. If we can take at most capacity weights, and that we can take any number of copies for each item, we have to find the maximum amount of value we can get.So, if the input is like weights = [1, 2, 3], values = [1, 5, 3], capacity = 5, then the output will be 11To solve this, we will follow these steps −Define a function ... Read More

Program to count number of perfect squares are added up to form a number in C++

Arnab Chakraborty
Updated on 20-Oct-2020 07:37:58

127 Views

Suppose we have a positive number n, we have to find the least number of perfect square numbers whose sum is same as n. So if the number is 10, then the output is 2, as the numbers are 10 = 9 + 1.To solve this, we will follow these steps −create one table for dynamic programming, of length n + 1, and fill it with infinitydp[0] := 0for i := 1, when i*i dp(n+1,INF);       dp[0] = 0;       for(int i =1;i*i

Program to count number of ways we can distribute coins to workers in Python

Arnab Chakraborty
Updated on 20-Oct-2020 07:36:17

352 Views

Suppose we have two lists of positive numbers called coins and salaries. Here coins[i] indicates the value for coin i and salaries[j] indicates the least amount of salary required to pay for worker j. Now suppose we have one coin per type and we must give each worker exactly one coin, we have to compute the number of ways to give coins to each worker. Here two ways are different if some worker receives one type of coin in one way but a different type of coin in the other way. If the result is very large return result mod ... Read More

Program to count how many ways we can divide the tree into two trees in Python

Arnab Chakraborty
Updated on 20-Oct-2020 07:31:58

213 Views

Suppose we have a binary tree containing values 0, 1 and 2. The root has at least one 0 node and one 1 node. Now suppose there is an operation where we delete an edge in the tree and the tree becomes two different trees. We have to find the number of ways we can delete one edge such that none of the two trees contain both a 0 node and a 1 node.So, if the input is likethen the output will be 1 as we can only delete the 0 to 2 edge.To solve this, we will follow these ... Read More

Advertisements