Server Side Programming Articles

Page 487 of 2109

Program to find maximum profit after buying and selling stocks at most two times in python

Arnab Chakraborty
Arnab Chakraborty
Updated on 25-Mar-2026 340 Views

Given a list of stock prices in chronological order, we need to find the maximum profit from buying and selling stocks at most two times. The constraint is that we must buy before selling, and we cannot hold multiple stocks simultaneously. For example, if prices = [2, 6, 3, 4, 2, 9], the maximum profit is 11 by buying at 2, selling at 6 (profit: 4), then buying at 2 and selling at 9 (profit: 7). Algorithm Approach We use dynamic programming to track four states ? first_buy: Maximum profit after first purchase first_sell: Maximum ...

Read More

Program to check a string can be broken into given list of words or not in python

Arnab Chakraborty
Arnab Chakraborty
Updated on 25-Mar-2026 664 Views

Suppose we have a word list and another string s with no spaces. We have to check whether the string can be broken down using the list of words or not. So, if the input is like words = ["love", "python", "we", "programming", "language"] s = "welovepythonprogramming", then the output will be True Algorithm To solve this, we will follow these steps − words := a new set of all unique words Define a function rec() . This will take i if i ...

Read More

Program to find maximum number of boxes we can fit inside another boxes in python

Arnab Chakraborty
Arnab Chakraborty
Updated on 25-Mar-2026 1K+ Views

Suppose we have a list of boxes where each box is represented by [width, height]. We can put one box inside another box only if both the width and height of the first box are smaller than the second box. We need to find the maximum number of boxes we can nest inside each other. Problem Example Given the following boxes ? Width Height 12 12 10 10 6 6 5 10 The output will be 3, as we can fit the box [6, ...

Read More

Program to find length of longest consecutive path of a binary tree in python

Arnab Chakraborty
Arnab Chakraborty
Updated on 25-Mar-2026 304 Views

Suppose we have a binary tree; we have to find the longest consecutive path in the binary tree. A consecutive path is a sequence of nodes where each node's value differs from the previous by exactly 1 (either increasing or decreasing). So, if the input is like 3 2 4 5 9 ...

Read More

Program to make almost BST to exact BST in python

Arnab Chakraborty
Arnab Chakraborty
Updated on 25-Mar-2026 250 Views

A binary search tree (BST) has a specific property: for each node, all values in the left subtree are smaller, and all values in the right subtree are larger. When two nodes are swapped in a BST, we can identify and fix them by performing an inorder traversal and finding the nodes that violate the BST property. Understanding the Problem In an inorder traversal of a valid BST, the values should be in ascending order. When exactly two nodes are swapped, we'll find one or two violations where a node's value is less than the previous node's value. ...

Read More

Program to find area of largest square of 1s in a given matrix in python

Arnab Chakraborty
Arnab Chakraborty
Updated on 25-Mar-2026 779 Views

Suppose we have a binary matrix, we have to find the area of the largest square of 1s in that given matrix using dynamic programming. Problem Understanding Given a binary matrix containing only 0s and 1s, we need to find the area of the largest square submatrix that contains only 1s. For example, if the input matrix is ? .cell { fill: white; stroke: #333; stroke-width: 1; } .one { fill: #4CAF50; } ...

Read More

Program to find largest rectangle area under histogram in python

Arnab Chakraborty
Arnab Chakraborty
Updated on 25-Mar-2026 1K+ Views

A histogram is a graphical representation where data is displayed using bars of different heights. The largest rectangle area problem asks us to find the maximum rectangular area that can be formed under the histogram bars. Given a list of heights representing histogram bars, we need to find the area of the largest rectangle that fits completely under the bars. Problem Example For the input nums = [3, 2, 5, 7], the histogram looks like: .bar { fill: #4CAF50; stroke: #333; ...

Read More

Program to find minimum swaps required to make given anagram in python

Arnab Chakraborty
Arnab Chakraborty
Updated on 25-Mar-2026 433 Views

Suppose we have two strings S and T and they are anagrams of each other. We have to find the minimum number of swaps required in S to make it same as T. So, if the input is like S = "kolkata" T = "katloka", then the output will be 3, as we can swap in this sequence: [katloka (given), kotlaka, koltaka, kolkata]. Algorithm To solve this, we will follow these steps − Define a function util(). This will take S, T, i if i >= size of S, then return 0 ...

Read More

Program to find nearest number of n where all digits are odd in python

Arnab Chakraborty
Arnab Chakraborty
Updated on 25-Mar-2026 702 Views

Given a number n, we need to find the nearest number where all digits are odd. When two values are equidistant from n, we return the larger one. For example, if the input is n = 243, the output will be 199 because it's the closest number to 243 where all digits (1, 9, 9) are odd. Algorithm To solve this problem, we follow these steps: Find the first even digit from left to right If no even digit exists, return the original number ...

Read More

Program to find maximum difference of adjacent values after deleting k numbers in python

Arnab Chakraborty
Arnab Chakraborty
Updated on 25-Mar-2026 301 Views

Given a sorted list of numbers, we need to delete k values to minimize the maximum difference between adjacent elements. This problem uses dynamic programming to find the optimal solution. Problem Understanding If we have nums = [15, 20, 30, 400, 1500] and k = 2, removing [400, 1500] leaves [15, 20, 30] with maximum adjacent difference of 10 (between 20 and 30). Algorithm Steps The approach follows these steps − Calculate differences between consecutive elements Use dynamic programming to try removing elements from both ends Find the minimum possible maximum difference ...

Read More
Showing 4861–4870 of 21,090 articles
« Prev 1 485 486 487 488 489 2109 Next »
Advertisements