Archive for March, 2013

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

Write a Program to Print Following Number Pattern

Input : Number of Rows

Output:

    1
  1 2 1
1 2 3 2 1

Code

        public static void PrintNumberPattern3(int rows)
        {
            int i, j, k, l;
            //Loop for number of Rows
            for (i = 1; i <= rows; i++)
            {
                //Loop for printing space between numbers
                for (l = 0; l < rows - i; l++)
                {
                    Console.Write("  ");
                }
                // 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 to Implement Doubly Link List

A doubly-linked list is a linked data structure that consists of a set of sequentially linked records called nodes. Each node contains two fields, called links, that are references to the previous and to the next node in the sequence of nodes. The beginning and ending nodes’ previous and next links, respectively, point to some kind of terminator, typically a sentinel node or null, to facilitate traversal of the list. If there is only one sentinel node, then the list is circularly linked via the sentinel node. It can be conceptualized as two singly linked lists formed from the same data items, but in opposite sequential orders.

image_thumb3

Source : http://en.wikipedia.org/wiki/Doubly-linked_list

Code

    public class DoubleLinkList<T>
    {
        private DoubleNode<T> head;
        private DoubleNode<T> tail;

        public int InsertAtHead(DoubleNode<T> item)
        {

            if (head == null)
            {
                item.Next = item.Previous = null;
                head = item;
                tail = item;
                return 0;
            }

            head.Previous = item;
            item.Next = head;
            head = item;

            return 0;
        }
        
        public int InsertAtTail(DoubleNode<T> item)
        {
            if (head == null)
            {
                item.Next = item.Previous = null;
                head = item;
                tail = item;
                return 0;
            }

            tail.Next = item;
            item.Previous = tail;
            tail = item;
            return 0;
        }

        public void ShowFromHead()
        {
            var temp = head;
            while (temp != null)
            {
                Console.Write(temp.Data + "->");
                temp = temp.Next;
            }
            Console.WriteLine(Environment.NewLine);
        }

        public void ShowFromHead(DoubleNode<T> root)
        {
            var temp = root;
            while (temp != null)
            {
                Console.Write(temp.Data + "->");
                temp = temp.Next;
            }
            Console.WriteLine(Environment.NewLine);
        }

        public void ShowFromTail()
        {
            var temp = tail;
            while (temp != null)
            {
                Console.Write(temp.Data + "->");
                temp = temp.Previous;
            }
            Console.WriteLine(Environment.NewLine);
        }
    }

    public class DoubleNode<T>
    {
        public DoubleNode<T> Next { get; set; }
        public DoubleNode<T> Previous { get; set; }
        public T Data { get; set; }

    }

Write a Program for Finding Second Largest Element in a unsorted Array

Finding largest or smallest element is easy as compare to finding the second largest element in an unsorted array. This problem can be solved using Sorting. Instead of sorting we will use a different approach for solving this problem.

        public static int FindSecondNdLarge(int[] array)
        {
            int size = array.Length;
            int[] max = new int[] { 0, 0 };
            int counter;

            if (array[0] > array[1])
            {
                max[0] = array[0];
                max[1] = array[0];
            }
            else
            {
                max[0] = array[1];
                max[1] = array[0];
            }

            for (counter = 2; counter < size; counter++)
            {
                if (array[counter] > max[1])
                {
                    if (array[counter] > max[0])
                    {
                        max[1] = max[0];
                        max[0] = array[counter];
                    }
                    else
                    {
                        max[1] = array[counter];
                    }
                }
            }

            return max[1];
        }