Write a Program to find all the Pythagorean triples.

Given a sorted array of integers and we have to find all Pythagorean triples.

Definition from Wikipedia : A Pythagorean triple consists of three positive integers a, b, and c, such that a2 + b2 = c2. Such a triple is commonly written (a, b, c), and a well-known example is (3, 4, 5).

Code :

        public static void PythagoreanTriple(int[] input)
        {

            int length = input.Length;
            if (length < 3)
            {
                Console.WriteLine("Length of Array can not be less then 3");
                return;
            }

            for (int i = 0; i < length; i++)
            {
                input[i] = input[i] * input[i];
            }

            int leftPointer = 0;
            int rightPointer = 0;
            int temp = 0;
            for (int i = length - 1; i > 1; i--)
            {
                leftPointer = 0;
                rightPointer = i - 1;
                temp = input[i]; ;
                while (leftPointer < rightPointer)
                {
                    if (input[leftPointer] + input[rightPointer] == temp)
                    {
                        Console.WriteLine(Math.Sqrt(input[leftPointer]) + ";" 
                                                    + Math.Sqrt(input[rightPointer]) 
                                                    + ";" + Math.Sqrt(input[i]));
                        break;
                    }
                    else if (input[leftPointer] <= input[rightPointer])
                    {
                        leftPointer++;
                    }
                    else
                    {
                        rightPointer--;
                    }
                }
            }

        }
Test Case:
Input Output
{ 1, 3, 4,5, 6, 7, 8, 10, 11 } 6;8;10

3;4;5

{ 1, 3, 4,5, 6, 7, 8, 10, 11,12,13 } 5;12;13

6;8;10

3;4;5

{ 5, 6, 7, 8, 10, 11,12,13,14,15,17 } 8;15;17

5;12;13

6;8;10


Write a Program For Binary Search

Binary Search is used to find a given key in a sorted array. In each step, the algorithm compares the search key value with the key value of the middle element of the array. If the keys match, then a matching element has been found and its index, or position, is returned. Otherwise, if the search key is less than the middle element’s key, then the algorithm repeats its action on the sub-array to the left of the middle element or, if the search key is greater, on the sub-array to the right. If the remaining array to be searched is empty, then the key cannot be found in the array and a special “not found” indication is returned.

Code

        public static int BinarySearch(int[] input, int key)
        {

            int leftPointer = 0;
            int length = input.Length;
            int rightPointer = length - 1;
            int middlePointer = 0;

            while (rightPointer > leftPointer)
            {
                middlePointer = (rightPointer + leftPointer) / 2;

                if (input[middlePointer] < key)
                {
                    leftPointer = leftPointer + 1;
                }
                else if (input[middlePointer] > key)
                {
                    rightPointer = rightPointer - 1;
                }
                else
                {
                    return middlePointer;
                }
            }
            return -1;
        }

Get List of Tables Present in SQL Server Database

Today I have to get a list of all tables present in database. I used following query to get the list of all tables present in SQL Server Database.

SELECT TABLE_NAME  
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_TYPE = 'BASE TABLE' order by TABLE_NAME

Find the Length of a given number C#

        public static long Length(this long number)
        {

            long length = 0;

            if (number < 10 && number >= 0)
            {
                length = 1;
                return length;
            }

            number = Math.Abs(number);

            while (number > 0)
            {
                length += 1;
                number = number / _base;
            }

            return length;
        }

Test Case

Length of number 123 is  3

Length of number 1234567890 is  10

Length of number -123456 is  6

Length of number 9 is  1

Length of number 545 is  3

Length of number 123454321 is  9

Length of number -123454321 is  9

Tree Traversal In-order, Pre-order and Post-order:Recursive

public class TreeNode
    {
        public int Data { get; set; }
        public TreeNode Left { get; set; }
        public TreeNode Right { get; set; }
    }

In-Order

