Programming Articles

Page 517 of 2547

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

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

This problem requires finding the maximum possible minimum value in an array after performing K operations. Each operation increments a contiguous subarray of specified size by 1. We use binary search combined with a greedy approach to solve this efficiently. Problem Understanding Given an array nums, we can perform K operations where each operation: Selects a contiguous subarray of length size Increments every element in that subarray by 1 Our goal is to maximize the minimum value in the final array. Example Walkthrough For nums = [2, 5, 2, 2, 7], size = ...

Read More

Program to make pairwise adjacent sums small in Python

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

We need to find the minimum number of operations to make every pair of adjacent values in a list sum to at most k. We can only decrease positive numbers by 1 in each operation. The key insight is to process the array from left to right. For each adjacent pair, if their sum exceeds k, we reduce the second element to make the sum exactly k (or reduce it to 0 if needed). Algorithm Steps The approach follows these steps ? Initialize a counter for total operations For each adjacent pair (nums[i], nums[i+1]): ...

Read More

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

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

The coin change problem is a classic dynamic programming challenge where we need to find the minimum number of coins required to make a given amount. Given coins of denominations [1, 5, 10, 25] and a target amount, we use dynamic programming to build up solutions for smaller amounts. Problem Understanding For example, to make amount 64, we can use: 25 + 25 + 10 + 1 + 1 + 1 + 1 = 64, requiring 7 coins total. Algorithm Steps The dynamic programming approach follows these steps: If amount ...

Read More

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

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

Given two lists, we need to make them identical by repeatedly choosing a sublist and replacing it with its sum. The goal is to find the maximum possible length of the resulting identical lists, or return -1 if no solution exists. The key insight is to work backwards from the end of both lists, comparing elements and merging adjacent elements when needed. Algorithm Approach We use a greedy approach starting from the end of both lists ? Compare elements from the end of both lists If elements ...

Read More

Program to check minimum number of characters needed to make string palindrome in Python

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

A palindrome is a string that reads the same forwards and backwards. To make any string a palindrome, we need to find the minimum number of characters to insert. This can be solved using dynamic programming by comparing characters from both ends. Problem Statement Given a string, find the minimum number of characters needed to be inserted to make it a palindrome ? For example, if the input is s = "mad", we can insert "am" to get "madam", requiring 2 insertions. Algorithm The approach uses a recursive function dp(i, j) that compares characters at ...

Read More

Program to find sum of longest sum path from root to leaf of a binary tree in Python

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

Suppose we have a binary tree, we have to find the sum of the longest path from the root to a leaf node. If there are two same long paths, return the path with larger sum. So, if the input is like: 2 10 ...

Read More

Program to find the length of longest substring which has two distinct elements in Python

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

Suppose we have a string s, we have to find the length of the longest substring that contains at most 2 distinct characters. So, if the input is like s = "xyzzy", then the output will be 4, as "yzzy" is the longest substring with at most 2 unique characters. Algorithm To solve this problem, we will use the sliding window technique with the following steps ? Initialize start pointer to 0 Create a character frequency counter Initialize answer to 0 For each character at the end pointer: Add character to the counter While ...

Read More

Program to find length of longest sublist with given condition in Python

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

Suppose we have a list of numbers called nums. We need to find the length of the longest sublist where 2 * minimum of sublist > maximum of sublist. So, if the input is like nums = [10, 2, 6, 6, 4, 4], then the output will be 4, as the sublist [6, 6, 4, 4] is the longest sublist that satisfies the condition since 2 * 4 > 6. Algorithm We use a sliding window approach with two deques to efficiently track minimum and maximum values in the current window ? ...

Read More

Program to find length of longest alternating inequality elements sublist in Python

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

Given a list of numbers, we need to find the length of the longest sublist where the inequality relation between consecutive numbers alternates between less-than and greater-than operations. The first two numbers can start with either inequality relation. So, if the input is like nums = [1, 2, 6, 4, 5], then the output will be 4, as the longest alternating inequality sublist is [2, 6, 4, 5] where 2 < 6 > 4 < 5. Algorithm To solve this problem, we follow these steps ? Define a helper function get_direction() that returns the relationship ...

Read More

Program to find length of longest increasing subsequence in Python

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

The Longest Increasing Subsequence (LIS) problem asks us to find the length of the longest subsequence where elements are in increasing order. For example, in the array [6, 1, 7, 2, 8, 3, 4, 5], the LIS is [1, 2, 3, 4, 5] with length 5. We'll solve this using an efficient binary search approach with O(n log n) time complexity. Algorithm Steps The algorithm maintains a tails array where tails[i] stores the smallest ending element of all increasing subsequences of length i+1 ? Create a tails array of same size as input array, initialize ...

Read More
Showing 5161–5170 of 25,466 articles
« Prev 1 515 516 517 518 519 2547 Next »
Advertisements