Programming Articles

Page 228 of 2547

Program to find number of ways we can arrange letters such that each prefix and suffix have more Bs than As in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 26-Mar-2026 234 Views

Suppose we have a string with n number of A's and 2n number of B's. We need to find the number of arrangements possible such that in each prefix and each suffix, the number of B's is greater than or equal to the number of A's. This is a classic problem in combinatorics known as the ballot problem or Catalan number variant. The constraint ensures that at any point while reading from left to right (or right to left), we never have more A's than B's. Problem Understanding For n = 2, we have 2 A's and ...

Read More

Program to find lexicographically smallest string to move from start to destination in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 26-Mar-2026 290 Views

Suppose we are at position (0, 0) in the Cartesian plane and want to reach point (x, y) using only horizontal (H) and vertical (V) moves of single unit. There are multiple possible paths to reach the destination, each comprising H and V moves. We need to find the lexicographically kth smallest path. For example, to go from (0, 0) to (2, 2), one possible path is "HVVH". Since 'H' comes before 'V' lexicographically, paths starting with more H's appear earlier in lexicographical order. Algorithm The solution uses a greedy approach with combinatorial counting ? ...

Read More

Program to find n length string made of letters from m sized alphabet with no palindrome in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 26-Mar-2026 366 Views

Given m letters and a value n, we need to count strings of length n that contain no palindromic substrings of length greater than 1. A palindromic substring reads the same forwards and backwards, like "aa" or "aba". The key insight is that to avoid palindromes, no two adjacent characters can be the same, and no character at position i can equal the character at position i+2. Algorithm We follow these steps: If n = 1: Any single character is valid, so return m If n = 2: First character has m choices, second has ...

Read More

Program to find number of consecutive subsequences whose sum is divisible by k in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 26-Mar-2026 950 Views

Suppose we have an array nums and a value k. We need to find the number of consecutive subsequences whose sum is divisible by k. So, if the input is like k = 3 and nums = [1, 2, 3, 4, 1], then the output will be 4 because the subsequences are [3], [1, 2], [1, 2, 3] and [2, 3, 4]. Algorithm To solve this, we will follow these steps ? Create an array x of size k and fill with 0 Set x[0] = 1 (to handle cases where prefix sum itself is ...

Read More

Program to find number of pairs (i, j) such that ith and jth elements are same in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 26-Mar-2026 366 Views

Given an array nums, we need to find the number of pairs (i, j) where nums[i] = nums[j] but i ≠ j. This means we're looking for indices that point to the same value, excluding pairs where an element is paired with itself. For example, if the input is nums = [1, 3, 1, 3, 5], the output will be 4, because the pairs are (0, 2), (2, 0), (1, 3), and (3, 1). Algorithm To solve this, we follow these steps: Create a frequency map to count occurrences of each element For each element ...

Read More

Program to find sum of differences between max and min elements from randomly selected k balls from n balls in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 26-Mar-2026 219 Views

Suppose we have n balls which are numbered by an array nums, whose size is n and nums[i] represents the number of ball i. Now we have another value k. In each turn we pick k balls from n different balls and find the difference of maximum and minimum values of k balls and store the difference in a table. Then put these k balls again into that pot and pick again until we have selected all possible selections. Finally find the sum of all differences from the table. If the answer is too large, then return result mod 10^9+7. ...

Read More

Program to find number of ways we can merge two lists such that ordering does not change in Python

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

Suppose we have two lists nums1 and nums2. When we merge them, the constraint is that the order of elements in each list does not change. For example, if the elements are [1, 2, 3] and [4, 5, 6], then some valid merged lists are [1, 4, 2, 3, 5, 6] and [1, 2, 3, 4, 5, 6]. We need to find the number of ways we can merge two lists of sizes N and M to get a valid merged list. If the answer is too large, return the result modulo 10^9 + 7. So, if the input ...

Read More

Program to find list of all possible combinations of letters of a given string s in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 26-Mar-2026 7K+ Views

Sometimes we need to find all possible combinations of letters from a given string. This problem involves generating all non-empty subsequences of characters while maintaining lexicographical order when duplicates exist. So, if the input is like s = "pqr", then the output will be ['r', 'qr', 'q', 'pr', 'pqr', 'pq', 'p'] Algorithm Approach To solve this, we will follow these steps ? Create an empty result list Iterate through the string from right to left For each character, combine it with all existing combinations Add the character itself as a new combination Implementation ...

Read More

Program to find nCr values for r in range 0 to n, in an efficient way in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 26-Mar-2026 661 Views

Computing binomial coefficients nCr for all values of r from 0 to n can be done efficiently using Pascal's triangle properties. Instead of calculating each nCr independently, we can use the recurrence relation: nCr = nC(r-1) × (n-r+1) / r. This approach builds the coefficients incrementally, making it much faster than computing each combination separately. Problem Statement Given n, find all nCr values where r ranges from 0 to n. If any result exceeds 10^9, return it modulo 10^9. For example, if n = 6, the output should be [1, 6, 15, 20, 15, 6, 1] ...

Read More

Program to find total area covered by two rectangles in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 26-Mar-2026 421 Views

When two rectangles overlap in a 2D plane, we need to calculate their total covered area by finding the sum of both areas minus any overlapping region. Each rectangle is defined by its bottom-left corner (x1, y1) and top-right corner (x2, y2). Rectangle 1 (A, B) (C, D) Rectangle 2 (E, F) (G, H) Overlap Algorithm To ...

Read More
Showing 2271–2280 of 25,469 articles
« Prev 1 226 227 228 229 230 2547 Next »
Advertisements