Server Side Programming Articles - Page 1546 of 2650

Program to find sum of the sum of all contiguous sublists in Python

Arnab Chakraborty
Updated on 05-Oct-2020 12:30:30

436 Views

Suppose we have a list of numbers called nums, now consider every contiguous subarray. Sum each of these subarray and return the sum of all these values. Finally, mod the result by 10 ** 9 + 7.So, if the input is like nums = [3, 4, 6], then the output will be 43, as We have the following subarrays − [3] [4] [6] [3, 4] [4, 6] [3, 4, 6] The sum of all of these is 43.To solve this, we will follow these steps −N:= size of numsans:= 0for i in range 0 to size of nums, don:= nums[i]ans ... Read More

Program to check we can reach end of list by starting from k in Python

Arnab Chakraborty
Updated on 05-Oct-2020 12:27:54

382 Views

Suppose we have a list of numbers called nums and another number k. If we start at index k and at any index i, we can go either left or right by exactly nums[i] number of steps. We have to check whether we can reach the end of the list or not.So, if the input is like nums = [0, 0, 2, 1, 3, 3, 1, 1] k = 2, then the output will be True, as if we start at index 2, then jump to index 4 and then jump to the last index 7.To solve this, we will ... Read More

Program to find how many ways we can climb stairs (maximum steps at most k times) in Python

Arnab Chakraborty
Updated on 05-Oct-2020 12:28:55

264 Views

Suppose we have a staircase with n steps and we also have another number k, initially we are at stair 0, and we can climb up either 1, 2 or 3 steps at a time. but we can only climb 3 stairs at most k times. Now we have to find the number of ways we can climb the staircase.So, if the input is like n = 5, k = 2, then the output will be 13, as there are different ways we can climb the stairs −[1, 1, 1, 1, 1][2, 1, 1, 1][1, 2, 1, 1][1, 1, 2, ... Read More

Program to find how many ways we can climb stairs in Python

Arnab Chakraborty
Updated on 05-Oct-2020 12:25:32

350 Views

Suppose we have a staircase with n steps, and we can climb up either 1 or 2 steps at a time. We have to define a function that returns the number of unique ways we can climb the staircase.The order of the steps should not be changed, so each different order of steps counts as a way. If the answer is very large then mod the result by 10^9 + 7So, if the input is like n = 5, then the output will be 8, as there are 8 unique ways −1, 1, 1, 1, 12, 1, 1, 11, 2, ... Read More

Program to find minimum number of busses are required to pass through all stops in Python

Arnab Chakraborty
Updated on 05-Oct-2020 12:35:51

2K+ Views

Suppose we have a list of numbers called nums and that is showing the bus stops on a line where nums[i] shows the time a bus must arrive at station i. Now that buses can only move forward, we have to find the minimum number of buses that are needed to pass through all the stops.So, if the input is like nums = [1, 2, 7, 9, 3, 4], then the output will be 2, as one bus can take stops [1, 2, 3, 4] and another can do [7, 9].To solve this, we will follow these steps−ans := 0seen ... Read More

Program to add two binary strings, and return also as binary string in C++

Arnab Chakraborty
Updated on 05-Oct-2020 12:19:16

1K+ Views

Suppose we have two binary strings a and b, we have to add these binary numbers and find their sum, also as a string.So, if the input is like a = "10110", b = "10010", then the output will be "101000".To solve this, we will follow these steps −ret := empty stringna := size of a, nb := size of bi := na - 1, j := nb - 1carry := 0while (i >= 0 or j >= 0), do:addA := (if i >= 0, then a[i] - ASCII of '0', otherwise 0)addB := (if j >= 0, then b[j] ... Read More

Program to find maximum amount we can get by taking different items within the capacity in Python

Arnab Chakraborty
Updated on 05-Oct-2020 12:15:03

591 Views

Suppose we have two lists called weights and values which are of same length and another number called capacity k. Here weights[i] and values[i] shows the weight and value of the ith item. Now, we can take at most k capacity weights, and that we can only take at most one copy of each item, we have to find the maximum amount of value we can get.So, if the input is like weights = [2, 3, 4], values = [2, 6, 4], capacity = 6, then the output will be 8To solve this, we will follow these steps −n:= size ... Read More

Program to find the sum of elements that forms a Z shape on matrix in Python

Arnab Chakraborty
Updated on 05-Oct-2020 12:12:57

470 Views

Suppose we have one n x n matrix M, we have to find the sum of all elements that form a Z shape in the matrix.So, if the input is like432918256then the output will be 23, as elements are [4+3+2+1+2+5+6] = 23.To solve this, we will follow these steps −n := row count of matrixif n

Program to check we can spell out the target by a list of words or not in Python

Arnab Chakraborty
Updated on 05-Oct-2020 12:23:00

202 Views

Suppose we have a list of numbers called nums and another number k. If we start at index k and at any index i, we can go either left or right by exactly nums[i] number of steps. We have to check whether we can reach the end of the list or not.So, if the input is like nums = [0, 0, 2, 1, 3, 3, 1, 1] k = 2, then the output will be True, as if we start at index 2, then jump to index 4 and then jump to the last index 7.To solve this, we will ... Read More

Program to find the maximum profit we can get by buying on stock market multiple times in Python

Arnab Chakraborty
Updated on 05-Oct-2020 12:08:38

314 Views

Suppose we have a list of prices representing the stock prices of a company in chronological sequence, we have to find the maximum profit we could have made from buying and selling that stock any number of times. We have to keep in mind that we must buy before we can sell it.So, if the input is like prices = [10, 50, 30, 40, 60], then the output will be 70, as We can buy at 10, sell at 50, buy at 30, and sell at 60.To solve this, we will follow these steps −prev_price := infinityprofit := 0for each ... Read More

Advertisements