Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Server Side Programming Articles
Page 513 of 2109
Program to find maximum length of non-sharing words in Python
Given a list of lowercase alphabetical strings, we need to find the maximum sum of lengths of two distinct words that do not share any common letters. This problem can be efficiently solved using bit manipulation to represent character sets. Problem Understanding For the input words = ["abcd", "mno", "abdcmno", "amno"], we need to find two words with no common letters. The words "abcd" and "mno" share no common letters, giving us a total length of 4 + 3 = 7. Algorithm Approach We use bit manipulation where each bit position represents a letter (a=0, b=1, ...
Read MoreProgram to find maximum sum of non-adjacent nodes of a tree in Python
Finding the maximum sum of non-adjacent nodes in a binary tree is a classic dynamic programming problem. We need to select nodes such that no parent-child pairs are included, maximizing the total sum. Problem Understanding Given a binary tree, we want to find the maximum sum where no two selected nodes are adjacent (parent-child relationship). For each node, we have two choices: include it or exclude it. 1 2 10 ...
Read MoreProgram to maximize the number of equivalent pairs after swapping in Python
Suppose we have two lists A and B of the same length, and a 2D list C containing pairs [i, j] that allow us to swap elements A[i] and A[j] as many times as needed. We need to find the maximum number of equivalent pairs where A[i] = B[i] after optimal swapping. The key insight is that elements connected through swap operations form groups, and within each group, we can arrange elements in any order to maximize matches. Example If the input is A = [5, 6, 7, 8], B = [6, 5, 8, 7], and C ...
Read MoreProgram to maximize the minimum value after increasing K sublists in Python
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 MoreProgram to make pairwise adjacent sums small in Python
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 MoreProgram to find number of coins needed to make the changes in Python
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 MoreProgram to convert one list identical to other with sublist sum operation in Python
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 MoreProgram to check minimum number of characters needed to make string palindrome in Python
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 MoreProgram to find sum of longest sum path from root to leaf of a binary tree in Python
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 MoreProgram to find the length of longest substring which has two distinct elements in Python
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