Found 33676 Articles for Programming

Program to count number of fraction pairs whose sum is 1 in python

Arnab Chakraborty
Updated on 25-Nov-2020 12:55:17

2K+ Views

Suppose we have a list of fractions where each fraction is individual lists [numerator, denominator] which represents the number (numerator / denominator). We have to find the number of pairs of fractions whose sum is 1.So, if the input is like fractions = [[2, 7], [3, 12], [4, 14], [5, 7], [3, 4], [1, 4]], then the output will be 4, as (2/7 + 5/7), (3/12 + 3/4), (3/4 + 1/4), (4/14 + 5/7) are the four pairs which sum to 1.To solve this, we will follow these steps:d := a new mapans := 0for each fraction i in fractions, ... Read More

Program to find number not greater than n where all digits are non-decreasing in python

Arnab Chakraborty
Updated on 25-Nov-2020 12:53:29

300 Views

Suppose we have a number n, we have to find the largest number smaller or equal to n where all digits are non-decreasing.So, if the input is like n = 221, then the output will be 199.To solve this, we will follow these steps:digits := a list with all digits in nbound := nullfor i in range size of digits - 1 down to 0, doif digits[i] < digits[i - 1], thenbound := idigits[i - 1] := digits[i - 1] - 1if bound is not null, thenfor i in range bound to size of digits, dodigits[i] := 9join each digit ... Read More

Program to find kth smallest n length lexicographically smallest string in python

Arnab Chakraborty
Updated on 25-Nov-2020 12:50:44

555 Views

Suppose we have a number n and another value k. Now let us consider a string containing only "0", "1", and "2"s where no character is repeated in succession. We have to select such strings of length n and find the kth lexicographically smallest string. If there is no kth string, return empty string.So, if the input is like n = 4 k = 2, then the output will be "0120".To solve this, we will follow these steps:define a method solve() this will take s, k and lastif s is same as 0, thenreturn blank stringfor each character c in ... Read More

Program to generate first n lexicographic numbers in python

Arnab Chakraborty
Updated on 25-Nov-2020 12:48:34

342 Views

Suppose we have a number n, we have to find first n numbers that are sorted in lexicographic sequence.So, if the input is like n = 15, then the output will be [1, 10, 11, 12, 13, 14, 15, 2, 3, 4, 5, 6, 7, 8, 9]To solve this, we will follow these steps:count := 1ans := a list with single element countwhile size of ans < n, docount := count * 10while count > n , docount := quotient of count / 10count := count + 1while count mod 10 is same as 0, docount := quotient of count ... Read More

Program to find number of distinct combinations that sum up to k in python

Arnab Chakraborty
Updated on 25-Nov-2020 12:46:38

394 Views

Suppose we have a list of distinct numbers called nums and another number k, we have to find the number of distinct combinations that sum up to k. You can reuse numbers when creating combinations.So, if the input is like nums = [2, 4, 5] k = 4, then the output will be 2, as we can make two such groups like [2, 2] and [4].To solve this, we will follow these steps:table := a list with size k + 1, and fill with 0table[0] := 1for each num in nums, dofor i in range num to k, dotable[i] := ... Read More

Program to find minimum number of movie theatres required to show all movies in python

Arnab Chakraborty
Updated on 25-Nov-2020 12:44:17

292 Views

Suppose we have a list of intervals for different movie showings (they can be overlapped), we have to find the minimum number of theatres required to be able to show all of the movies.So, if the input is like intervals = [[20, 65], [0, 40], [50, 140]], then the output will be 2, as [20, 65] and [0, 40] are overlapping. [20, 65] and [50, 140] are also overlapping but [0, 40] and [50, 140] are not. So we need 2 theaters.To solve this, we will follow these steps:t := a new listfor each interval [a, b] in intervals, doinsert ... Read More

Program to find most occurring number after k increments in python

Arnab Chakraborty
Updated on 25-Nov-2020 12:42:25

190 Views

Suppose we have a list of numbers called nums and another value k. Let us consider an operation where we increase some element by one. We can perform at most k times, we have to find the value of the most frequently occurring number we can obtain. If there are more than one solution, select the smallest possible number.So, if the input is like nums = [1, 0, 0, 0, 8, 8, 8, 8] k = 8, then the output will be 8, as we can increase 1, 7 times to get 8, and increase any 0 to 1, so, ... Read More

Program to find sum of minimum trees from the list of leaves in python

Arnab Chakraborty
Updated on 25-Nov-2020 12:40:48

221 Views

Suppose we have a list of numbers called nums. This list is representing the leaf nodes in inorder traversal of a tree. Here the internal nodes have have 2 children and their value is same as the product of the largest leaf value of its left subtree and the largest leaf value of its right subtree. We have to find the sum of the tree with the minimum sum of its valuesSo, if the input is like nums = [3, 5, 10], then the output will be 83.To solve this, we will follow these steps:res := sum of all elements ... Read More

Program to find minimum sum subsequence by taking at least one element from consecutive 3 elements in python

Arnab Chakraborty
Updated on 25-Nov-2020 12:38:27

991 Views

Suppose we have a a list of numbers called nums, we have to find a minimum sum subsequence from the given list such that at least one number for all groups of three consecutive numbers is selected. If the length of given list is less than 3, a number should still be selected.So, if the input is like nums = [2, 3, 4, 5, 6, 7], then the output will be 7, as we can select 2 and 5.To solve this, we will follow these steps:n := size of numsif n is same as 0, thenreturn 0if n is same ... Read More

Program to find two pairs of numbers where difference between sum of these pairs are minimized in python

Arnab Chakraborty
Updated on 25-Nov-2020 12:36:20

256 Views

Suppose we have a list of numbers called nums and we want to select two pairs of numbers from it such that the absolute difference between the sum of these two pairs is minimized.So, if the input is like nums = [3, 4, 5, 10, 7], then the output will be 1, as we can select these pairs (3 + 7) - (4 + 5) = 1.To solve this, we will follow these steps:distances := a new listfor i in range 0 to size of nums - 2, dofor j in range i + 1 to size of nums - ... Read More

Advertisements