Found 33676 Articles for Programming

Find n-th lexicographically permutation of a strings in Python

Arnab Chakraborty
Updated on 25-Aug-2020 11:47:23

286 Views

Suppose we have a string whose length is m, and this string is containing only lowercase letters, we have to find the n-th permutation of string lexicographically.So, if the input is like string = "pqr", n = 3, then the output will be "qpr" as all permutations are [pqr, prq, qpr, qrp, rpq, rqp], they are in sorted order.To solve this, we will follow these steps −MAX_CHAR := 26MAX_FACT := 20factorials := an array of size MAX_FACTfactorials[0] := 1for i in range 1 to MAX_FACT, dofactorials[i] := factorials[i - 1] * isize := size of stringoccurrence := an array of ... Read More

Find N distinct numbers whose bitwise Or is equal to K in Python

Arnab Chakraborty
Updated on 25-Aug-2020 11:43:59

159 Views

Suppose we have two integers N and K; we have to find N unique values whose bit-wise OR is same as K. If there is no such result, then return -1So, if the input is like N = 4 and K = 6, then the output will be [6, 0, 1, 2].To solve this, we will follow these steps −MAX := 32visited := a list of size MAX and fill with Falseres := a new listDefine a function add() . This will take numpoint := 0value := 0for i in range 0 to MAX, doif visited[i] is non-zero, thengo for ... Read More

Find missing element in a sorted array of consecutive numbers in Python

Arnab Chakraborty
Updated on 25-Aug-2020 11:40:46

565 Views

Suppose we have an array A of n unique numbers, these n elements are present in the array in ascending order, but there is one missing element. We have to find the missing element.So, if the input is like A = [1, 2, 3, 4, 5, 6, 7, 9], then the output will be 8.To solve this, we will follow these steps −n := size of Aleft := 0right := n - 1mid := 0while right > left , domid := left +(right - left) / 2if A[mid] - mid is same as A[0], thenif A[mid + 1] - A[mid] ... Read More

Find minimum time to finish all jobs with given constraints in Python

Arnab Chakraborty
Updated on 25-Aug-2020 11:36:09

498 Views

Suppose we have an array of jobs with different time requirements, there are k different persons to assign jobs and we also have how much time t an assignee takes to do one unit of the job. We have to find the minimum time to complete all jobs with following constraints.An assignee can be assigned only the contiguous jobs.Two assignees cannot share or perform a single job.So, if the input is like k = 4, t = 5, job = {12, 6, 9, 15, 5, 9}, then the output will be 75 as we get this time by assigning [12], ... Read More

Find minimum adjustment cost of an array in Python

Arnab Chakraborty
Updated on 25-Aug-2020 11:31:24

550 Views

Suppose we have an array of positive numbers; we replace each element from that array array so that the difference between two adjacent elements in the array is either less than or equal to a given target. Now, we have to minimize the adjustment cost, so the sum of differences between new value and old value. More precisely, we minimize ∑|A[i] – Anew[i]| where i in range 0 to n-1, here n is denoted as size of A and Anew is the array with adjacent difference less than or equal to target.So, if the input is like [56, 78, 53, ... Read More

Find median of BST in O(n) time and O(1) space in Python

Arnab Chakraborty
Updated on 25-Aug-2020 11:28:40

540 Views

Suppose we have Binary Search Tree(BST), we have to find median of it. We know for even number of nodes, median = ((n/2th node + (n+1)/2th node) /2 For odd number of nodes, median = (n+1)/2th node.So, if the input is likethen the output will be 7To solve this, we will follow these steps −if root is same as None, thenreturn 0node_count := number of nodes in the treecount_curr := 0current := rootwhile current is not null, doif current.left null, thencount_curr := count_curr + 1if node_count mod 2 is not 0 and count_curr is same as(node_count + 1) /2, thenreturn ... Read More

Find maximum sum of triplets in an array such than i < j < k and a[i] < a[j] < a[k] in Python

Arnab Chakraborty
Updated on 25-Aug-2020 11:05:35

570 Views

Suppose we have an array of positive numbers, there are n elements in that array, we have to find the maximum sum of triplet (ai + aj + ak ) such that 0 A[i], thensecond_max := maximum of second_max, A[j]if first_max and second_max is non-zero, thenres := maximum of res, first_max + A[i] + second_maxreturn resExampleLet us see the following implementation to get better understanding − Live Demodef get_max_triplet_sum(A) :    n = len(A)    res = 0    for i in range(1, (n - 1)) :       first_max = 0       second_max = 0   ... Read More

Find maximum points which can be obtained by deleting elements from array in Python

Arnab Chakraborty
Updated on 25-Aug-2020 11:02:23

275 Views

Suppose we have an array A with N elements, we also have two integers l and r where, 1≤ ax ≤ 10^5 and 1≤ l≤ r≤ N. Taking an element from the array say ax and remove it, and also remove all elements equal to ax+1, ax+2 … ax+R and ax-1, ax-2 … ax-L from that array. By doing this it will cost ax points. We have to maximize the total cost after removing all of the elements from the array.So, if the input is like A = [2, 4, 3, 10, 5], l = 1, r = 2, then ... Read More

Find maximum operations to reduce N to 1 in Python

Arnab Chakraborty
Updated on 25-Aug-2020 10:59:50

262 Views

Suppose we have two numbers P and Q and they form a number N = (P!/Q!). We have to reduce N to 1 by performing maximum number of operations possible. In each operation, one can replace N with N/X when N is divisible by X. We will return the maximum number of operations that can be possible.So, if the input is like A = 7, B = 4, then the output will be 4 as N is 210 and the divisors are 2, 3, 5, 7.To solve this, we will follow these steps −N := 1000005factors := an array of ... Read More

Find maximum N such that the sum of square of first N natural numbers is not more than X in Python

Arnab Chakraborty
Updated on 25-Aug-2020 10:57:52

202 Views

Suppose we have a given integer X, we have to find the maximum value N so that the sum of first N natural numbers should not exceed the value X.So, if the input is like X = 7, then the output will be 2 as 2 is the maximum possible value of N, for N = 3, the sum of the series will exceed X = 7 So, 1^2 + 2^2 + 3^2 = 1 + 4 + 9 = 14.To solve this, we will follow these steps −Define a function sum_of_squares() . This will take Nres :=(N *(N + ... Read More

Advertisements