Found 26504 Articles for Server Side Programming

Program to find minimum absolute sum difference in Python

Arnab Chakraborty
Updated on 07-Oct-2021 12:51:40

443 Views

Suppose we have two positive valued arrays nums1 and nums2, of same size. The absolute sum difference of these two arrays is the sum of |nums1[i] - nums2[i]| for each 0 [2, 2, 6], orReplace the element at index 1 with the element at index 2: [2, 8, 6] => [2, 6, 6].Both of them get a sum difference of |2-3| + (|2-4| or |6-4|) + |6-6| = 3.To solve this, we will follow these steps −if nums1 is same as nums2, thenreturn(0)minn_diff := -infinityind := -1for i in range 0 to size of nums1 - 1, doif |nums1[i]-nums2[i]| ... Read More

Program to get maximum length merge of two given strings in Python

Arnab Chakraborty
Updated on 07-Oct-2021 12:48:18

211 Views

Suppose we have two strings s and t. We have to make a string called merge in the following way: while either s or t are non-empty, select one of the following options −If s is non-empty, then append the first character in s to merge and delete it from s.If t is non-empty, then append the first character in t to merge and delete it from t.So we have to find the lexicographically largest merge we can make.So, if the input is like s = "zxyxx" t = "yzxxx", then the output will be zyzxyxxxxx, becausePick from s: merge ... Read More

Program to find total similarities of a string and its substrings in Python

Arnab Chakraborty
Updated on 07-Oct-2021 12:55:37

351 Views

Suppose we have a string s. We have to find the sum of similarities of string s with each of it's suffixes. Here the similarity between two strings are the length of the longest prefix common to both strings.So, if the input is like s = "pqpqpp", then the output will be 11 because the suffixes of the string are "pqpqpp", "qpqpp", "pqpp", "qpp", "pp" and "p". The similarities of these substrings with the string "pqpqpp" are 6, 0, 3, 0, 1, and 1. So the summation is 6 + 0 + 3 + 0 + 1 + 1 = ... Read More

Program to count nice pairs in an array in Python

Arnab Chakraborty
Updated on 07-Oct-2021 12:44:31

384 Views

Suppose we have an array called nums with non-negative values. We have to find number nice pairs of indices present in the array, if the answer is too large, then return answer mod 10^9+7. Here a pair of indices (i, j) is said to be nice when they satisfy all of these conditions: 1. 0

Program to convert hour minutes’ time to text format in Python

Arnab Chakraborty
Updated on 07-Oct-2021 12:42:35

630 Views

Suppose we have two inputs hour and minutes. We have to show the time in text format. This is like −8:00 : 8'o clock8:01 : one minute past eight8:10 : ten minutes past eight8:15 : quarter past eight8:30 : half past eight8:40 : twenty minutes to nine8:45 : quarter to nine8:47 : thirteen minutes to nine8:28 : twenty eight minutes past eightSo, if the input is like h = 9, m = 42, then the output will be eighteen minutes to tenTo solve this, we will follow these steps −text:= a list containing texts for 30 different numeric values as ... Read More

Program to check whether two sentences are similar or not in Python

Arnab Chakraborty
Updated on 07-Oct-2021 12:39:15

1K+ Views

Suppose we have two sentences s and t. We have to check whether they are similar or not. Here sentence has only English letters. Two sentences are said to be similar when it is possible to add an arbitrary sentence (possibly empty) inside one of these given sentences such that the two sentences become equal.So, if the input is like s = "we live at city Kolkata" t = "city Kolkata", then the output will be True because we can get s from t by adding sentence "we live in".To solve this, we will follow these steps −s1 := a ... Read More

Program to find total sum of all substrings of a number given as string in Python

Arnab Chakraborty
Updated on 07-Oct-2021 12:36:02

493 Views

Suppose we have a number in string format, and we have to find the sum of all substrings of s. The answer may be very large, so return result modulo 10^9+7.So, if the input is like s = "268", then the output will be 378 as the substrings are "2", "6", "8", "26", "68" and "268" total sum is 378.To solve this, we will follow these steps −M := 10^9 + 7sum_val := 0B := 1res := 0for i in range size of s - 1 down to 0, decrease by 1, dores :=(res + digit value of s[i] * ... Read More

Program to find minimum remove required to make valid parentheses in Python

Arnab Chakraborty
Updated on 07-Oct-2021 12:34:13

418 Views

Suppose we have a string s with parenthesis '(' , ')' and lowercase English characters. We have to delete the minimum number of parentheses ( '(' or ')', from any positions ) so that the resulting parentheses string is valid and have to finally return any valid string. Here the parentheses string is valid when all of these criteria are fulfilled −The string is empty and contains lowercase characters only, orThe string can be written as AB (A concatenated with B), where A and B are valid strings, orThe string can be written as the form of (A), where A ... Read More

Program to find numbers with same consecutive differences in Python

Arnab Chakraborty
Updated on 07-Oct-2021 12:29:23

378 Views

Suppose we have to find an array of size N such that the absolute difference between every two consecutive digits is K. Every number in the answer must not have leading zeros except for the number 0 itself.So, if the input is like N = 4 K = 7, then the output will be [1818, 2929, 7070, 8181, 9292], here 0707 is not valid as it has leading 0.To solve this, we will follow these steps −if N is same as 1, thenreturn a new list from range 0 to 9queue := make a queue with all elements from 1 ... Read More

Program to find how many swaps needed to sort an array in Python

Arnab Chakraborty
Updated on 07-Oct-2021 12:32:03

2K+ Views

Suppose, we have an array called nums and we have to find the number of swaps needed to make nums sorted in any order, either ascending or descending.So, if the input is like nums = [2, 5, 6, 3, 4], then the output will be 2 because initially nums has [2, 5, 6, 3, 4]. If we swap numbers 6 and 4, the array will be [2, 5, 4, 3, 6]. Then, if we swap the numbers 5 and 3, the array will be [2, 3, 4, 5, 6]. So 2 swaps are needed to make the array sorted in ... Read More

Advertisements