Write A Program to Detect Loop in Singly LinkedList

Given a single linked list, we have to detect loop. For this we will use two pointers called slow and fast. We will increment slow by one and fast by two. once both these pointers meet, this is the node from were loop is present

        public void DetectLoop(Node<T> node)
        {
            Node<T> slow = node;
            Node<T> fast = node;

            while (slow != fast && fast.Next != null)
            {
                slow = slow.Next;
                fast = fast.Next.Next;
                if (slow == fast)
                {
                    Console.WriteLine("Loop is detected");
                    return;
                }
            }
            Console.WriteLine("There is no loop");

        }

Write a Program to Print Following Number Pattern

Input  : Number of Rows

Output:

1
121
12321
1234321
123454321

Code

 

        public static void PrintNumberPattern2(int rows)
        {
            int i, j, k;
            //Loop for number of Rows
            for (i = 1; i <= rows; i++)
            {
                // Loop for printing numbers in increasing order
                for (j = 1; j < 2 * i + 1; j++)
                {
                    Console.Write(j);
                    if (j >= i)
                    {
                        break;
                    }
                }
                // Loop for printing numbers in decreasing order
                for (k = j - 1; k > 0; k--)
                {
                    Console.Write(k);
                }
                Console.WriteLine("");
            }
        }

Write a Program for Rotate a Sqaure Matrix by 90 Degree

Today we have to write a program for rotating a given matrix by 90 degree. This code only works with square matrix.

Code

        public static void MatrixRotation<T>(T[,] matrix)
        {
            Console.WriteLine("\n Input Matrix");
            Print2DMatrix(matrix);
            int row = matrix.GetLength(0);
            int column = matrix.GetLength(1);

            T temp;
            for (int i = 0; i < row; i++)
            {
                for (int j = i + 1; j < column; j++)
                {
                    temp = matrix[j, i];
                    matrix[j, i] = matrix[i, j];
                    matrix[i, j] = temp;
                }
            }


            for (int i = 0; i < row / 2; i++)
            {
                for (int j = 0; j < column; j++)
                {
                    temp = matrix[j, i];
                    matrix[j, i] = matrix[j, row - i - 1];
                    matrix[j, row - i - 1] = temp;
                }
            }
            Console.WriteLine("\n Rotation matrix");
            Print2DMatrix(matrix);

        }

        private static void Print2DMatrix<T>(T[,] matrix)
        {
            Console.WriteLine("--------------\n");
            int row = matrix.GetLength(0);
            int column = matrix.GetLength(1);

            for (int i = 0; i < row; i++)
            {
                for (int j = 0; j < column; j++)
                {
                    Console.Write(matrix[i, j] + " ");
                }
                Console.WriteLine(" ");
            }

        }

Result


Input Matrix

————–

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

Rotation matrix

————–

51 41 31 21 11

52 42 32 22 12

53 43 33 23 13

54 44 34 24 14

55 45 35 25 15

Input Matrix

————–

A B C D

E F G H

I J K L

M N O P

Rotation matrix

————–

M I E A

N J F B

O K G C

P L H D

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;
        }

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 Printing the Stars

input : number of Rows

Output

         *
      *  *
   *  *  *
*  *  *  *
Code
        public static void PrintStar(int rows)
        {
        int temp=rows;
            for (int i = 1; i <= rows; i++)
            {
                for (int spacecounter = temp; spacecounter >= 1; spacecounter--)
                {
                    Console.Write("   ");
                }

                for (int starCounter = 1; starCounter <= i; starCounter++)
                {
                    Console.Write(" * ");
                }
                temp=temp-1;
                Console.WriteLine("");
            }
        }

Write a Program for Finding Duplicate Elements in Array

Given an array of n elements and elements are  from 1 to n-1. that is

clip_image002[6]

Code

        public static void PrintDuplicate(int[] input)
        {
            int length = input.Length;
            Console.WriteLine(string.Join(";", input));
            for (int i = 0; i < length; i++)
            {
                if (input[Math.Abs(input[i])] >= 0)
                {
                    input[Math.Abs(input[i])] = -input[Math.Abs(input[i])];

                }
                else
                {
                    Console.WriteLine("duplicate number {0} and it's index is {1} ",
Math.Abs(input[i]), i + " ; "); } } }

Today We will write a program for converting matrix’s row and columns to zero if any one element is zero.

Example

Input Output

2 2 2 2

2 0 2 3

2 2 2 2

2 0 2 2

0 0 0 0

2 0 2 2

Code

        public static void RowColumnZero(int[,] matrix)
        {
            Console.WriteLine("------------------------------------------------------");
            Console.WriteLine("Before");
            Print2DMatrix(matrix);
            int rowConter;
            int columnCounter;

            rowConter = columnCounter = 0;
            int[] rowArray = new int[matrix.GetLength(0)];
            int[] columnArray = new int[matrix.GetLength(1)];

            for (rowConter = 0; rowConter < rowArray.Length; rowConter++)
            {

                for (columnCounter = 0; columnCounter < columnArray.Length; 
columnCounter++) { if (matrix[rowConter, columnCounter] == 0) { rowArray[rowConter] = 1; columnArray[columnCounter] = 1; } } } for (rowConter = 0; rowConter < rowArray.Length; rowConter++) { for (columnCounter = 0; columnCounter < columnArray.Length;
columnCounter++) { if (rowArray[rowConter] == 1 || columnArray[columnCounter] == 1) { matrix[rowConter, columnCounter] = 0; } } } //Print output Console.WriteLine("After"); Print2DMatrix(matrix); Console.WriteLine("------------------------------------------------------");
        }
