Vamsi Pavan’s Place

When curiousity outbursts …..

Entries Tagged as 'Algorithms'

Give a fast way to multiply a number by 7

October 6th, 2006 · No Comments · Algorithms

Represent 7 in binary format. Now shifting and adding by appropriate times will give the solution
or Multiply it by 8 (shift by 3 bits) and subtract the original number.

Bookmark it!
These icons link to social bookmarking sites where readers can share and discover new web pages.

Bookmark it! These icons link to social bookmarking sites where readers can share and discover new web pages.
  • bodytext
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google
  • Live
  • StumbleUpon
  • BlinkList
  • YahooMyWeb
  • NewsVine
  • blogtercimlap
  • Netvouz
  • Technorati
  • Slashdot
  • Print this article!

[Read more →]

Tags:

Check whether a number is power of 2

October 6th, 2006 · No Comments · Algorithms

Q. Give one line C expression to check whether a given number is an exponential power of 2
Ans:
if ( x & (x-1) == 0){
printf(”Yes”)
}else{
printf(”No”)
}
What about zero ??
Set the LSB to zero
x = x&x(-1);

Bookmark it!
These icons link to social bookmarking sites where readers can share and discover new web pages.

Bookmark it! These icons link to social bookmarking sites where readers can share and discover new web pages.
  • bodytext
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google
  • Live
  • StumbleUpon
  • BlinkList
  • YahooMyWeb
  • NewsVine
  • blogtercimlap
  • Netvouz
  • Technorati
  • Slashdot
  • Print this article!

[Read more →]

Tags:

In array of n numbers one numer repeating, detect it in one pass

October 6th, 2006 · No Comments · Algorithms

Repeated Number = Sigma(n) - Sigma(n-1)
Q. The integers from 1 to n are stored in an array in a random fashion. but one integer is missing. Write a program to find the missing integer.
Follow the same technique

Bookmark it!
These icons link to social bookmarking sites where readers can share and discover new web pages.

Bookmark it! These icons link to social bookmarking sites where readers can share and discover new web pages.
  • bodytext
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google
  • Live
  • StumbleUpon
  • BlinkList
  • YahooMyWeb
  • NewsVine
  • blogtercimlap
  • Netvouz
  • Technorati
  • Slashdot
  • Print this article!

[Read more →]

Tags:

print integer using putchar, in same order

October 6th, 2006 · No Comments · Algorithms

Given only putchar (no sprintf, itoa, etc.) write a routine putlong that prints out an unsigned long in decimal.
[The solution of taking % 10 and / 10, which gives us the decimal value in reverse order. This requires an array since we need to print it out in the correct order. The interviewer wasn’t too […]

Bookmark it! These icons link to social bookmarking sites where readers can share and discover new web pages.
  • bodytext
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google
  • Live
  • StumbleUpon
  • BlinkList
  • YahooMyWeb
  • NewsVine
  • blogtercimlap
  • Netvouz
  • Technorati
  • Slashdot
  • Print this article!

[Read more →]

Tags:

Graph problems

October 6th, 2006 · No Comments · Algorithms

BFS(G)
Breadth First search, will give the shortest path from the root, assuming all the edges are of same weight. You need additional datastructure like queue to implement it.
DFS(G)
Depth First Search, calculates the discovery and finished time of each node. These will help in classifying the edges into forward or tree, backward, cross edge.
The finishing time […]

Bookmark it! These icons link to social bookmarking sites where readers can share and discover new web pages.
  • bodytext
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google
  • Live
  • StumbleUpon
  • BlinkList
  • YahooMyWeb
  • NewsVine
  • blogtercimlap
  • Netvouz
  • Technorati
  • Slashdot
  • Print this article!

[Read more →]

Tags:

NQueens

October 6th, 2006 · No Comments · Algorithms

assume x[i] represents the position of queen i
canPlace(k,i)

Bookmark it!
These icons link to social bookmarking sites where readers can share and discover new web pages.

Bookmark it! These icons link to social bookmarking sites where readers can share and discover new web pages.
  • bodytext
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google
  • Live
  • StumbleUpon
  • BlinkList
  • YahooMyWeb
  • NewsVine
  • blogtercimlap
  • Netvouz
  • Technorati
  • Slashdot
  • Print this article!

[Read more →]

Tags:

Detect a cycle in a tree with only one pointer

October 6th, 2006 · No Comments · Algorithms

In one parse, count the number Normal nodes and NULL nodes. If there is no cycle then the
number of NULL nodes = No. Normal nodes + 1
If this is not followed, which means there is a cycle in the tree.
But since there is a cycle, you will never finish counting the number of nodes.

Bookmark it!
These […]

Bookmark it! These icons link to social bookmarking sites where readers can share and discover new web pages.
  • bodytext
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google
  • Live
  • StumbleUpon
  • BlinkList
  • YahooMyWeb
  • NewsVine
  • blogtercimlap
  • Netvouz
  • Technorati
  • Slashdot
  • Print this article!

[Read more →]

Tags:

Left and Right justification of text

October 6th, 2006 · No Comments · Algorithms

If the desired length of each line is n, then a line can be divided into three possible ways:
1. The line has exactly n characters, and hence doesn’t need any processing
2.. If the line has more than n characters, then we can always choose maximum number of words that can fit into a length of […]

Bookmark it! These icons link to social bookmarking sites where readers can share and discover new web pages.
  • bodytext
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google
  • Live
  • StumbleUpon
  • BlinkList
  • YahooMyWeb
  • NewsVine
  • blogtercimlap
  • Netvouz
  • Technorati
  • Slashdot
  • Print this article!

[Read more →]

Tags:

Longest monotone increasing subsequence

October 6th, 2006 · No Comments · Algorithms

A monotone increasing subsequence is a subset of numbers which are strictly increasing from left to rght. This definition does not require that the numbers be adjacent in the original set or that the longest sequence is unique. Ex.
1 2 9 4 7 3 11 8 14 6
The sequence 1 2 4 7 11 14 […]

Bookmark it! These icons link to social bookmarking sites where readers can share and discover new web pages.
  • bodytext
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google
  • Live
  • StumbleUpon
  • BlinkList
  • YahooMyWeb
  • NewsVine
  • blogtercimlap
  • Netvouz
  • Technorati
  • Slashdot
  • Print this article!

[Read more →]

Tags:

Finding the kth smallest element in an array

October 6th, 2006 · No Comments · Algorithms

Follow Quick sort paradigm
What if k

Bookmark it! These icons link to social bookmarking sites where readers can share and discover new web pages.
  • bodytext
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google
  • Live
  • StumbleUpon
  • BlinkList
  • YahooMyWeb
  • NewsVine
  • blogtercimlap
  • Netvouz
  • Technorati
  • Slashdot
  • Print this article!

[Read more →]

Tags: