Showing posts with label sort. Show all posts
Showing posts with label sort. Show all posts

[ALGO] Water jugs problem

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
  1. always compare between a red and a blue jar (no two reds, no two blues)
  2. fill water in red / blue jug
  3. pour from red jug to blue (& vice versa). This will help compare the capacity of the jugs.
Source : Introduction to Algorithms (by Cormen..., Problem 8-4)

Sort on second key when, first key in a set is sorted

Assume that we are given as input n pairs of items, where
  1. the first item is a number
  2. the second item is one of three colors (red, blue, or yellow).
Further, assume that the items are sorted by number. Give an O(n) algorithm to sort the items by color (all reds before all blues before all yellows) such that the numbers for identical colors stay sorted.

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

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.
 
Moviehole-madhurtanwani © 2010 | Designed by Chica Blogger | Back to top