Found 33676 Articles for Programming

Program to maximize the number of equivalent pairs after swapping in Python

Arnab Chakraborty
Updated on 19-Oct-2020 15:21:26

235 Views

Suppose we have a list of numbers A and list of numbers B of same length. We also have a 2D list of numbers C where each element is of the form [i, j] this indicates we can swap A[i] and A[j] as many times as we want. We have to find the maximum number of pairs where A[i] = B[i] after the swapping.So, if the input is like A = [5, 6, 7, 8], B = [6, 5, 8, 7], C = [[0, 1], [2, 3]], then the output will be 4, as we can swap A[0] with A[1] ... Read More

Program to maximize the minimum value after increasing K sublists in Python

Arnab Chakraborty
Updated on 19-Oct-2020 15:19:15

245 Views

Suppose we have a list of numbers called nums and two values, size and k. Now suppose there is an operation where we take a contiguous sublist of length size and increment every element by one. We can perform this operation k times, we have to find the largest minimum value possible in nums.So, if the input is like nums = [2, 5, 2, 2, 7], size = 3, k = 2, then the output will be 3, as we can increase [2, 5, 2] to get [3, 6, 3, 2, 7] and then increment [6, 3, 2] to get ... Read More

Program to make pairwise adjacent sums small in Python

Arnab Chakraborty
Updated on 19-Oct-2020 15:16:59

340 Views

Suppose we have a list of non-negative numbers say nums and a non-negative value k. Now suppose we can perform an operation where we select a single positive umber in nums and decrease it by 1. We have to find the minimum number of operations required such that every pair of adjacent values in the list sums

Program to find number of coins needed to make the changes with given set of coins in Python

Arnab Chakraborty
Updated on 19-Oct-2020 15:15:15

271 Views

Suppose we have coins of different denominations and a total amount of money amount. We have to define one function to compute the fewest number of coins that we need to make up that amount. When that amount of money cannot be accommodated by any combination of the coins, return -1. So if the input is [1, 2, 5], and the amount is 64, the output is 14. This is formed using 12*5 + 2 + 2 = 64.To solve this, we will follow these steps −if amount = 0, then return 0if minimum of coins array > amount, then ... Read More

Program to find number of coins needed to make the changes in Python

Arnab Chakraborty
Updated on 19-Oct-2020 15:13:36

1K+ Views

Suppose we have coins of different denominations (1, 5, 10, 25) and a total amount of money amount. We have to define one function to compute the fewest number of coins that we need to make up that amount. So if the input is 64, the output is 7. This is formed using 25 + 25 + 10 + 1 + 1 + 1 + 1 = 64.To solve this, we will follow these steps −if amount = 0, then return 0if minimum of coins array > amount, then return -1define one array called dp, of size amount + 1, ... Read More

Program to convert one list identical to other with sublist sum operation in Python

Arnab Chakraborty
Updated on 19-Oct-2020 15:11:05

167 Views

Suppose we have two lists l1 and l2, we have to make the lists equal by applying this operation repeatedly − Choose a sublist, and replace the whole sublist with its sum. Finally return the size of the longest resulting list possible after applying above operations. If there's no solution, return -1.So, if the input is like l1 = [1, 4, 7, 1, 2, 10] l2 = [5, 6, 1, 3, 10], then the output will be 4, as if we perform this operation as follows −Take l1's sublist [1, 4] we get [5, 7, 1, 2, 10]Take l1's sublist ... Read More

How to exclude extra margin between points and the axes for a plot created by using ggplot2 in R?

Nizamuddin Siddiqui
Updated on 19-Oct-2020 15:02:27

1K+ Views

In a plot created by using ggplot package there exists an extra area around all the sides of the plot which uses extra space, thus we might want to get rid of that space by removing that extra margin area. It can be done by setting the scale for both the axes to zero with the help of scale_x_continuous and scale_y_continuous function.Consider the below data frame −Example Live Demoset.seed(151) x

How to find the intersection between two or more lists in R?

Nizamuddin Siddiqui
Updated on 19-Oct-2020 15:01:22

8K+ Views

The intersection of lists means the elements that are unique and common between the lists. For example, if we have a list that contains 1, 2, 3, 3, 3, 2, 1 and the other list that contains 2, 2, 1, 2, 1 then the intersection will return only those elements that are common between the lists and also unique, hence for this example we will get 1 and 2. In R, we can do this by using intersection function along with Reduce function.Consider the below lists −Example Live DemoList1

How to apply manually created x-axis labels in a histogram created by hist function in R?

Nizamuddin Siddiqui
Updated on 19-Oct-2020 14:57:52

3K+ Views

When we generate a histogram in R using hist function, the x-axis labels are automatically generated but we might want to change them to values defined by researchers or by any other authority. Therefore, firstly we need to create the histogram by ignoring the labels and then axis function can be used for new values.Consider the below vector x and create a histogram of x by ignoring x-axis labels −Exampleset.seed(1999) x

How to find the sum of diagonal elements in a table in R?

Nizamuddin Siddiqui
Updated on 19-Oct-2020 14:57:23

2K+ Views

The sum of diagonal elements could be required in matrix analysis therefore, we can convert the matrix into a table and find the sum of diagonal elements. This can be easily done by using sun function by extracting diagonal elements of the table using diag function. For example, if we have a table T then the sum of diagonal elements of T can be found as sum(diag(T)).Example Live DemoTable1

Advertisements