Programming Articles - Page 1014 of 3363

Program to find multiple of n with only two digits in Python

Arnab Chakraborty
Updated on 11-Oct-2021 07:15:16

271 Views

Suppose we have a number n. We have to find the least positive value x such that x is made up of only two digits 9's and 0's, and x is multiple of n.So, if the input is like n = 26, then the output will be 90090.To solve this, we will follow these steps −m := 9x := 1while m is not divisible by n, dox := x + 1m := replace all 1s with 9s in the binary form of xreturn m as integerExampleLet us see the following implementation to get better understanding −def solve(n):    m = ... Read More

Program to count index pairs for which array elements are same in Python

Arnab Chakraborty
Updated on 11-Oct-2021 07:09:51

650 Views

Suppose we have a list of numbers called nums. We have to find the number of pairs i < j such that nums[i] and nums[j] are same.So, if the input is like nums = [5, 4, 5, 4, 4], then the output will be 4, as We have index pairs like (0, 2), (1, 3), (1, 4) and (3, 4).To solve this, we will follow these steps −c := a list containing frequencies of each elements present in numscount := 0for each n in list of all values of c, docount := count + floor of (n *(n - 1)) ... Read More

Program to sort given set of Cartesian points based on polar angles in Python

Arnab Chakraborty
Updated on 11-Oct-2021 07:05:51

1K+ Views

Suppose we have a set of Cartesian points in a list called points. We have to sort them based on their polar angles. The polar angles vary in range 0 and 2*PI. If some points have same polar angles, then arrange them based on the distance of that point from the origin.So, if the input is like points = [(1, 1), (1, -2), (-2, 2), (5, 4), (4, 5), (2, 3), (-3, 4)], then the output will be [(5, 4), (1, 1), (4, 5), (2, 3), (-3, 4), (-2, 2), (1, -2)]To solve this, we will follow these steps −Define ... Read More

Program to find duplicate item from a list of elements in Python

Arnab Chakraborty
Updated on 11-Oct-2021 07:00:09

231 Views

Suppose we have a list of elements called nums of size n + 1, they are selected from range 1, 2, ..., n. As we know, by pigeonhole principle, there must be a duplicate. We have to find the duplicate. Here our target is to find the task in O(n) time and constant space.So, if the input is like nums = [2, 1, 4, 3, 5, 4], then the output will be 4To solve this, we will follow these steps −q := sum of all elements present in numsn := size of numsv := floor of ((n - 1)*(n)/2)return q ... Read More

Program to count elements whose next element also in the array in Python

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

245 Views

Suppose we have a list of numbers say nums, we have to find the number of elements x in the array, such that x + 1 also exists in the array.So, if the input is like nums = [4, 2, 3, 3, 7, 9], then the output will be 3, because 2+1 = 3 is present, 3+1 = 4 is present and another 3 is present so total 3.To solve this, we will follow these steps −answer := 0c := a list containing frequencies of each elements present in numsdlist := a list from list of all keys of cfor ... Read More

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

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

170 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

410 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

258 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

462 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

727 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

Advertisements