Program For Creating the Matrix 
        public static int[,] CreateTestMatrix(int row, int column)
        {
            int[,] matrix = new int[row, column];
            int i, j;
            Random rnd = new Random();
            for (i = 0; i < row; i++)
            {
                //create a test matrix
                for (j = 0; j < column; j++)
                {
                    matrix[i, j] = rnd.Next(0, row * column);
                }
            }
            return matrix;
        }

Program for Printing the Matrix

        private static void Print2DMatrix(int[,] matrix)
        {
            for (int rowConter = 0; rowConter < matrix.GetLength(0); rowConter++)
            {
                {
                    for (int columnCounter = 0; columnCounter < matrix.GetLength(1); 
columnCounter++) { Console.Write(matrix[rowConter, columnCounter] + " "); } Console.WriteLine(Environment.NewLine); } } }
Test
Input Output

6 0 9 11

7 3 10 5

10 5 11 3

0 0 0 0

7 0 10 5

10 0 11 3

7 8 5 3

8 8 11 19

15 19 0 19

7 7 9 11

14 8 16 6

7 8 0 3

8 8 0 19

0 0 0 0

7 7 0 11

14 8 0 6

6 0 3

5 5 3

6 8 6

0 0 0

5 0 3

6 0 6

5 11 6 8

9 5 7 5

13 7 1 7

0 12 9 12

0 11 6 8

0 5 7 5

0 7 1 7

0 0 0 0

2 6 3

4 5 3

4 3 7

2 6 3

4 5 3

4 3 7

1 1

3 1

1 1

3 1

Today We will write a program for converting matrix’s row and columns to zero if any one element is zero.

Example

Input Output

2 2 2 2

2 0 2 3

2 2 2 2

2 0 2 2

0 0 0 0

2 0 2 2

Code

        public static void RowColumnZero(int[,] matrix)
        {
            Console.WriteLine("------------------------------------------------------");
            Console.WriteLine("Before");
            Print2DMatrix(matrix);
            int rowConter;
            int columnCounter;

            rowConter = columnCounter = 0;
            int[] rowArray = new int[matrix.GetLength(0)];
            int[] columnArray = new int[matrix.GetLength(1)];

            for (rowConter = 0; rowConter < rowArray.Length; rowConter++)
            {

                for (columnCounter = 0; columnCounter < columnArray.Length; 
columnCounter++) { if (matrix[rowConter, columnCounter] == 0) { rowArray[rowConter] = 1; columnArray[columnCounter] = 1; } } } for (rowConter = 0; rowConter < rowArray.Length; rowConter++) { for (columnCounter = 0; columnCounter < columnArray.Length;
columnCounter++) { if (rowArray[rowConter] == 1 || columnArray[columnCounter] == 1) { matrix[rowConter, columnCounter] = 0; } } } //Print output Console.WriteLine("After"); Print2DMatrix(matrix); Console.WriteLine("------------------------------------------------------");
        }
Program For Creating the Matrix 
        public static int[,] CreateTestMatrix(int row, int column)
        {
            int[,] matrix = new int[row, column];
            int i, j;
            Random rnd = new Random();
            for (i = 0; i < row; i++)
            {
                //create a test matrix
                for (j = 0; j < column; j++)
                {
                    matrix[i, j] = rnd.Next(0, row * column);
                }
            }
            return matrix;
        }

Program for Printing the Matrix

        private static void Print2DMatrix(int[,] matrix)
        {
            for (int rowConter = 0; rowConter < matrix.GetLength(0); rowConter++)
            {
                {
                    for (int columnCounter = 0; columnCounter < matrix.GetLength(1); 
columnCounter++) { Console.Write(matrix[rowConter, columnCounter] + " "); } Console.WriteLine(Environment.NewLine); } } }
Test
Input Output

6 0 9 11

7 3 10 5

10 5 11 3

0 0 0 0

7 0 10 5

10 0 11 3

7 8 5 3

8 8 11 19

15 19 0 19

7 7 9 11

14 8 16 6

7 8 0 3

8 8 0 19

0 0 0 0

7 7 0 11

14 8 0 6

6 0 3

5 5 3

6 8 6

0 0 0

5 0 3

6 0 6

5 11 6 8

9 5 7 5

13 7 1 7

0 12 9 12

0 11 6 8

0 5 7 5

0 7 1 7

0 0 0 0

2 6 3

4 5 3

4 3 7

2 6 3

4 5 3

4 3 7

1 1

3 1

1 1

3 1

Write A Program to Find Elements occurring Odd Number of times.

Given an Array, find all the elements occurring odd number of time. All the elements are

clip_image002

That is all elements are positive integers.

Code

        public static void FindOddOccurrence(int[] input)
        {
            int length = input.Length;

            Console.Write("input Array is " + string.Join(",", input));
            Console.Write(Environment.NewLine);
            Console.WriteLine("Elements Occurring odd number of Times");
            for (int i = 0; i < length; i++)
            {
                input[Math.Abs(input[i])] = -input[Math.Abs(input[i])];
            }

            for (int i = 0; i < length; i++)
            {
                if (input[Math.Abs(input[i])] < 0)
                {
                    Console.Write(Math.Abs(input[i]) + ",");
                    input[Math.Abs(input[i])] = -input[Math.Abs(input[i])];
                }
            }
            Console.Write(Environment.NewLine);
        }

Test

Input Array is 1,2,3,1,3,1,6

Elements Occurring odd number of Times

1,2,6,

Input Array is 1,2,3,2,3,1,3

Elements Occurring odd number of Times

3,

Input Array is 1,2,4,6,7,8,3,2,3,1,3,4,1,3,2,2,2,2,2,4,8,3

Elements Occurring odd number of Times

1,2,4,6,7,3,

Input Array is 1,2,3,4,5,6,7,1,2,3,4,5,6,7

Elements Occurring odd number of Times