Found 26504 Articles for Server Side Programming

Program to swap nodes in a linked list in Python

Arnab Chakraborty
Updated on 06-Oct-2021 13:14:20

1K+ Views

Suppose we have a list L and another value k. We have to swap kth node from start and kth node from end and return the final list at end.So, if the input is like L = [1, 5, 6, 7, 1, 6, 3, 9, 12] k = 3, then the output will be [1, 5, 3, 7, 1, 6, 6, 9, 12], the 3rd node from start is 6 and from end is 3, so they are swapped.To solve this, we will follow these steps −temp := Lfor i in range 0 to k-2, dotemp := next of tempfirstNode ... Read More

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

Arnab Chakraborty
Updated on 06-Oct-2021 13:08:28

124 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 needs 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.So, if the input is like square of radius (j) = 4, ... Read More

Program to find maximum weighted sum for rotated array in Python

Arnab Chakraborty
Updated on 06-Oct-2021 13:05:51

216 Views

Suppose we have an array of few elements. We shall have to find the maximum weighted sum if the array elements are rotated. The weighted sum of an array nums can be calculated like below −$$\mathrm{𝑆=\sum_{\substack{𝑖=1}}^{n}𝑖∗𝑛𝑢𝑚𝑠[𝑖]}$$So, if the input is like L = [5, 3, 4], then the output will be 26 becausearray is [5, 3, 4], sum is 5 + 2*3 + 3*4 = 5 + 6 + 12 = 23array is [3, 4, 5], sum is 3 + 2*4 + 3*5 = 3 + 8 + 15 = 26 (maximum)array is [4, 5, 3], sum is 4 + ... Read More

Program to count average of all special values for all permutations of a list of items in Python

Arnab Chakraborty
Updated on 06-Oct-2021 13:03:17

134 Views

Suppose we have a list of elements we can calculate the value of S by the following algorithm.while size of L > 1 is non-zero, do    a := L[0]    b := L[1]    remove L[1]    L[0] := a + b + a*b return L[0] mod (10^9 + 7)Here we shall have to find the average of all S values that are calculated from all possible combinations of L.So, if the input is like L = [5, 3, 4], then the output will be 199, because for all permutation of L, the value of S is 119, so ... Read More

Program to construct the lexicographically largest valid sequence in Python

Arnab Chakraborty
Updated on 06-Oct-2021 13:07:41

306 Views

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 on the sequence, a[i] and a[j], is |j - i|. We have to find the lexicographically largest sequence.So, if the input is like n = 4, then the output will be [4, 2, 3, 2, 4, 3, 1]To solve this, we will ... Read More

Program to find out the length between two cities in shortcuts in Python

Arnab Chakraborty
Updated on 06-Oct-2021 12:59:26

486 Views

Suppose there are n number of cities and the cities are connected with two types of roads; highways and shortcuts. Now, there is a map and only the highways are present on the map and all the shortcuts are absent. The transport division of the cities wants to launch a transport that connects the cities utilizing the highways and the shortcuts. We know there is a shortcut between the two cities when there is no highway between them. Our task here is to find the minimum distances in terms of shortcuts from a starting city to all other cities.So, if ... Read More

Program to find out the sum of minimum cost within a graph among all vertices in Python

Arnab Chakraborty
Updated on 06-Oct-2021 12:55:40

464 Views

Suppose there is a weighted graph with n vertices and m edges. The edges have the weights in powers of 2. Any vertex can be reached from any vertex in the graph, and the cost of travel will be the addition of all the edge weights in the graph. We shall have to determine the sum of minimum cost between each pair of vertices.So, if the input is likeand number of vertices (n) = 6; then the output will be 2696.The sum of all the distances is 2696.To solve this, we will follow these steps −Define a function par_finder() . ... Read More

Program to count maximum score from removing substrings in Python

Arnab Chakraborty
Updated on 06-Oct-2021 12:58:19

213 Views

Suppose we have a string s and two values x and y. We can perform given two types of operations any number of times.Search substring "ab", if present, then we can gain x points by removing it.Search substring "ba", if present, then we can gain y points by removing it.We have to find maximum points we can gain after applying the above operations on s.So, if the input is like s = "cbbaacdeabb" x = 4 y = 5, then the output will be 14 because initial string is "cbbaacdeabb", then remove "cbbaacde(ab)b" to get 4, now string is "cbbaacdeb", ... Read More

Program to find out the minimum path to deliver all letters in Python

Arnab Chakraborty
Updated on 06-Oct-2021 12:51:28

171 Views

Suppose there are n cities and that are connected with n -1 roads. A city can be visited from any other city. Now the postal system of the cities delivers k letters daily. The letter's destination can be any of the k different cities. A postal worker has to deliver all the letters to their addresses each day. We shall have to find out the minimum distance the worker has to travel to deliver all the letters. The worker can start from any given city.So, if the input is likeand the letters have to be delivered in cities (delv) 1, ... Read More

Program to find out special types of subgraphs in a given graph in Python

Arnab Chakraborty
Updated on 06-Oct-2021 12:48:43

491 Views

Suppose we have a special type of graph that has two types of vertices that are named head and feet. The graph has only one head and there are k edges that connect the head to each of the feet. So, if we are given an undirected, unweighted graph; we shall have to find out these special types of graphs in the vertex disjoint subgraphs of the graph. Any two graphs are said to be vertex disjoint if they have no vertex in common.So, if the input is likenumber of nodes (n) = 5, number of feet (t) = 2, ... Read More

Advertisements