Thursday, March 12, 2015
Swapping bits of an Integer
Swapping individual bits is implemented using a function. This function is repeatedly called with different position data.
Wednesday, March 11, 2015
Printing the binary format of a integer
Here printing iss done thru a neat macro trick. We can calculate different way too, which is commonly seen. In that case we divide the number by 2 and not the modulo and append it to a string. Same time we will divide the number by two and make it as the new number. This process continues till the number becomes equivalent to one.
Saturday, January 31, 2015
Printing powerset for given set of characters.
Printing powerset of characters is somewhat easy. Here the key point is to understand that for a given number of n characters we will have (2^N)-1 combinations. We will then count thru integers starting from 1 to (2^N)-1 and then check the bit set on the integer and accordingly the same element will be printed from the character array.
Below is the C implementation of this interesting program :
Sunday, January 25, 2015
Stack implementation in C language.
Given below is a simple stack implementation in C language. Three main functions of a stack are implemented here, namely push, pop and top.
Tuesday, January 6, 2015
String compression
Given a string with repeated characters find out a way to compress the string with a count of number of times the character got appended.
For example a string like "aabbbccdeeefff" would become like "a2b3c2de3f3" after compression !
Sunday, December 21, 2014
Spiral (ZigZag printing) Printing the Binary Tree nodes in C
This is a variant of level order printing. In level order printing we can use iterative or a queue based approach. Iterative option is still available in this one too.But we are trying to follow a non iterative approach here.
Instead of a queue we will have two stacks for implementing Sprial printing (also called ZigZag printing) of the Binary Tree. First we push the root node to stack one. Then we visit both stacks and take the top item and store it in a temp variable, then we will pop the stack from which we had pushed the binary tree node. At the same time we will push into the alternate stack the children of the temporary node we had stored. On one stack we will push in right then left child order and in the other one we will be pushing in left and then right children order.
For a binary tree like below
1
/ \
2 3
/\ /\
4 5 6 7
the spiral printing order will be as below:
1
23
7654
Below is the implementation:
For a binary tree like below
1
/ \
2 3
/\ /\
4 5 6 7
the spiral printing order will be as below:
1
23
7654
Below is the implementation:
Tuesday, December 16, 2014
Kadanes Algorithm for finding sub-array with maximum sum
Here is the Kadanes algorithm implementation in Python. Here we pass thru the array in a linear fashion. While we pass thru the array we keep adding the elements. As we add the elements we keep track of previous largest sum ever found. We compare the present sum with so far largest sum
till we finish the entire array.
Subscribe to:
Posts (Atom)