Server Side Programming Articles

Page 295 of 2109

Program to find out the shortest path to reach the goal in Python

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

Finding the shortest path in a grid is a classic problem that can be solved using Breadth-First Search (BFS). In this problem, we need to navigate through a grid where different symbols represent different cell types and find the minimum moves to reach the goal. Grid Symbol Meanings '#' is the goal cell that we want to reach 'O' is a free cell via which we can travel to the goal cell '*' is our current position in the grid 'X' is a blocked cell, via which we cannot travel Problem Example Given the ...

Read More

Program to find largest submatrix with rearrangements in Python

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

Given an m x n binary matrix, we can rearrange columns to find the largest submatrix containing only 1s. This problem combines dynamic programming with greedy optimization to maximize the area. Problem Understanding Consider this input matrix ? 0 0 1 1 1 1 1 0 1 After rearranging columns, we can get ? 1 1 0 1 1 1 0 1 1 The highlighted 2x2 submatrix has area 4, which is the maximum possible. Algorithm Steps ...

Read More

Program to find tuple with same product in Python

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

Given an array nums with unique positive values, we need to find the number of tuples (a, b, c, d) such that a*b = c*d where a, b, c, and d are elements of nums, and all elements are distinct. For example, if the input is nums = [2, 3, 4, 6], then the output will be 8 because we can get tuples like (2, 6, 3, 4), (2, 6, 4, 3), (6, 2, 3, 4), (6, 2, 4, 3), (3, 4, 2, 6), (4, 3, 2, 6), (3, 4, 6, 2), (4, 3, 6, 2). Algorithm ...

Read More

Python program to find list of triplets for which i+j+k is not same as n

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

Sometimes we need to find all possible combinations of three numbers that don't sum to a specific value. This problem involves generating all triplets (i, j, k) where each value is within given ranges and their sum is not equal to a target number n. We can solve this efficiently using list comprehension with a filtering condition to exclude triplets that sum to n. Problem Statement Given three upper bounds i, j, and k, and a target sum n, find all triplets [x, y, z] where: x ranges from 0 to i (inclusive) y ranges ...

Read More

Program to minimize hamming distance after swap operations in Python

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

The Hamming distance between two arrays is the number of positions where elements differ. This problem asks us to minimize the Hamming distance between two arrays src and tgt by performing allowed swap operations on the source array. The key insight is that allowed swaps create connected components of indices. Within each component, we can rearrange elements optimally to match the target array as much as possible. Algorithm Overview We use a Union-Find (Disjoint Set) data structure to group indices that can be swapped with each other. For each group, we count mismatches and calculate the minimum ...

Read More

Python program to check a number n is weird or not

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

A number is considered weird based on specific conditions involving whether it's odd or even and its range. Understanding these conditions helps us write a program to classify any given number. Weird Number Conditions A number n is weird when ? The number is odd The number is not in range 2 to 5 The number is even and in range 6 to 20 So, if the input is n = 18, then the output will be "Weird" because it is even and in range 6 to 20. Algorithm To solve this, ...

Read More

Program to find kpr sum for all queries for a given list of numbers in Python

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

We need to find the kpr sum for each query in a list. Given a list of numbers and queries containing [k, p, r], we calculate the sum using XOR operations on pairs of elements within a specified range. The formula for kpr_sum is: $$\mathrm{{𝑘𝑝𝑟}\_{𝑠𝑢𝑚} =\sum_{\substack{𝑖=𝑃}}^{𝑅−1}\sum_{\substack{𝑗=𝑖+1}}^{𝑅}(𝐾 ⊕(𝐴[𝑖]⊕𝐴[𝑗]))}$$ If the sum exceeds the limit, we return the result modulo 10^9+7. Understanding the Problem For each query [k, p, r], we: Take all pairs (i, j) where p ≤ i < j ≤ r Calculate k XOR (nums[i] XOR nums[j]) for each pair Sum all ...

Read More

Program to swap nodes in a linked list in Python

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

Swapping the kth node from the start with the kth node from the end in a linked list is a common problem. We need to locate both nodes and swap their values while maintaining the list structure. Problem Understanding Given a linked list and a value k, we swap the kth node from the beginning with the kth node from the end. For example, if we have nodes [1, 5, 6, 7, 1, 6, 3, 9, 12] and k=3, the 3rd node from start (6) swaps with the 3rd node from end (3). Algorithm Steps The ...

Read More

Program to find out if k monitoring stations are enough to monitor particular points in Python

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

Suppose there is a sensor module that can monitor its nearby environment up to a radius of r. There are some things in the lattice point of the module's monitoring circle that need to be monitored. So, k number of low-powered modules are placed so that they can monitor only those specific points. Given the square of the radius and k number of low-powered modules, we shall have to find out if the points can be monitored correctly. We return true if monitoring is possible, otherwise, we return false. Problem Understanding If the input is like square of ...

Read More

Program to find maximum weighted sum for rotated array in Python

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

When we have an array and need to find the maximum weighted sum after rotating it, we calculate the sum where each element is multiplied by its position (1-indexed). The weighted sum formula is: $$\mathrm{S=\sum_{i=0}^{n-1}(i+1) \times nums[i]}$$ For example, with array [5, 3, 4], we need to check all possible rotations and find the maximum weighted sum. Understanding the Problem Let's see how weighted sum works for array L = [5, 3, 4]: Original array [5, 3, 4]: sum = 1×5 + 2×3 + 3×4 = 5 + 6 + 12 = 23 Rotated ...

Read More
Showing 2941–2950 of 21,090 articles
« Prev 1 293 294 295 296 297 2109 Next »
Advertisements