Server Side Programming Articles - Page 1539 of 2650

Program to check if the given list has Pythagorean Triplets or not in Python

Arnab Chakraborty
Updated on 07-Oct-2020 13:24:11

1K+ Views

Suppose we have a list of numbers called nums, we have to check whether there exist three numbers a, b, and c such that a^2 + b^2 = c^2.So, if the input is like [10, 2, 8, 5, 6], then the output will be True, as 8^2 + 6^2 = 64+36 = 100 = 10^2.To solve this, we will follow these steps −tmp := list of square of all numbers in nums in descending orderfor each index i and corresponding number n in tmp, dobase := nleft := i+1, right := size of tmp -1while left base, thenleft := ... Read More

Program to find all prime factors of a given number in sorted order in Python

Arnab Chakraborty
Updated on 07-Oct-2020 13:20:24

775 Views

Suppose we have a number n greater than 1, we have to find all of its prime factors and return them in sorted sequence. We can write out a number as a product of prime numbers, they are its prime factors. And the same prime factor may occur more than once.So, if the input is like 42, then the output will be [2, 3, 7].To solve this, we will follow these steps −res:= a new listwhile n mod 2 is same as 0, doinsert 2 at the end of resn := quotient of n/2for i in range 3 to (square ... Read More

Program to find minimum total cost for equalizing list elements in Python

Arnab Chakraborty
Updated on 07-Oct-2020 13:15:20

283 Views

Suppose we have two lists of numbers called nums and costs. Now consider, there is an operation where we can increase or decrease nums[i] for cost costs[i]. We can perform any number of these operations, and we want to make all elements equal in the nums. We have to find the minimum total cost required.So, if the input is like nums = [3, 2, 4] costs = [1, 10, 2], then the output will be 5, as if we can decrease the number 3 into 2 for a cost of 1. Then we can decrement 4 two times for a ... Read More

Program to find a tree by updating values with left and right subtree sum with itself in Python

Arnab Chakraborty
Updated on 07-Oct-2020 13:13:10

288 Views

Suppose we have a binary tree, we have to find the same tree but every node's value is replaced by its its value + all of the sums of its left and right subtrees.So, if the input is likethen the output will beTo solve this, we will follow these steps −Define a function tree_sum() . This will take root of a treeif root is null, thenreturn 0data of root := tree_sum(left of root) + tree_sum(right of root) + data of rootreturn data of rootFrom the main method, do the following:tree_sum(root)return rootLet us see the following implementation to get better understanding ... Read More

Program to find the minimum edit distance between two strings in C++

Arnab Chakraborty
Updated on 07-Oct-2020 13:09:22

525 Views

Suppose we have two words S and T, we have to find the minimum number of operations needed to convert from S to T. The operations can be of three types, these areinsert a character, delete a characterreplace a character.So if the input strings are “evaluate” and “fluctuate”, then the result will be 5.To solve this, we will follow these steps −n := size of s, m := size of t, create an array dp of size n + 1for i in range 0 to ndp[i] := new array of size m + 1for j in range 0 to m:dp[i, ... Read More

Program to count number of unique palindromes we can make using string characters in Python

Arnab Chakraborty
Updated on 07-Oct-2020 12:49:28

290 Views

Suppose we have a string s, we have to find the number of distinct palindromes we can generate using all characters. If the answer is very large then mod the result by 10^9 + 7.So, if the input is like s = "xyzzy", then the output will be 2, as we can make "zyxyz" and "yzxzy"To solve this, we will follow these steps −m = 10^9+7char_freq := a map holding each character of s and their frequenciesodd := 0for each character k and frequency v in char_freq, doif v mod 2 is 1, thenodd := odd + 1if odd > ... Read More

Program to calculate vertex-to-vertex reachablity matrix in Python

Arnab Chakraborty
Updated on 07-Oct-2020 12:46:39

358 Views

Suppose we have a graph as an adjacency list representation, we have to find 2D matrix M whereM[i, j] = 1 when there is a path between vertices i and j.M[i, j] = 0 otherwise.So, if the input is likethen the output will be1111101111011110111101111To solve this, we will follow these steps −ans:= a 2d matrix of size n x n, where n is the number of vertices, fill with 0sfor i in range 0 to n, doq:= a queue, and insert i at firstwhile q is not empty, donode:= first element of q, and delete first element from qif ans[i, ... Read More

Program to count number of ways we can throw n dices in Python

Arnab Chakraborty
Updated on 07-Oct-2020 12:40:00

265 Views

Suppose we have a number n, number of faces, and a total value, we have to find the number of ways it is possible to throw n dice with faces each to get total. If the answer is very large mod the result by 10**9 + 7.So, if the input is like n = 2 faces = 6 total = 8, then the output will be 5, as there are 5 ways to make 8 with 2 6-faced dice: (2 and 6), (6 and 2), (3 and 5), (5 and 3), (4 and 4).To solve this, we will follow these ... Read More

Program to find sum each of the diagonal path elements in a binary tree in Python

Arnab Chakraborty
Updated on 07-Oct-2020 12:37:45

191 Views

Suppose we have a binary tree, we have to find the sum of each of the diagonals in the tree starting from the top to bottom right.So, if the input is likethen the output will be [27, 18, 3] as the diagonals are [12, 15], [8, 10], [3]. So the sum values are [27, 18, 3]To solve this, we will follow these steps −Define a function traverse() . This will take node, numLeft, outputif node is null, thenreturnif numLeft >= size of output , theninsert data of node at the end of outputotherwise, output[numLeft] := output[numLeft] + data of nodeif ... Read More

Program to sort each diagonal elements in ascending order of a matrix in C++

Arnab Chakraborty
Updated on 07-Oct-2020 12:34:41

533 Views

Suppose we have n x m matrix Mat, we have to sort this Mat diagonally in increasing order from top-left to the bottom right, so that all elements in the diagonals are sorted. So if the input matrix is like −331122121112The output matrix will be −111112221233To solve this, we will follow these steps −Define a method called solve(), this will take si, sj and matrix matn := number of rows and m := number of columnsmake an array called tempi:= si and j := sj, and index := 0while i < n and j < m, doinsert m[i, j] into ... Read More

Advertisements