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
Programming Articles
Page 138 of 2547
Program 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 MoreProgram to find number of ways to split array into three subarrays in Python
Suppose we have an array called nums, we have to find the number of good ways to split this array. A split is considered good if the array is divided into three non-empty contiguous subarrays where sum(left) ≤ sum(middle) ≤ sum(right). Since the answer may be large, return result modulo 10^9 + 7. Problem Understanding If the input is nums = [2, 3, 3, 3, 7, 1], then the output will be 3 because there are three different ways of splitting − [2], [3], [3, 3, 7, 1] → sums: 2 ≤ 3 ≤ 11 [2], ...
Read MoreProgram to find out an MST using Prim's algorithm in Python
A Minimum Spanning Tree (MST) is a subset of edges in a weighted graph that connects all vertices with the minimum possible total edge weight and no cycles. Prim's algorithm builds the MST by starting from a vertex and greedily adding the minimum weight edge that connects a visited vertex to an unvisited one. Original Graph ...
Read More