Programming Articles

Page 541 of 2547

Find the sums for which an array can be divided into subarrays of equal sum in Python

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

Given an array of integers, we need to find all possible sum values for which the array can be divided into contiguous subarrays where each subarray has the same sum. If no such division is possible, we return -1. For example, if the input array is [2, 4, 2, 2, 2, 4, 2, 6], the output will be [6, 8, 12] because the array can be divided into subarrays with these equal sums: Sum 6: [{2, 4}, {2, 2, 2}, {4, 2}, {6}] Sum 8: [{2, 4, 2}, {2, 2, 4}, {2, 6}] Sum 12: [{2, 4, ...

Read More

Minimize (max(A[i], B[j], C[k]) – min(A[i], B[j], C[k])) of three different sorted arrays in Python

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

Given three sorted arrays A, B, and C of different sizes, we need to find the minimum absolute difference between the maximum and minimum values of any triplet (A[i], B[j], C[k]) where each element comes from a different array. The key insight is to use a greedy approach: start from the largest elements of each array and progressively reduce the maximum element to minimize the difference. Algorithm Steps The algorithm works as follows ? Start with pointers at the end of each array (largest elements) Calculate the current difference between max and min of the ...

Read More

Find the sum of maximum difference possible from all subset of a given array in Python

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

Given an array of n values, we need to find the sum of maximum differences possible from all subsets. For each subset, we calculate max(subset) - min(subset), then sum these differences across all possible subsets. For example, if we have array [1, 3, 4], the subsets and their max-min differences are: {1}: max=1, min=1, difference=0 {3}: max=3, min=3, difference=0 {4}: max=4, min=4, difference=0 {1, 3}: max=3, min=1, difference=2 {1, 4}: max=4, min=1, difference=3 {3, 4}: max=4, min=3, difference=1 {1, 3, 4}: max=4, min=1, difference=3 Total sum = 0+0+0+2+3+1+3 = 9 Algorithm Approach Instead ...

Read More

Find the sum of all Truncatable primes below N in Python

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

A truncatable prime is a prime number that remains prime when digits are successively removed from either the left (left-truncatable) or right (right-truncatable) side. A number must be both left-truncatable and right-truncatable to be considered truncatable. For example, 3797 is truncatable because 3797, 797, 97, 7 (left truncation) and 3797, 379, 37, 3 (right truncation) are all prime numbers. Algorithm We use the Sieve of Eratosthenes to precompute all primes, then check each number for truncatability ? Generate all primes up to a reasonable limit using the sieve For each prime number, check if it's left-truncatable ...

Read More

Find the smallest window in a string containing all characters of another string in Python

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

Finding the smallest window in a string that contains all characters of another string is a classic sliding window problem. This technique uses two pointers to efficiently find the minimum substring. Problem Statement Given two strings s1 and s2, we need to find the smallest substring in s1 that contains all characters of s2. For example, if s1 = "I am a student" and s2 = "mdn", the output should be "m a studen". Algorithm Steps The sliding window approach works as follows: Create frequency arrays for both pattern and current window characters ...

Read More

Find the smallest positive integer value that cannot be represented as sum of any subset of a given array in Python

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

In some programming problems, we need to find the smallest positive integer that cannot be represented as the sum of any subset from a given sorted array. This is a classic greedy algorithm problem that can be solved efficiently in O(n) time complexity. So, if the input is like A = [1, 4, 8, 12, 13, 17], then the output will be 2, because we can represent 1 using [1], but we cannot represent 2 using any subset combination. Algorithm Approach The key insight is that if we can represent all numbers from 1 to answer-1, and the current element A[i]

Read More

Find the probability of a state at a given time in a Markov chain - Set 1 in Python

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

A Markov chain is a random process where the probability of moving to the next state depends only on the current state. We can represent it as a directed graph where nodes are states and edges have transition probabilities. To find the probability of reaching state F at time T starting from state S, we use dynamic programming. Problem Statement Given a Markov chain with N states, we need to find the probability of reaching state F at time T if we start from state S at time 0. Each transition takes one unit of time, and the ...

Read More

Find the position of box which occupies the given ball in Python

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

Given two arrays A and B, where A represents the number of boxes in each row and B contains ball numbers, we need to find the row and column position for each ball. Ball i with value B[i] will be placed in the B[i]-th box when counting from the beginning. Problem Understanding Array A defines the grid structure where A[i] is the number of boxes in row i. Array B contains ball numbers, and each ball B[i] goes to the B[i]-th position when boxes are numbered sequentially from 1. Example For A = [3, 4, 5, ...

Read More

Find the player who rearranges the characters to get a palindrome string first in Python

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

Suppose we have a string S with lowercase letters, and two players are playing a game. The rules are as follows: A player wins if they can shuffle the characters of the string to form a palindrome string A player cannot win by removing any character from the string Both players play optimally and Player 1 starts the game. We need to determine the winner. So, if the input is like "pqpppq", then the output will be Player 1 as Player 1 in the first step arranges the characters to get "ppqqpp" and wins the ...

Read More

Find the original matrix when largest element in a row and a column are given in Python

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

This problem involves reconstructing an original matrix when you know the largest element in each row and column, plus a binary matrix indicating where positive values were located. The key insight is that at any position where the binary matrix has a 1, the original value must be the minimum of the row maximum and column maximum. Problem Understanding Given: Array A of size N (maximum elements for each row) Array B of size M (maximum elements for each column) Binary matrix of size N×M where 1 indicates a positive value existed, 0 indicates the position was ...

Read More
Showing 5401–5410 of 25,466 articles
« Prev 1 539 540 541 542 543 2547 Next »
Advertisements