Saturday, March 12, 2016

Check if a string has all unique characters

Code:

using System;

public class Test
{
    public static void Main()
    {
        string str = "Asif";

        Console.WriteLine("Input: " + str);
       
        if(new Test().IsUniqueChar(str))
        {
         Console.WriteLine("Output: " + str + " have unique characters.");
        }
        else
        {
         Console.WriteLine("Output: " + str + " does not have unique characters.");
        }

       
    }

    public bool IsUniqueChar(string str)
    {
     int n = str.Length;
   
        for(int i = 0; i < n - 1 ; i++)
        {
         for(int j= i + 1; j < n; j++)
         {
          if(str.Substring(i,1) == str.Substring(j,1))
          {
           return false;
          }
         }
        }
       
        return true;
    }

}

Output:

Input: Asif
Output: Asif have unique characters.

Run Time: O(N^2)

Wednesday, March 9, 2016

Find the square root of the number

Code:

Method - 1:

using System;

public class Test
{
    public static void Main()
    {
        int n = 25;

        Console.WriteLine("Input: " + n);

        Console.WriteLine("Output:" + new Test().SquareRoot(n, 1, n));
    }

    public int SquareRoot(int n, int min, int max)
    {
        if(min > max)
        {
            return -1;
        }

        int value = (min + max) / 2;

        if (value * value == n)
        {
            return value;
        }
        else if (value * value > n)
        {
            return SquareRoot(n, min, value - 1);
        }
        else
        {
            return SquareRoot(n, value + 1, max);
        }
    }


}

Run Time: O(log n)

Method - 2:

using System;

public class Test
{
    public static void Main()
    {
        int n = 25;

        Console.WriteLine("Input: " + n);

        Console.WriteLine("Output:" + new Test().SquareRoot(n));
    }

    public int SquareRoot(int n)
    {
        for(int i = 1; i * i <= n ; i++)
        {
        if(i * i ==  n)
        {
        return i;
        }
        }
        
        return -1;
    }


}

Run Time: O(n^1/2)

Output:


Input: 25
Output:5

Print all the square of 2 from 0 to n

Code:

using System;

public class Test
{
    public static void Main()
    {
        int n = 4;

        Console.WriteLine("Input: " + n);

        Console.WriteLine("Output:");
        
        new Test().PowerOf2(n);
    }

    public int PowerOf2(int n)
    {
        if (n <= 1)
        {
        Console.WriteLine(1);
            return 1;
        }
        else
        {
        int value = 2 * PowerOf2(n/2);
            Console.WriteLine(value);
            return value;
        }
    }


}

Output:

Input: 7
Output:
1
2
4

Run Time: O(log n)

Print all the Fibonacci numbers from 0 to n

Code:

Method - 1:

using System;

public class Test
{
    public static void Main()
    {
        int n = 5;

        Console.WriteLine("Input: " + n);

        Console.WriteLine("Output: ");

        new Test().Fibonacci(n);
    }

    public void Fibonacci(int n)
    {
        int prevprev = 0;
        int prev = 1;

    Console.WriteLine(prevprev);

        for(int i=1; i <= n ; i++)
        {
        Console.WriteLine(prev);
        
        prev = prev + prevprev;
            prevprev = prev - prevprev;
        }
    }

}


Run Time: O(N)

Method - 2:

using System;

public class Test
{
    public static void Main()
    {
        int n = 5;

        Console.WriteLine("Input: " + n);
        Console.WriteLine("Output: ");
        new Test().AllFibonacci(n);
    }

public void AllFibonacci(int n)
{
for(int i = 0; i <= n; i++)
{
Console.WriteLine(Fibonacci(i));
}
}
    
    public int Fibonacci(int n)
    {
        if (n == 0)
        {
            return 0;
        }
        else if (n == 1)
        {
            return 1;
        }
        else
        {
            return Fibonacci(n - 1) + Fibonacci(n - 2);
        }
    }

}

Run Time: O(2^N)

Method - 3:

using System;

public class Test
{
    public static void Main()
    {
        int n = 5;

        Console.WriteLine("Input: " + n);
        Console.WriteLine("Output: ");
        new Test().AllFibonacci(n);
    }

 public void AllFibonacci(int n)
 {
  int[] cache = new int[n+1];
  for(int i = 0; i <= n; i++)
  {
   Console.WriteLine(Fibonacci(i,cache));
  }
 }
    
