Found 26504 Articles for Server Side Programming

Program to check all 1s are present one after another or not in Python

Arnab Chakraborty
Updated on 11-Oct-2021 06:55:36

145 Views

Suppose we have a list of numbers called nums that contains at least one element whose value is 1. We have to check whether all the 1s appear consecutively or not.So, if the input is like nums = [8, 2, 1, 1, 1, 3, 5], then the output will be True.To solve this, we will follow these steps −visited := 0for each x in nums, doif x is same as 1, thenif visited is same as 2, thenreturn Falsevisited := 1otherwise when visited is non-zero, thenvisited := 2return TrueExampleLet us see the following implementation to get better understandingdef solve(nums): ... Read More

Program to find number of optimal steps needed to reach destination by baby and giant steps in Python

Arnab Chakraborty
Updated on 11-Oct-2021 06:57:45

385 Views

Suppose we have a list of queries Q, where each query Q[i] contains a triplet [a_i, b_i and d_i]. Consider we are at position (0, 0) initially, then in one step we can move from some position (x1, y1) to (x2, y2) where Euclidean distance between these two points is at least a and at most b. Now for each queries we have to find minimum number of steps needed to reach (d_i, 0) from (0, 0).So, if the input is like Q = [(2, 3, 1), (1, 2, 0), (3, 4, 11)], then the output will be [2, 0, ... Read More

Program to find removed term from arithmetic sequence in Python

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

234 Views

Suppose we have an array called nums holding n-1 arithmetic sequence terms. One element except the first or last element of nums was removed before. We have to find the removed number.So, if the input is like nums = [5, 7, 11, 13], then the output will be 9 because, the items are following the formula 2i+5, so for i = 2 it will be 2*2 + 5 = 9 which is missing.To solve this, we will follow these steps −if size of nums is same as 2, thenreturn floor of (sum of all elements present in nums)/2if nums[0] is ... Read More

Program to find list that shows closest distance of character c from that index in Python

Arnab Chakraborty
Updated on 11-Oct-2021 06:47:55

447 Views

Suppose we have a string s and another character c, c must be present in s, we have to find a list whose length is same as length of s where for each index i its value is set the closest distance of s[i] to c.So, if the input is like s = "ppqppq" c = "q", then the output will be [2, 1, 0, 1, 1, 0]To solve this, we will follow these steps −j := size of sd := [j - 1] * jx := index of c in sfor i in range 0 to j - 1, ... Read More

Program to check a number is power of two or not in Python

Arnab Chakraborty
Updated on 11-Oct-2021 06:45:22

686 Views

Suppose we have a number n. We have to check whether this is power of 2 or not.So, if the input is like n = 2048, then the output will be True as 2048 is 2^11.To solve this, we will follow these steps −if n is same as 0, thenreturn Falsereturn true when (n AND (n - 1)) is same as 0 otherwise falseExampleLet us see the following implementation to get better understandingdef solve(n): if n == 0: return False return (n & (n - 1)) == 0 n = 2048 print(solve(n))Input2048 OutputTrue

Program to check number is perfect square or not without sqrt function in Python

Arnab Chakraborty
Updated on 11-Oct-2021 06:43:44

3K+ Views

Suppose we have a number n, we have to check whether n is a perfect square number or not. A perfect square number k can be represented as k = a * a for some integer a. We have to solve this without using built-in square root function.So, if the input is like n = 121, then the output will be True because 121 = 11*11.To solve this, we will follow these steps −if n is same as 0 or n is same as 1, thenreturn Truestart := 2stop := floor of n / 2while start n, thenstart := ... Read More

Program to check two spheres can ever meet by accelerating or not in a 3D space in Python

Arnab Chakraborty
Updated on 11-Oct-2021 06:47:52

101 Views

Suppose there are two spheres whose radius values are r1 and r2. They are at (x1, y1, z1) and (x2, y2, z2) coordinates. And their acceleration values are given like (ax1, ay1, az1) and (ax2, ay2, az2). We have to check whether these two spheres will ever meet on 3D space if they move with given acceleration or not.So, if the input is like r1 = 1 r2 = 2 pos1 = (0, 0, 0) acc1 = (100, 0, 0) pos2 = (4, 0, 0) acc2 = (0, 0, 0), then the output will be True, because second sphere has ... Read More

Program to find sum of digits that are inside one alphanumeric string in Python

Arnab Chakraborty
Updated on 11-Oct-2021 06:40:39

587 Views

Suppose we have an alphanumeric string s with digits from "0" to "9" and lowercase English letters. We have to find the sum of the numbers that are present in s. If digits are consecutive then consider them into a single number.So, if the input is like s = "hello25world63power86", then the output will be 174 because 25+63+86 = 174To solve this, we will follow these steps −ret := 0, curr := 0for each ch in s, doif ch is a digit, thencurr := 10 * curr + (ch as an integer)otherwise, ret := ret + currcurr := 0return ret ... Read More

Program to find the maximum sum of the subarray modulo by m in Python

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

278 Views

Program to find the maximum sum of the subarray modulo by m in PythonSuppose we have an array nums with n elements. We have another integer m. We have to find the maximum value of sum of any of its subarrays modulo m.So, if the input is like nums = [1, 5, 7, 3] m = 5, then the output will be 3 because[1] mod 5 = 1[5] mod 5 = 0[7] mod 5 = 2[3] mod 5 = 3[1, 5] mod 5 = 1[5, 7] mod 5 = 2[7, 3] mod 5 = 0[1, 5, 7] mod 5 = ... Read More

Program to check we can rearrange array to make difference between each pair of elements same in Python

Arnab Chakraborty
Updated on 11-Oct-2021 06:36:04

197 Views

Suppose we have a list called nums, we have to check whether we can rearrange the order of nums in such a way that the difference between every pair of consecutive two numbers is same.So, if the input is like nums = [8, 2, 6, 4], then the output will be True, because if we rearrange nums like [2, 4, 6, 8], then the difference between every two pair of consecutive numbers is 2.To solve this, we will follow these steps −N := size of numsif N

Advertisements