[ALGO] Water jugs problem
Labels: algorithms, cormen, sort, water jugs
Suppose that you are given 'n' red and 'n' blue water jugs, all of different shapes and sizes. All red jugs hold different amounts of water, as do the blue ones. For every red jug, there is a blue jug that holds the same amount of water & vice versa.
How can you find the grouping of the jugs into pairs of red & blue jugs that hold the same amount of water, in the minimum number of comparisons.
Operations allowed
How can you find the grouping of the jugs into pairs of red & blue jugs that hold the same amount of water, in the minimum number of comparisons.
Operations allowed
- always compare between a red and a blue jar (no two reds, no two blues)
- fill water in red / blue jug
- pour from red jug to blue (& vice versa). This will help compare the capacity of the jugs.
Find if number occurs exactly n/2 times in an array
Labels: algorithms, problem solving
You have an array of number/characters. If there is a number which occurs exactly n/2 times.
and all other elements in the set/array are distinct, can you find the the repeating number?
Expected time complexity : O(n)
Expected space complexity : O(1)
What if the other members are not distinct?
Can we solve the problem in O(log n) time complexity? (distinct and / or not distinct)
Source : http://inder-gnu.blogspot.com/2009/08/number-occuring-n2-times.html
and all other elements in the set/array are distinct, can you find the the repeating number?
Expected time complexity : O(n)
Expected space complexity : O(1)
What if the other members are not distinct?
Can we solve the problem in O(log n) time complexity? (distinct and / or not distinct)
Source : http://inder-gnu.blogspot.com/2009/08/number-occuring-n2-times.html
Dividing array into groups
Labels: algorithms, array, problem solving
Divide a list of numbers into groups of consecutive numbers but their original order should be preserved.
Example:
<8,2,4,7,1,0,3,6>
should be divided into two groups like this:
<2,4,1,0,3> <8,7,6>
Source : http://discuss.techinterview.org/default.asp?interview.11.770712.8
Example:
<8,2,4,7,1,0,3,6>
should be divided into two groups like this:
<2,4,1,0,3> <8,7,6>
Source : http://discuss.techinterview.org/default.asp?interview.11.770712.8
Sort on second key when, first key in a set is sorted
Labels: algorithms, sort
Assume that we are given as input n pairs of items, where
For example: (1,blue), (3,red), (4,blue), (6,yellow), (9,red) should become (3,red), (9,red), (1,blue), (4,blue), (6,yellow).
- the first item is a number
- the second item is one of three colors (red, blue, or yellow).
For example: (1,blue), (3,red), (4,blue), (6,yellow), (9,red) should become (3,red), (9,red), (1,blue), (4,blue), (6,yellow).
Find if summation of numbers from two lists is given number
Labels: algorithms, search, sort
Problem: Given two lists A & B define an algo, which will identify two numbers 'a' and 'b', such that a + b = x, where 'x' is the input.
Subscribe to:
Posts (Atom)