public void Inorder(TreeNode node)
        {
            if (node == null)
            {
                return;
            }
            Inorder(node.Left);
            Console.Write(node.Data + ” >”);
            Inorder(node.Right);
        }

Pre-Order

public void Preorder(TreeNode node)
        {
            if (node == null)
            {
                return;
            }
            Console.Write(node.Data + ” >”);
            Preorder(node.Left);
            Preorder(node.Right);
        }

Post-Order

public void Postorder(TreeNode node)
   {
       if (node == null)
       {
           return;
       }

       Postorder(node.Left);
       Postorder(node.Right);
       Console.Write(node.Data + ” >”);
   }

Write a Program for Swapping Every Second Element of LinkList

Input : 0->1->2->3->4->5->6->7->8->9->

output: 1->0->3->2->5->4->7->6->9->8->

Code :

        public void SwapEverySecondElement()
        {
            var p = head;
            var q = p.Next;

            while (q != null)
            {
                var temp = p.Data;
                p.Data = q.Data;
                q.Data = temp;
                p = q.Next;
                if (p != null)
                {
                    q = p.Next;

                }
                else
                {
                    q = null;
                }

            }

        }

Test

Input

0->1->2->3->4->5->6->7->8->9->

Output

1->0->3->2->5->4->7->6->9->8->

——————————————————————————–

Input

A->B->C->D->E->F->G->H->I->J->K->L->M->N->O->P->Q->R->S->T->U->V->W->X->Y->Z->

Output

B->A->D->C->F->E->H->G->J->I->L->K->N->M->P->O->R->Q->T->S->V->U->X->W->Z->Y->

Write A Program To Print Matrix In Diagonal Order

Today we will write a program to print a given matrix in diagonal order.

 

        public void DiagonalOrder(int[,] input)
        {
            int rowcount = input.GetLength(0);
            int columnCount = input.GetLength(1);
            int rowIndex = -1;
            int columnIndex = 0;
            int startRowIndex = rowIndex;
            int startColumnIndex = columnIndex;
            while (true)
            {
                if (rowIndex < rowcount - 1)
                {
                    rowIndex++;
                }
                else if (rowIndex == rowcount - 1 && columnIndex < columnCount - 1)
                {
                    columnIndex++;
                }
                else if (rowIndex == rowcount - 1 && columnIndex == columnCount - 1)
                {
                    break;
                }
                startRowIndex = rowIndex;
                startColumnIndex = columnIndex;
                while (startRowIndex < rowcount && startRowIndex >= 0
                        && startColumnIndex < columnCount && startColumnIndex >= 0)
                {
                    Console.Write(input[startRowIndex, startColumnIndex] + " , ");
                    startRowIndex = startRowIndex - 1;
                    startColumnIndex = startColumnIndex + 1;

                }
                Console.WriteLine("");
            }
        }
 
input

1 2 3 4 5 6 7 8 9 10
11 12 13 14 15 16 17 18 19 20
21 22 23 24 25 26 27 28 29 30
31 32 33 34 35 36 37 38 39 40
41 42 43 44 45 46 47 48 49 50

output

1 ,
11 , 2 ,
21 , 12 , 3 ,
31 , 22 , 13 , 4 ,
41 , 32 , 23 , 14 , 5 ,
42 , 33 , 24 , 15 , 6 ,
43 , 34 , 25 , 16 , 7 ,
44 , 35 , 26 , 17 , 8 ,
45 , 36 , 27 , 18 , 9 ,
46 , 37 , 28 , 19 , 10 ,
47 , 38 , 29 , 20 ,
48 , 39 , 30 ,
49 , 40 ,
50 ,

————————————————————————————————————————

input

11 12 13 14 15
21 22 23 24 25
31 32 33 34 35
41 42 43 44 45
51 52 53 54 55
16 17 18 19 20
26 27 28 29 30
36 37 38 39 40
46 47 48 49 50

Output

