Server Side Programming Articles

Page 512 of 2109

Program to find a list of product of all elements except the current index in Python

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

Given a list of numbers, we need to find a new list where each element at index i is the product of all numbers in the original list except the one at index i. This problem must be solved without using division. So, if the input is nums = [2, 3, 4, 5, 6], then the output will be [360, 240, 180, 144, 120]. Approach We use two auxiliary arrays to store left and right products: Left array: Contains products of all elements to the left of each index ...

Read More

Program to check whether all palindromic substrings are of odd length or not in Python

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

Sometimes we need to check if all palindromic substrings in a string have odd lengths. A key insight is that any even-length palindrome must contain adjacent identical characters. Therefore, we can solve this by simply checking for consecutive duplicate characters. Algorithm The approach is straightforward ? Iterate through the string from index 1 to the end If any character equals the previous character, return False If no adjacent duplicates are found, return True Why This Works Any even-length palindrome like "aa", "abba", or "deed" must have at least one pair of adjacent identical ...

Read More

Program to find minimum number of hops required to reach end position in Python

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

Suppose we have one array nums, where all elements are positive. We are at index 0. Here, each element in the array represents our maximum jump length at that position. Our goal is to reach to the final index (n-1, where n is size of nums) with minimum number of jumps. So if the array is like [2, 3, 1, 1, 4], then the output will be 2, as we can jump to index 1 from 0, then jump to index 4, that is the last index. Approach We use a greedy approach to solve this problem efficiently ...

Read More

Program to check whether every one has at least a friend or not in Python

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

Suppose we have n people represented as numbers from 0 to n-1, and a list of friendship pairs, where friends[i][0] and friends[i][1] are friends. We need to check whether everyone has at least one friend or not. So, if the input is like n = 3 and friends = [[0, 1], [1, 2]], then the output will be True. Person 0 is friends with Person 1, Person 1 is friends with Person 0 and 2, and Person 2 is friends with Person 1. Algorithm To solve this problem, we follow these steps − ...

Read More

Program to find most frequent subtree sum of a binary tree in Python

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

Suppose we have a binary tree, we have to find the most frequent subtree sum. The subtree sum of a node is the sum of all values under a node, including the node itself. So, if the input is like -6 3 6 Subtree sums: 3, 6, 3 (-6+3+6) then the output will be 3 as it occurs twice — ...

Read More

Program to count non-empty subsets where sum of min and max element of set is less than k in Python

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

Given a list of numbers and a value k, we need to find the number of non-empty subsets where the sum of minimum and maximum elements is less than or equal to k. Note that subsets are treated as multisets, meaning duplicate values can appear since they refer to specific positions in the list. For example, if nums = [2, 2, 5, 6] and k = 7, the valid subsets are: [2], [2], [2, 2], [2, 5], [2, 5], [2, 2, 5], giving us 6 subsets total. Algorithm The approach uses a two-pointer technique after sorting the ...

Read More

Program to find maximum time to finish K tasks in Python

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

Suppose we have a matrix of tasks where each row has 3 values representing costs for different resources. We also have another value k. We need to select k rows from tasks to minimize the total cost, which is calculated as the sum of maximum values from each of the 3 columns in the selected rows. The cost formula is: max(column_0) + max(column_1) + max(column_2) from the k selected rows. Problem Example If the input is like tasks = [[2, 3, 3], [4, 5, 2], [4, 2, 3]] and k = 2, then the output will be ...

Read More

Program to find minimum difference of max and mins after updating elements at most three times in Python

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

Suppose we have a list of numbers called nums, and we can perform an operation to update any element to any value. We can perform at most 3 such operations, and we need to find the resulting minimum difference between the max and min values in nums. So, if the input is like nums = [2, 3, 4, 5, 6, 7], then the output will be 2. We can change the list to [4, 3, 4, 5, 4, 4] and then 5 - 3 = 2. Algorithm To solve this problem, we follow these steps − ...

Read More

Program to find minimum difference between two elements from two lists in Python

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

Sometimes we need to find the smallest possible difference between any element from one list and any element from another list. Python provides an efficient solution using the two-pointer technique after sorting both lists. So, if the input is like L1 = [2, 7, 4], L2 = [16, 10, 11], then the output will be 3, as the smallest difference is 10 - 7 = 3. Algorithm Steps To solve this efficiently, we will follow these steps ? Sort both lists L1 and L2 Initialize minimum difference as infinity ...

Read More

Program to find minimum number of deletions required from two ends to make list balanced in Python

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

Suppose we have a list containing 0s and 1s, we have to remove values from the front or from the back of the list. Finally, we have to find the minimum number of deletions required such that the remaining list has an equal number of 0s and 1s. So, if the input is like nums = [1, 1, 1, 0, 0, 1], then the output will be 2, as we can delete the first one 1 and last one 1 so that there's two 1s and two 0s. Algorithm To solve this, we will follow these steps ...

Read More
Showing 5111–5120 of 21,090 articles
« Prev 1 510 511 512 513 514 2109 Next »
Advertisements