Found 10476 Articles for Python

Program to find center of star graph in Python

Arnab Chakraborty
Updated on 06-Oct-2021 11:30:49

572 Views

Suppose we have one undirected star graph with n nodes labeled from 1 to n. As we know a star graph is a graph where there is one center node and exactly n - 1 edges that connect the center node with every other node. We have to find the center of the given star graph.So, if the input is likethen the output will be 3 as 3 is at center.To solve this, we will follow these steps −seen := a new setfor each edge (u, v) in graph, doif u is in seen, thenreturn uif v is in seen, ... Read More

Program to find out number of distinct substrings in a given string in python

Arnab Chakraborty
Updated on 06-Oct-2021 11:28:03

945 Views

Suppose, we are given a substring denoted by 's'. We have to find out the unique substrings and return the number of these substrings as output.So, if the input is like s = 'prrstvt', then the output will be 26.The distinct substrings will be −'pr', 'rrs', 'st', 'rr', 'tv', 'rstv', 'stvt', 'prrstv', 'prrstvt', 'rrstvt', 's', 'prrst', 'stv', 'rrstv', 'rst', 'v', 'tvt', 'rstvt', 'r', 'rs', 'vt', 't', 'prr', 'p', 'rrst', and 'prrs'.To solve this, we will follow these steps −visited := a new mapfor each index ind, and value let in s, dotemp := a new setif ind-1 is present in ... Read More

Program to find number of restricted paths from first to last node in Python

Arnab Chakraborty
Updated on 06-Oct-2021 11:26:05

257 Views

Suppose we have one undirected weighted connected graph. The graph has n nodes and they are labelled from 1 to n. A path from start to end is a sequence of nodes like [z0, z1, z2, ..., zk] here z0 is start node and zk is end node and there is an edge between zi and zi+1 where 0

Program to find maximum score we can get in jump game in Python

Arnab Chakraborty
Updated on 06-Oct-2021 11:21:46

702 Views

Suppose we have an array called nums and another value k. We are at index 0. In one move, we can jump at most k steps right without going outside the boundaries of the array. We want to reach the final index of the array. For jumping we get score, that is the sum of all nums[j] for each index j we visited in the array. We have to find the maximum score we can get.So, if the input is like nums = [1, -2, -5, 7, -6, 4] k = 2, then the output will be 10 because, we ... Read More

Program to find minimum elements to add to form a given sum in Python

Arnab Chakraborty
Updated on 06-Oct-2021 11:17:31

357 Views

Suppose we have an array called nums and two values limit and goal. The array is special because |nums[i]|

Program to find maximum erasure value in Python

Arnab Chakraborty
Updated on 06-Oct-2021 11:14:15

184 Views

Suppose we have an array called nums (with positive values only) and we want to erase a subarray containing unique elements. We will get score that is sum of subarray elements. We have to find the maximum score we can get by erasing exactly one subarray.So, if the input is like nums = [6, 3, 2, 3, 6, 3, 2, 3, 6], then the output will be 11, because here optimal subarray is either [6, 3, 2] or [2, 3, 6], so sum is 11.To solve this, we will follow these steps −seen := a new mapans := sum := ... Read More

Program to find sum of beauty of all substrings in Python

Arnab Chakraborty
Updated on 06-Oct-2021 11:12:46

733 Views

Suppose we have a string s. We have to find the sum of beauty of all of its substrings. The beauty of a string is actually the difference in frequencies between the most frequent and least frequent characters. So if the string is "abaacc", then its frequency is 3 - 1 = 2.So, if the input is like s = "xxyzy", then the output will be 5 because the substrings with non-zero beauty are ["xxy", "xxyz", "xxyzy", "xyzy", "yzy"], each has beauty value 1.To solve this, we will follow these steps −res:= 0for i in range 0 to size of ... Read More

Program to find minimum difference of stone games score in Python

Arnab Chakraborty
Updated on 06-Oct-2021 11:10:16

218 Views

Suppose we have an array called stones where stones[i] represents the value of the ith stone from the left. Two friends Amal and Bimal re playing a turn based game with these stones and Amal starts first always. There are n stones arranged in a row. Each player can remove either the leftmost stone or the rightmost stone from the row and get points equal to the sum of the remaining stones' values in the row. Who will get the higher score will win. Now, Bimal found that he will always lose this game, so he decided to minimize the ... Read More

Program to check whether number is a sum of powers of three in Python

Arnab Chakraborty
Updated on 06-Oct-2021 11:07:25

570 Views

Suppose we have a number n, we have to check whether it is possible to represent n as the sum of distinct powers of three or not. An integer y is said to be power of three if there exists an integer x such that y = 3^x.So, if the input is like n = 117, then the output will be True because 117 = 3^4 + 3^3 + 3^2 + = 81 + 27 + 9.To solve this, we will follow these steps −for i in range 16 to 0, decrease by 1, doif n >= 3^i , thenn ... Read More

Program to partitioning into minimum number of Deci- Binary numbers in Python

Arnab Chakraborty
Updated on 06-Oct-2021 11:06:00

533 Views

Suppose we have a number n in string format. We have to find minimum deci-binary numbers are required, so that whose sum is equal to n. A deci-binary number is a decimal number whose digits are either 0 or 1.So, if the input is like n = "132", then the output will be 3 because 132 is sum of three decibinary number (10 + 11 + 111).To solve this, we will follow these steps −result := 1for each i in n, doif i is not in {0, 1}, thenresult := maximum of result and ireturn resultExampleLet us see the following ... Read More

Advertisements