Programming Articles - Page 935 of 3363

Program to find lexicographically smallest string to move from start to destination in Python

Arnab Chakraborty
Updated on 25-Oct-2021 06:47:12

251 Views

Suppose we are at at (0, 0) position in the Cartesian plane. We want to go to the point (x, y) using only horizontal(H) and vertical(V) moves of single unit. There are more than one possible ways to reach destination. Each way comprises of few H moves and few V moves. (For example if we want to go to point (2, 2) from point (0, 0), then HVVH is one of the possible ways.) If we have another value k, we have to find the lexicographically kth smallest way of going to the destination.So, if the input is like (x, ... Read More

Program to find n length string made of letters from m sized alphabet with no palindrome in Python

Arnab Chakraborty
Updated on 25-Oct-2021 06:43:36

343 Views

Suppose we have m number of letters and another value n. We have to count number of strings of length n created with letters by taking from these m letters, and string has no palindromic substring of length greater than 1. If answer is too large then mod the result by 10^9+7.So, if the input is like n = 2 m = 3, then the output will be 6 because m = 3, so if the alphabets are {x, y, z}, we can generate strings like: [xx, xy, xz, yx, yy, yz, zx, zy, zz] but [xx, yy, zz] are ... Read More

Program to find number of consecutive subsequences whose sum is divisible by k in Python

Arnab Chakraborty
Updated on 25-Oct-2021 06:41:10

887 Views

Suppose we have an array nums and a value k. We have to find number of consecutive subsequences whose sum is divisible by k.So, if the input is like k = 3 nums = [1, 2, 3, 4, 1], then the output will be 4 because the subsequences are [3], [1, 2], [1, 2, 3] and [2, 3, 4].To solve this, we will follow these steps −x := An array of size k and fill with 0x[0] := 1r:= 0, s:= 0for each elem in nums, dos :=(s + elem) mod kr := r + x[s]x[s] := x[s] + 1return ... Read More

Program to find number of pairs (i, j) such that ith and jth elements are same in Python

Arnab Chakraborty
Updated on 25-Oct-2021 06:37:43

327 Views

Suppose we have an array nums. We have to find number of pairs (i, j) are there such that nums[i] = nums[j] but i is not same as j.So, if the input is like nums = [1, 3, 1, 3, 5], then the output will be 4, because the pairs are (0, 2), (2, 0), (1, 3) and (3, 1)To solve this, we will follow these steps −d := a new mapfor each c in nums, dod[c] := (d[c] + 1) when c is present in d otherwise 1res := 0for each c is in the list of elements (x ... Read More

Program to find sum of differences between max and min elements from randomly selected k balls from n balls in Python

Arnab Chakraborty
Updated on 25-Oct-2021 06:35:10

189 Views

Suppose we have n balls which are numbered by an array nums, whose size is n and nums[i] represents the number of ball i. Now we have another value k. In each turn we pick k balls from n different balls and find the difference of maximum and minimum values of k balls and store the difference in a table. Then put these k balls again into that pot and pick again until we have selected all possible selections. Finally find the sum of all differences from the table. If the answer is too large, then return result mod 10^9+7.So, ... Read More

Program to find number of ways we can merge two lists such that ordering does not change in Python

Arnab Chakraborty
Updated on 25-Oct-2021 06:30:58

240 Views

Suppose we have two lists nums1 and nums2. Now the constraint is when we merge the order of elements in each list does not change, for example, if the elements are [1, 2, 3] and [4, 5, 6], then some valid merged lists are [1, 4, 2, 3, 5, 6] and [1, 2, 3, 4, 5, 6], there may be some other valid merge sequence. So if we have the size of lists N and M. We have to find number of ways we can merge them to get valid list. If the answer is too large the return result ... Read More

Program to find list of all possible combinations of letters of a given string s in Python

Arnab Chakraborty
Updated on 25-Oct-2021 06:27:49

7K+ Views

Suppose we have a string s. We have to find all possible combinations of letters of s. If there are two strings with same set of characters, then show the lexicographically smallest of them. And one constraint is each character in s are unique.So, if the input is like s = "pqr", then the output will be ['r', 'qr', 'q', 'pr', 'pqr', 'pq', 'p']To solve this, we will follow these steps −st_arr := a new listfor i in range size of s - 1 to 0, decrease by 1, dofor j in range 0 to size of st_arr - 1, ... Read More

Program to find number of ways we can get n R.s using Indian denominations in Python

Arnab Chakraborty
Updated on 23-Oct-2021 08:53:32

253 Views

Suppose we have limited coins of denominations (₹1, ₹2, ₹5 and ₹10). We have to find in how many ways can you sum them up to a total of ₹n? We have an array count of size 4, where count[0] indicates coins of ₹1, count[1] indicates coins of ₹2 and so on.So, if the input is like n = 25 count = [7, 3, 2, 2], then the output will be 9.To solve this, we will follow these steps −denom := [1, 2, 5, 10]A := an array of size (n + 1) and fill with 0B := a new ... Read More

Program to find nCr values for r in range 0 to n, in an efficient way in Python

Arnab Chakraborty
Updated on 23-Oct-2021 08:49:26

626 Views

Suppose we have to calculate nCr values many times. We can solve this very efficient way. If we store the lower values of nCr we can easily find higher values. So if we have n, we have to find list of nC0 to nCn. If answer is too large then return that modulo 10^9.So, if the input is like n = 6, then the output will be [1, 6, 15, 20, 15, 6, 1].To solve this, we will follow these steps −items := a list with single element 1for r in range 1 to n, doinsert floor of (last element ... Read More

Program to find total area covered by two rectangles in Python

Arnab Chakraborty
Updated on 23-Oct-2021 08:46:25

390 Views

Suppose we want to find the total area covered by two rectilinear rectangles in a 2D plane. Here each rectangle is defined by its bottom left corner and top right corner as shown in the figure.To solve this, we will follow these steps −width_1 := |C-A|, height_1 := |D-B|width_2 := |G-E|, height_2 := |H-F|area := width_1*height_1 + width_2*height_2if (GC) or (F>D) or (HD) or (H

Advertisements