Programming Articles - Page 1015 of 3363

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

122 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

622 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

298 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

222 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

Program to find minimum element addition needed to get target sum in Python

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

305 Views

Suppose we have list of numbers called nums and another two variables k and t. Let us consider an operation where we choose an element say e in range [-k, k] and insert it to nums at the end. We have to find the minimum number of operations needed so the sum of the nums equals to target.So, if the input is like nums = [3, 1] k = 4 t = 19, then the output will be 4 because we can add like [3, 1, 4, 4, 4, 3] to get the sum 19.To solve this, we will follow ... Read More

Program to add one to a number that is shown as a digit list in Python

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

745 Views

Suppose we have an array called nums, containing decimal digits of a number. For example, [2, 5, 6] is for 256. We have to add 1 with this number and return the list in same format as before.So, if the input is like nums = [2, 6, 9], then the output will be [2, 7, 0].To solve this, we will follow these steps −i := size of nums - 1while i >= 0, doif nums[i] + 1 = 0:       if nums[i] + 1

Program to count number of 5-star reviews required to reach threshold percentage in Python

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

835 Views

Suppose we have a list called reviews and a threshold value t. Each item in reviews[i] has [x, y] means product i had x number of 5-star rating and y number of reviews. We have to find the minimum number of additional 5-star reviews we need so that the percentage of 5-star reviews for those items list is at least t percent.So, if the input is like reviews = [[3, 4], [1, 2], [4, 6]] threshold = 78, then the output will be 7, as in total there were 8 5-star reviews and 12 reviews. To reach 78% 5-star reviews, ... Read More

Program to find number of different substrings of a string for different queries in Python

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

349 Views

Suppose we have a string s whose length is n. We also have a list of queries Q, where Q[i] contains a pair (l, r). For each query we have to count number of different substrings of s in the inclusive range between l and r.So, if the input is like s = "ppqpp" Q = [(1, 1), (1, 4), (1, 1), (0, 2)], then the output will be [1, 8, 1, 5] becauseFor query (1, 1) the only substring is 'p' so output is 1For query (1, 4) the substrings are 'p', 'q', 'pq', 'qp', 'pp', 'pqp', 'qpp' and ... Read More

Program to count number of similar substrings for each query in Python

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

398 Views

Suppose we have two strings s and a set of query Q. Where Q[i] contains pair (l, r), for each substring of s from l to r, we have to find number of substrings s from x to y where they are similar. Two strings s and t are similar if they follow these rules −They are of same lengthFor each pair of indices (i, j), if s[i] is same as s[j], then it must satisfy t[i] = t[j], and similarly if s[i] is not same as s[j], then t[i] and t[j] must be different.So, if the input is like ... Read More

Advertisements