    public int Fibonacci(int n, int[] cache)
    {
        if (n == 0)
        {
            return 0;
        }
        else if (n == 1)
        {
            return 1;
        }
        else if(cache[n] > 0)
        {
         return cache[n];
        }
        else
        {
         cache[n] = Fibonacci(n - 1, cache) + Fibonacci(n - 2, cache); 
            return cache[n];
        }
    }
}

Run Time: O(n)

Output:

Input: 5
Output: 
0
1
1
2
3
5


Print all the Fibonacci numbers less than value n

Code:

using System;

public class Test
{
    public static void Main()
    {
        int n = 13;

        Console.WriteLine("Input: " + n);

        Console.WriteLine("Output: ");

        new Test().Fibonacci(n);
    }

    public void Fibonacci(int n)
    {
        int prevprev = 0;
        int prev = 1;

    Console.WriteLine(prevprev);

        while(prev <= n)
        {
        Console.WriteLine(prev);
        
        prev = prev + prevprev;
            prevprev = prev - prevprev;
        }
    }

}

Output:

Input: 13
Output: 
0
1
1
2
3
5
8
13

Find nth Fibonacci number

Code:

using System;

public class Test
{
    public static void Main()
    {
        int n = 6;

        Console.WriteLine("Input: " + n);

        Console.WriteLine("Output: " + n + "th Fibonacci number is " + new Test().Fibonacci(n));
    }

    public int Fibonacci(int n)
    {
        if(n == 0)
        {
        return 0;
        }
        else if(n == 1)
        {
        return 1;
        }
        else
        {
        return Fibonacci(n-1) + Fibonacci(n-2);
        }
    }

}

Output:

Input: 6
Output: 6th Fibonacci number is 8

Run Time: O(2^N)

Print all the permutations of a string

Code:

using System;

public class Test
{
    public static void Main()
    {
        string str = "Asif";
        
        Console.WriteLine("Input: " + str);
        
        Console.WriteLine("Output: ");
        new Test().Permutations(str,"");
    }

    public void Permutations(string str, string prefix)
  {
  if(str.Length == 0)
  {
  Console.WriteLine(prefix);
  }
 
  for(int i = 0; i < str.Length; i++)
{
string sPrefix = prefix + str.Substring(i,1);
string sString = str.Substring(0,i) + str.Substring(i+1);

Permutations(sString, sPrefix);
}
}

}

Output:

Input: Asif
Output: 
Asif
Asfi
Aisf
Aifs
Afsi
Afis
sAif
sAfi
siAf
sifA
sfAi
sfiA
iAsf
iAfs
isAf
isfA
ifAs
ifsA
fAsi
fAis
fsAi
fsiA
fiAs
fisA

Run Time: O(n!)


Find the factorial for the given number

Code:

using System;

public class Test
{
    public static void Main()
    {
    int n = 4;
        
        Console.WriteLine("Input: " + n);
        
Console.WriteLine("Output: Factorial for " + n + " is " + new Test().Factorial(n));
    }

    public int Factorial(int n)
{
if(n == 2)
{
return 2;
}
else
{
return n * Factorial(n - 1);
}
}

}

Output:

Input: 4
Output: Factorial for 4 is 24

Run Time: O(N)

Check if the given number is Prime Number or not

Code:

using System;

public class Test
{
    public static void Main()
    {
    int n = 25;
        
        Console.WriteLine("Input: " + n);
        
        if(new Test().IsPrime(n) == true)
        {
        Console.WriteLine("Output: " + n + " is Prime Number");
        }
        else
        {
        Console.WriteLine("Output: " + n + " is not Prime Number");
        }

        Console.ReadKey();
    }

    public bool IsPrime(int n)
{
for(int i=2; i * i < n; i++)
{
if(n % i == 0)
{
return false;
}
}

return true;
}

}

Output:

Input: 25
Output: 25 is Prime Number

Run Time: O(N^(1/2))

Reveres array and it's run time

Code:

using System;

public class Test
{
    public static void Main()
    {
        Console.WriteLine("Start");

        int[] array = new int[5];
        array[0] = 1;
        array[1] = 2;
        array[2] = 3;
        array[3] = 4;
        array[4] = 5;
        
        new Test().Reverse(array);
        new Test().Print(array);

        Console.WriteLine("End");
        Console.ReadKey();
    }

    public void Reverse(int[] array)
{
for(int i = 0; i
{
int temp = array[i];
array[i] = array[array.Length - i - 1];
array[array.Length - i - 1] = temp;
}
}

public void Print(int[] array)
{
for(int i = 0; i
{
Console.WriteLine(array[i]);
}
}

}

Output:


Start
5
4
3
2
1
End

Run Time: O(N)