Programming Articles

Page 513 of 2547

Program to find maximum sum of the subsequence, where difference of two values is same as their position difference in Python

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

Given a list of numbers, we need to find a subsequence where the difference between consecutive values equals the difference between their original indices. Our goal is to find the maximum sum of such a subsequence. For example, if we have nums = [6, 7, 9, 9, 8, 5], we can select subsequence [6, 7, 9] at indices [0, 1, 3]. The value differences are [1, 2] and index differences are also [1, 2], giving us a sum of 22. Algorithm The key insight is that for any valid subsequence, if we have values a and b ...

Read More

Program to check sublist sum is strictly greater than the total sum of given list Python

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

Suppose we have a list of numbers called nums, we have to check whether there is a sublist such that its sum is strictly greater than the total sum of the list. So, if the input is like nums = [1, −2, 3, 4], then the output will be True, as the sum of the list is 6 and the sum of the sublist [3, 4] is 7 which is strictly larger. Algorithm Approach To solve this, we will follow these steps − Calculate total sum of all elements in the list Use prefix sum ...

Read More

Program to find product of few numbers whose sum is given in Python

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

Suppose we have a number n, we have to find two or more numbers such that their sum is equal to n, and the product of these numbers is maximized. We need to find the maximum product. So, if the input is like n = 12, then the output will be 81, as 3 + 3 + 3 + 3 = 12 and 3 * 3 * 3 * 3 = 81. Algorithm To solve this, we will follow these steps ? Define a function dp() that takes parameter n If n is equal to ...

Read More

Program to find smallest intersecting element of each row in a matrix in Python

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

Suppose we have a 2D matrix where each row is sorted in ascending order. We have to find the smallest number that exists in every row. If there's no such result, then return −1. So, if the input is like ? 2 3 5 5 10 10 1 3 5 then the output will be 5 because it's the smallest element present in all three rows. Algorithm To solve this, we will follow these steps ? If matrix is empty, then return −1 ...

Read More

Program to split a set into equal sum sets where elements in first set are smaller than second in Python

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

We need to split a list of numbers into two groups A and B such that they have equal sums, and every number in A is strictly smaller than every number in B. For example, with nums = [3, 4, 5, 12], we can split into A = [3, 4, 5] and B = [12], both having sum 12. Algorithm The key insight is that after sorting, we need to find a split point where: All elements before the split form group A All elements from the split onwards form group B Both groups have ...

Read More

Program to count total number of set bits of all numbers in range 0 to n in Python

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

Suppose we have a number n. For each number i in the range 0 ≤ i ≤ n, we need to count the number of set bits (1's) in their binary representation and return the total count. For example, if n = 5, the numbers are [0, 1, 2, 3, 4, 5] with binary representations [0, 1, 10, 11, 100, 101]. The count of 1's are [0, 1, 1, 2, 1, 2], so the total is 7. Approach We use dynamic programming with bit manipulation. The key insight is that for any number i, if i & (i-1) ...

Read More

Program to count minimum number of animals which have no predator in Python

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

Suppose we have a list of numbers called nums where nums[i] shows the predator of the ith animal and if there is no predator, it will hold −1. We have to find the smallest number of groups of animals such that no animal is in the same group with its direct or indirect predator. So, if the input is like nums = [1, 2, −1, 4, 5, −1], then the output will be 3, as we can have the groups like: [0, 3], [1, 4], [2, 5]. Approach To solve this, we will follow these steps − ...

Read More

Program to separate persons where no enemies can stay in same group in Python

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

Suppose we have a number n and a 2D matrix called enemies. Here n indicates there are n people labeled from [0, n - 1]. Now each row in enemies contains [a, b] which means that a and b are enemies. We have to check whether it is possible to partition the n people into two groups such that no two people that are enemies are in the same group. So, if the input is like n = 4, enemies = [[0, 3], [3, 2]], then the output will be True, as we can have these two groups [0, ...

Read More

Program to find in which interval how many tasks are worked on in Python

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

Suppose we have a list of intervals where each interval is like [start, end) and we also have a list of strings called types. Now for a given i, the intervals[i] shows the times someone worked on the job types[i] from [start, end). Two intervals of the same type never overlap or touch. So we have to find a sorted merged list where each item has [start, end, num_types], indicates from start to end, num_types number of tasks were being worked on. So, if the input is like intervals = [[0, 3], [5, 7], [0, 7]] types = ["problem ...

Read More

Program to rotate square matrix by 90 degrees counterclockwise in Python

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

Rotating a square matrix 90 degrees counterclockwise is a common matrix transformation problem. We can achieve this using a two-step approach: first reverse each row, then transpose the matrix. Understanding the Rotation Let's visualize how a 90-degree counterclockwise rotation works: Original Matrix 1 4 7 2 5 8 3 ...

Read More
Showing 5121–5130 of 25,466 articles
« Prev 1 511 512 513 514 515 2547 Next »
Advertisements