11 ,
21 , 12 ,
31 , 22 , 13 ,
41 , 32 , 23 , 14 ,
51 , 42 , 33 , 24 , 15 ,
16 , 52 , 43 , 34 , 25 ,
26 , 17 , 53 , 44 , 35 ,
36 , 27 , 18 , 54 , 45 ,
46 , 37 , 28 , 19 , 55 ,
47 , 38 , 29 , 20 ,
48 , 39 , 30 ,
49 , 40 ,
50 ,

Reverse a Given Number C#

 

        public static long ReverseNumber(this long number)
        {
            long reversedNumber = 0;
            bool isNegative = false;
 
            if (number < 0)
            {
                isNegative = true;
            }
            if (number < 10 && number >= 0)
            {
                reversedNumber = number;
                return reversedNumber;
            }
 
            number = Math.Abs(number);
 
            while (number > 0)
            {
                reversedNumber = _base * reversedNumber + number % _base;
                number = number / _base;
            }
 
            if (isNegative)
            {
                reversedNumber = reversedNumber * -1;
            }
            return reversedNumber;
        }

 

Test Case

Number 1234567890 is reversed to 987654321

Number -123456 is reversed to -654321

Number 9 is reversed to 9

Number 545 is reversed to 545

Number 123454321 is reversed to 123454321

Number -123454321 is reversed to -123454321

Write code to check a String is palindrome or not

A palindrome is a word, phrase, number, or other sequence of symbols or elements, whose meaning may be interpreted the same way in either forward or reverse direction.

public static bool IsPalindrome(this string input)
     {
         bool result = true;
         input = input.Trim();
         try
         {

             if (string.IsNullOrEmpty(input))
             {
                 return result = false;
             }

             var inputArray = input.ToCharArray();

             if (inputArray.Length == 1)
             {
                 return result = true;
             }

             int length = inputArray.Length – 1;

             for (int i = 0; i <= length / 2; i++)
             {
                 if (inputArray[i] != inputArray[length – i])
                 {
                     return result == false;
                 }
             }
         }
         catch (Exception)
         {

             result = false;
         }
         return result;

     }
Test Case

string s = “”;
Console.WriteLine(string.Format(“String {0} IsPalindrome {1}”, s, s.IsPalindrome()));
s = “AJAY”;
Console.WriteLine(string.Format(“String {0} IsPalindrome {1}”, s, s.IsPalindrome()));
s = “ABBA”;
Console.WriteLine(string.Format(“String {0} IsPalindrome {1}”, s, s.IsPalindrome()));
s = “ABBa”;
Console.WriteLine(string.Format(“String {0} IsPalindrome {1}”, s, s.IsPalindrome()));
s = “palindrome”;
Console.WriteLine(string.Format(“String {0} IsPalindrome {1}”, s, s.IsPalindrome()));

Output:

String  IsPalindrome False
String AJAY IsPalindrome False
String ABBA IsPalindrome True
String ABBa IsPalindrome False
String palindrome IsPalindrome False

Write a program to calculate power(x,n)

Below solution divides the problem into sub problems of size y/2 and call the sub problems recursively

public static UInt64 Power(UInt64 x, UInt64 n)
{
    if (n == 0)
        return 1;
    else if (n % 2 == 0)
        return Power(x, n / 2) * Power(x, n / 2);
    else
        return x * Power(x, n / 2) * Power(x, n / 2);

}

Test Case

UInt64 x = 0;
UInt64 n = 0;
x = 2; n = 2;
Console.WriteLine(string.Format(“{0}^{1} ={2}”, x, n, Power(x, n)));
x = 2; n = 3;
Console.WriteLine(string.Format(“{0}^{1} ={2}”, x, n, Power(x, n)));
x = 10; n = 2;
Console.WriteLine(string.Format(“{0}^{1} ={2}”, x, n, Power(x, n)));
x = 12; n = 18;
Console.WriteLine(string.Format(“{0}^{1} ={2}”, x, n, Power(x, n)));
Console.Read();

Output

2^2 =4
2^3 =8
10^2 =100
12^18 =8176589207175692288