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
Articles by Arnab Chakraborty
Page 39 of 377
Program to find number of ways to form a target string given a dictionary in Python
Suppose we have a list of strings called words, where all elements are of the same length. We also have a string called target. We have to generate target using the given words under the following rules: We should generate target from left to right. To get the ith character (0-indexed) of target, we can select the kth character of the jth string in words when target[i] is same as words[j][k]. Once we use the kth character of the jth string of words, we cannot use the xth character of any string in words where x
Read MoreProgram to find minimum one bit operations to make integers zero in Python
We need to transform a number n into 0 using specific bit operations. The operations allowed are: Select the rightmost bit in the binary representation of n. Change the ith bit in the binary representation of n when the (i-1)th bit is set to 1 and the (i-2)th through 0th bits are set to 0. We need to find the minimum number of operations required to transform n into 0. Understanding the Problem For example, if n = 6, the binary representation is "110". The transformation steps are ? Start: "110" (6 ...
Read MoreProgram to find out how many transfer requests can be satisfied in Python
Suppose there are n hostel rooms numbered from 0 to n-1. Students want to transfer to different rooms and place transfer requests. Since no room can remain vacant, a transfer is only satisfied if another student takes the transferring student's place. We need to find the maximum number of requests that can be satisfied simultaneously. Problem Understanding For a set of transfer requests to be valid, the number of students leaving each room must equal the number of students entering that room. This creates a balanced system where no room becomes empty or overcrowded. Example If ...
Read MoreProgram to find out the shortest path to reach the goal in Python
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 MoreProgram 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 More