Convert Binary Tree to Double Linked List in Zig-Zag Order

Question: given a binary tree, write an algorithm to convert the tree into a double-linked list. The list must be as if the tree is traversed in zig-zag and level order. Solution: let’s first understand what the objective is. By zig-zag level order, the question means that we need to traverse the tree in level order, a.k.a breadth first, such that the next level is traversed in the oposite direction to the current level. [Read More]

Convert array of positive numbers to sorted array

You are given an array of positive integers. Convert it to a sorted array with minimum cost. Only valid operation are Decrement -> cost = 1 Delete an element completely from the array -> cost = value of element For example: 4,3,5,6, -> cost 1 (decrement 4) 10,3,11,12 -> cost 3 (delete 3) Trying out DP We can make the DP more efficient You don’t need to scan the whole previous column when calculating costs of decrementing. [Read More]

Convert a Number (unknown base) to a Base 10 Number

Problem Given an integer, write a program that converts the given number to a number (in base 10). The base of the given number is unknown. Solution The problem statement states that the base of the given number is unknown. Thus to proceed one must need to assume a base for the number. It is practically safe to assume that the digit with the maximum value in the number denotes the maximum that can be accounted in the unknown base. [Read More]

atoi() in Java

Problem: Write a function to convert an ASCII string to integer, similar to atoi() function of C++. Solution: The solution is too simple, it’s simple checks for erroneous inputs that makes writing such a function fun. public class AsciiToInteger { public static void main(String args) { AsciiToInteger instance \= new AsciiToInteger(); int x \= instance.atoi("-683"); System.out.println("Conversion is: " + x); } private int atoi(String number) { // check for NULL or empty if(number \=\= null || number. [Read More]

itoa() in Java

Problem: Write a function to convert an integer value to its ASCII equivalent and return as a character array, similar to itoa() function of C++. Solution: The solution may seem tricky at first because the array is constructed left-to-right, whereas the number is read right-to-left. The easiest approach is to construct an array with digits from right-to-left and then reverse the array itself. The result being the number representation in character array. [Read More]

Taking input as string 1 - " C-String to Int "

Converting C-Strings to Integer If you want to convert a C-string (zero-terminated array of chars) of digits, you can call one of the library functions to do this (good idea), or write something like the following (good exercise). Character codes for digits Every character is represented by a pattern of bits. These patterns can be thought of as integers. If your system uses ASCII (or any of the newer standards), the integer value of the code for ‘0’ is 48, ‘1’ is 49, etc. [Read More]

Convert integer into a binary number

This code will print the binary number for the integer num: void generatebits(int num) { int temp; if(num!=0) { temp = num % 2; generatebits(num >>= 1); printf("%d",temp); } } int main() { int num; printf("\\nEnter a number\\n"); scanf("%d", &num); printf("\\n\\n"); generatebits(num); return(0); } Writing a function to convert integer to bits-string For example, if input is 2, the output is “00000000000000000000000000000010” if the system is 32-bit, “0000000000000010” if the system is 16-bit and so on. [Read More]

Convert the decimal number to any base (binary, hex, oct etc...)

#include 

int main()
{
  decimal_to_anybase(10, 2);
  decimal_to_anybase(255, 16);
  getch();
}

decimal_to_anybase(int n, int base)
{
  int i, m, digits[1000], flag;
  i=0;

  printf("\n\n[%d] converted to base [%d] : “, n, base);

  while(n)
  {
     m=n%base;
     digits[i]="0123456789abcdefghijklmnopqrstuvwxyz”[m];
     n=n/base;
     i++;
   }

   //Eliminate any leading zeroes
   for(i–;i>=0;i–)
   {
     if(!flag && digits[i]!='0’)flag=1;
     if(flag)printf("%c”,digits[i]);
   } 
}