Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Server Side Programming Articles
Page 295 of 2109
Program to find largest submatrix with rearrangements in Python
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 MoreProgram to find tuple with same product in Python
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 MorePython program to find list of triplets for which i+j+k is not same as n
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 MoreProgram to minimize hamming distance after swap operations in Python
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 MorePython program to check a number n is weird or not
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 MoreProgram to find kpr sum for all queries for a given list of numbers in Python
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 MoreProgram to swap nodes in a linked list in Python
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 MoreProgram to find out if k monitoring stations are enough to monitor particular points in Python
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 MoreProgram to find maximum weighted sum for rotated array in Python
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 MoreProgram to count average of all special values for all permutations of a list of items in Python
Finding the average of special values across all permutations of a list involves calculating a specific transformation for each arrangement and then averaging the results. This problem demonstrates an elegant mathematical property where the average can be computed directly without generating all permutations. Understanding the Algorithm The special value S is calculated using this process ? while size of L > 1 is non-zero, do a := L[0] b := L[1] remove L[1] L[0] := a + b + a*b ...
Read More