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 40 of 377
Program 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 MoreProgram to construct the lexicographically largest valid sequence in Python
Suppose we have a number n, we have to find a sequence that satisfies all of the following rules ? 1 occurs once in the sequence. Each number in between 2 and n occurs twice in the sequence. For every i in range 2 to n, the distance between the two occurrences of i is exactly i. The distance between two numbers in the sequence, a[i] and a[j], is |j - i|. We have to find the lexicographically largest sequence. So, if the ...
Read MoreProgram to find out the length between two cities in shortcuts in Python
Finding the shortest path using shortcuts between cities is a graph theory problem. We have highways connecting some cities, and shortcuts exist between cities that are not directly connected by highways. Our goal is to find the minimum distances using only shortcuts from a starting city to all other cities. Problem Understanding Given a graph where edges represent highways, shortcuts exist between vertices that are not connected by highways. We need to find shortest paths using only these shortcuts ? 1 2 ...
Read MoreProgram to find out the minimum path to deliver all letters in Python
Suppose there are n cities connected with n-1 roads, forming a tree structure where any city can be reached from any other city. A postal worker needs to deliver k letters daily to different destination cities. We need to find the minimum distance the worker must travel to deliver all letters, starting from any city. Problem Understanding Given a tree of cities and a list of delivery destinations, we need to find the shortest path that visits all destination cities. The key insight is that in a tree, we need to traverse edges optimally — some edges might ...
Read MoreProgram to find out special types of subgraphs in a given graph in Python
A special graph is defined as having exactly one "head" vertex connected to exactly t "feet" vertices. Given an undirected, unweighted graph, we need to find how many such special subgraphs can be formed where each subgraph uses disjoint vertices (no vertex appears in multiple subgraphs). The key insight is that feet vertices have degree 1 (connected only to their head), while head vertices have degree equal to the number of feet they connect to. Special Graph Structure Head ...
Read MoreProgram to find out the path between two vertices in a graph that has the minimum penalty (Python)
When working with weighted graphs, sometimes we need to find a path that minimizes a specific cost function. In this problem, we find the path between two vertices where the penalty is minimized. The penalty of a path is the bitwise OR of all edge weights in the path. Given an undirected, weighted graph, we need to find the minimum penalty path from node a to node b. If no path exists, we return -1. Problem Example 1 2 ...
Read MoreProgram to find out the minimum size of the largest clique in a graph (Python)
A clique in a graph is a subset of vertices where every pair of vertices is connected by an edge. Finding the largest clique is computationally challenging, but we can determine the minimum size of the largest clique using a binary search approach with edge counting. The problem asks: given the number of nodes and edges in a graph, what is the minimum possible size of the largest clique? Understanding the Problem If we have a graph with n nodes and e edges, we need to find the smallest value k such that any graph with these ...
Read More