Given a list of arraylists containing elements, write a function that prints out the permutations of of the elements such that, each of the permutation set contains only 1 element from each arraylist and there are no duplicates in the list of permutation sets. For example: consider the following lists L1= {a1,b1,c1,d1} L2= {a2,b2,c2} L3= […]
Category Archives: Algorithms
Find the rank of a number in the lexicographic order of its permutations For example: 312 has rank 5 in the sorted permutation list {123, 132, 213, 231, 312, 321}. A brute force method would be to generate all the permutation and sort them. This will be in exponential order as to generate all the […]
Permutation Permutation means arranging all the members of a set into some sequence or order, or if the set is already ordered, rearranging (reordering) its elements, a process called permuting. These differ from combinations, which are selections of some members of a set where order is disregarded. For example, written as tuples, there are six […]
Given a sorted positive integer array nums and an integer n, add/patch elements to the array such that any number in range [1, n] inclusive can be formed by the sum of some elements in the array. Return the minimum number of patches required. This is a Leetcode problem. Example 1: nums = [1, 3], […]
Given an array of integers and a value. Find all the pairs that sums up to the value in O(n) time with only one pass over the array. Can you do it efficiently without any space constraint? For example, Given A=[3, 0, -2, 1, 3, 6, 8] and sum = 6 then output pairs are: […]
Given two integers m and n , n>=m. Find total number of structurally correct BSTs possible with all numbers between m and n (inclusive). For example, m=3, n=5 then answer is 5. 3 5 3 5 4 \ / \ / / \ 4 4 5 3 3 5 \ / / \ 5 3 […]
Write a program to find the n-th ugly number. Ugly numbers are positive numbers whose prime factors only include 2, 3, 5. For example, 1, 2, 3, 4, 5, 6, 8, 9, 10, 12 is the sequence of the first 10 ugly numbers. Note that 1 is typically treated as an ugly number. First Attempt […]
Given a number N. Find the smallest even number greater than N such that the digits are a permutation of the number N. A trivial solution to start with would be to generate all the permutations of digits of the given number and then sort all the permutations. The first even permutation in the sorted […]
Given two big integers represented as strings, Multiplication them and return the production as string. For example, given a=2343324 and b=232232 then retiurn c = a*b = 23433242334323342 * 23223233232434324 = 544195652122144709711313995190808 We can see that the problem is trivial if we had given two small integers. But as the numbers can be really big […]
Design a class to calculate moving average of last N numbers in a stream of real numbers For example, if N=3 and the stream S=[2,3,4,1,2,-3,0,…] then moving averages at each each streamed number are = [2.0, 2.5, 3.0, 2.66, 2.33, 0, -0.33,…]. We need to design in a way that when a new element ins […]