private void button3_Click(object sender, EventArgs e)
        {
            int[]  input      = AlgorithmHelper.ConvertCommaSeparetedStringToInt(this.textBox16.Text);
            int    windowsize = Convert.ToInt32(this.textBox1.Text);
            string output     = AlgorithmHelper.ConvertIntArrayToCommaSeparatedString(MaxofSlidingWindowUsingHeap(input, windowsize));

            this.textBox11.Text = output;
        }
        private void button1_Click(object sender, EventArgs e)
        {
            int[] input        = AlgorithmHelper.ConvertCommaSeparetedStringToInt(this.textBox15.Text);
            int   firstnumber  = Convert.ToInt32(this.textBox15.Text);
            int   secondnumber = Convert.ToInt32(this.textBox1.Text);

            this.textBox12.Text = ReverseFibonacciSeriesByIteration(firstnumber, secondnumber);
        }
        private void button12_Click(object sender, EventArgs e)
        {
            int[] array1   = AlgorithmHelper.ConvertCommaSeparetedStringToInt(this.textBox10.Text);
            int[] array2   = AlgorithmHelper.ConvertCommaSeparetedStringToInt(this.textBox9.Text);
            int   k        = Convert.ToInt32(this.textBox11.Text);
            int   smallest = FindKthSmallestNaiveApproachWrapper(array1, array2, k);

            this.textBox7.Text = smallest.ToString();
        }
        private void button5_Click(object sender, EventArgs e)
        {
            int[] array = AlgorithmHelper.ConvertCommaSeparetedStringToInt(this.textBox2.Text);
            int   sum   = Convert.ToInt32(this.textBox4.Text);

            string output = FindPairsBySort(sum, array);

            this.textBox5.Text = output;
        }
        private void button13_Click(object sender, EventArgs e)
        {
            int[] array1   = AlgorithmHelper.ConvertCommaSeparetedStringToInt(this.textBox10.Text);
            int[] array2   = AlgorithmHelper.ConvertCommaSeparetedStringToInt(this.textBox9.Text);
            int   k        = Convert.ToInt32(this.textBox11.Text);
            int   smallest = FindKthSmallestUsingInvariant(array1, 0, array1.Length - 1, array2, 0, array2.Length - 1, k);

            this.textBox7.Text = smallest.ToString();
        }
        private void button4_Click(object sender, EventArgs e)
        {
            int[] input = AlgorithmHelper.ConvertCommaSeparetedStringToInt(this.textBox11.Text);

            MedianOfMedian med    = new MedianOfMedian(input);
            int            median = med.FindMiddleElement(input, 0, input.Length - 1);

            this.textBox15.Text = median.ToString();
        }
Esempio n. 7
0
        private void button4_Click(object sender, EventArgs e)
        {
            int[]  input      = AlgorithmHelper.ConvertCommaSeparetedStringToInt(this.textBox3.Text);
            int    m          = Convert.ToInt32(this.textBox2.Text);
            string output     = "";
            bool   ispossible = isSubsetSum(input, input.Length, m);

            this.textBox4.Text = ispossible == true ? "Yes" : "No";
            this.textBox1.Text = output;
        }
        private void button12_Click(object sender, EventArgs e)
        {
            int[] input = AlgorithmHelper.ConvertCommaSeparetedStringToInt(this.textBox13.Text);
            int   i     = Convert.ToInt32(this.textBox15.Text);
            //this sln is for ith..for length
            //i = i - 1;
            string output = SortHelper.FindtheLargest(input, i).ToString();

            this.textBox14.Text = output;
        }
        private void button8_Click(object sender, EventArgs e)
        {
            int[]            input = AlgorithmHelper.ConvertCommaSeparetedStringToInt(this.textBox5.Text);
            int              index = Convert.ToInt32(this.textBox8.Text);
            int              value = Convert.ToInt32(this.textBox9.Text);
            SegmentationTree tree  = new SegmentationTree(input);

            tree.UpdateValueAtindex(index, value);
            this.textBox5.Text = AlgorithmHelper.ConvertIntArrayToCommaSeparatedString(tree.InputArray);
        }
        private void button3_Click(object sender, EventArgs e)
        {
            //https://www.youtube.com/watch?v=_H50Ir-Tves
            //http://www.geeksforgeeks.org/median-of-two-sorted-arrays-of-different-sizes/


            int[] array1 = AlgorithmHelper.ConvertCommaSeparetedStringToInt(this.textBox5.Text);
            int[] array2 = AlgorithmHelper.ConvertCommaSeparetedStringToInt(this.textBox6.Text);

            this.textBox8.Text = FindingMedianofSortedArray(array1, array2).ToString();
        }
        private void button1_Click(object sender, EventArgs e)
        {
            //https://www.youtube.com/watch?v=cETfFsSTGJI

            int[] input = AlgorithmHelper.ConvertCommaSeparetedStringToInt(this.textBox15.Text);

            JumpItem result = MinimumNumberofJumpsDP(input);

            this.textBox12.Text = result.MinimumJump.ToString();
            this.textBox1.Text  = result.Path;
        }
        private void button4_Click(object sender, EventArgs e)
        {
            int[] weights  = AlgorithmHelper.ConvertCommaSeparetedStringToInt(this.textBox6.Text);
            int[] values   = AlgorithmHelper.ConvertCommaSeparetedStringToInt(this.textBox1.Text);
            int   capacity = Convert.ToInt32(this.textBox2.Text);

            var result = FindKnackSnapMaxValueWithItems(capacity, weights, values, 0);

            this.textBox3.Text = result.Max.ToString();
            this.textBox4.Text = result.Items;
        }
        private void button7_Click(object sender, EventArgs e)
        {
            //http://articles.leetcode.com/2011/01/find-k-th-smallest-element-in-union-of.html
            int[] array1 = AlgorithmHelper.ConvertCommaSeparetedStringToInt(this.textBox10.Text);
            int[] array2 = AlgorithmHelper.ConvertCommaSeparetedStringToInt(this.textBox9.Text);
            int   k      = Convert.ToInt32(this.textBox11.Text);
            //int smallest = kthSmallestMethod2(array1, array2, k);
            //update: 6/16/2015 see below method
            int smallest = FindKthSmallestUsingInvariant(array1, 0, array1.Length - 1, array2, 0, array2.Length - 1, k);

            this.textBox7.Text = smallest.ToString();
        }
        private void button1_Click(object sender, EventArgs e)
        {
            int[] array = AlgorithmHelper.ConvertCommaSeparetedStringToInt(this.textBox1.Text);

            KarthicBST <int> tree = new KarthicBST <int>();

            tree.Root = BuildTreeWithPreOrderArray(array, 0, 0, array.Length - 1).treenode;

            string output = tree.PreOrderTraversal(tree.Root);

            bool result = String.Equals(this.textBox1.Text, output.Substring(0, output.LastIndexOf(',')), StringComparison.OrdinalIgnoreCase);
        }
        private void button6_Click(object sender, EventArgs e)
        {
            int[] weights  = AlgorithmHelper.ConvertCommaSeparetedStringToInt(this.textBox6.Text);
            int[] values   = AlgorithmHelper.ConvertCommaSeparetedStringToInt(this.textBox1.Text);
            int   capacity = Convert.ToInt32(this.textBox2.Text);
            //using memoizing
            Dictionary <string, int> dictionary = new Dictionary <string, int>();

            int result = FindKnackSnapMaxValue(capacity, weights, values, 0, dictionary);

            this.textBox3.Text = result.ToString();
        }
        private void button6_Click(object sender, EventArgs e)
        {
            int[] input      = AlgorithmHelper.ConvertCommaSeparetedStringToInt(this.textBox5.Text);
            int   querystart = Convert.ToInt32(this.textBox7.Text);
            int   queryend   = Convert.ToInt32(this.textBox6.Text);

            SegmentationTree tree = new SegmentationTree(input);

            int sum = tree.QuerySumBetweenIndex(querystart, queryend);

            this.textBox2.Text = sum.ToString();
        }
        private void button3_Click(object sender, EventArgs e)
        {
            int[] array = AlgorithmHelper.ConvertCommaSeparetedStringToInt(this.textBox1.Text);

            KarthicBST <int> tree = new KarthicBST <int>();

            tree.Root = ConstructTreeWithMinAndMax(array, 0, Int32.MinValue, Int32.MaxValue, array[0]).treenode;

            string output = tree.PreOrderTraversal(tree.Root);

            bool result = String.Equals(this.textBox1.Text, output.Substring(0, output.LastIndexOf(',')), StringComparison.OrdinalIgnoreCase);
        }
        private void button13_Click(object sender, EventArgs e)
        {
            int[]         input    = AlgorithmHelper.ConvertCommaSeparetedStringToInt(this.textBox5.Text);
            StringBuilder sb       = new StringBuilder();
            int           maxvalue = FindSubsetsMaxValue(input, 0, sb);
            //this.textBox8.Text = maxvalue.ToString(); we need to do extra logic to do value and its easy..no time now

            //update: 4/21/2015  I did the extra logic to get the values
            Items result = FindSubsetsMaxValueWithItems(input, 0);

            this.textBox9.Text = result.Value.ToString();
            this.textBox8.Text = result.ItemNames;
        }
        private void button10_Click(object sender, EventArgs e)
        {
            // http://www.geeksforgeeks.org/find-a-repeating-and-a-missing-number/
            int[] input = AlgorithmHelper.ConvertCommaSeparetedStringToInt(this.textBox6.Text);
            int   n     = Convert.ToInt32(this.textBox4.Text);

            int x = 0;
            int y = 0;

            getTwoElements(input, n, out x, out y);
            this.textBox5.Text = x.ToString();
            this.textBox7.Text = y.ToString();
        }
        private void button11_Click(object sender, EventArgs e)
        {
            //Logic:
            //There are 2^32 possible intergers = 4 Billion integers
            //Given memory 1 GB = 1023 MB = 10 pow 3 mb = 10 pow 6 kb = 10 pow 9 bytes = 8 * 10 pow 9 = 8 billion bits
            //We are not going to store int32 value as you know each int needs 32 bits which willbe 32 * 4 billion bits
            //We can create 4 billion bits and represent each bit for each int  
            //Creating 4 billion bits will only take 512mb memory

            //Initialize all 4 billion bits to 0 (by default)
            //Read the file and for each number n..find the nth bit set it to 1..duplicates shouldn't be a problem
            // after done
            //read the bits and return the first bit that has 0

            //update:
            //Unfortunately we don't have datatype to store bits but we have byte which is of 8 bits
            //so consider 1 bit  as index 1 in byte 1  000000001
            //byte[0] will have  0 to 7
            //byte[1] will have  8 to 15
            //byte[2] will have  16 to 23
            //....untill byte[4billion/8] will have x---4billion
            //for simplity imagine the file has number 0 to 10

            //we should have 4 billion positive interger..C# has only 2 billion integers
            //+1 is bcoz index are 0 based .. number 8 will have 2 byte[0] and byte[1]
            long numberofints = (long)Int32.MaxValue + 1;
            //we are using bytes array not bit..so update the nth bits on the bytes array
            //4 billion number need 4 billion bits = 4 billion/8 bytes
            //we can get number bytes array length can be set like this
            //byte[] bitfield = new byte[(int)(numberofints / 8)];


            //But here we are doing this logic test with small set of numbers

            int size = Convert.ToInt32(this.textBox16.Text);
            int[] input = AlgorithmHelper.ConvertCommaSeparetedStringToInt(this.textBox11.Text);

            //this.textBox8.Text = maxvalue.ToString(); we need to do extra logic to do value and its easy..no time now
            List<int> missingnums = FindMissingNumberUsingBytesLogic(size, input);

             StringBuilder sb = new StringBuilder();
             foreach (int number in missingnums)
             {
                 sb.Append(number).Append(",");
             }


             this.textBox13.Text = sb.ToString();
          
        }
Esempio n. 21
0
        private void button7_Click(object sender, EventArgs e)
        {
            //http://www.geeksforgeeks.org/count-set-bits-in-an-integer/

            int[] array = AlgorithmHelper.ConvertCommaSeparetedStringToInt(this.textBox16.Text);

            int count = 0;

            foreach (int value in array)
            {
                count += CountNumberofBits(value);
            }
            this.textBox15.Text = count.ToString();
        }
Esempio n. 22
0
        private void button8_Click(object sender, EventArgs e)
        {
            int[] input = AlgorithmHelper.ConvertCommaSeparetedStringToInt(this.textBox4.Text);

            int[] output = SortHelper.MergeSort(input, true);

            StringBuilder sb = new StringBuilder();

            foreach (int i in output)
            {
                sb.Append(i).Append(',');
            }

            this.textBox3.Text = sb.ToString();
        }
        private void button6_Click(object sender, EventArgs e)
        {
            int[] levelarray = AlgorithmHelper.ConvertCommaSeparetedStringToInt(this.textBox7.Text);


            KarthicBinaryTree <int> tree = new KarthicBinaryTree <int>();

            tree.Root = BuildSpecialTreeFromLevelTraversalArray(levelarray);
            StringBuilder output = new StringBuilder();

            tree.LevelTraversal(tree.Root, output);


            bool result = String.Equals(this.textBox7.Text, output.ToString().Substring(0, output.ToString().LastIndexOf(',')), StringComparison.OrdinalIgnoreCase);
        }
        private void button13_Click(object sender, EventArgs e)
        {
            int[]         bigarray   = AlgorithmHelper.ConvertCommaSeparetedStringToInt(this.textBox5.Text);
            int[]         smallarray = AlgorithmHelper.ConvertCommaSeparetedStringToInt(this.textBox3.Text);
            Range         result     = GetShortestSubSequenceUsingMinHeap(bigarray, smallarray);
            StringBuilder sb         = new StringBuilder();

            if (result != null)
            {
                for (int i = result.StartIndex; i <= result.EndIndex; i++)
                {
                    sb.Append(bigarray[i]).Append(",");
                }
            }
            this.textBox4.Text = sb.ToString();
        }
        private void button5_Click(object sender, EventArgs e)
        {
            //source: http://www.geeksforgeeks.org/construct-a-special-tree-from-given-preorder-traversal/
            int[]  preorderarray = AlgorithmHelper.ConvertCommaSeparetedStringToInt(this.textBox5.Text);
            char[] leafarray     = AlgorithmHelper.ConvertCommaSeparetedStringToCharArray(this.textBox4.Text);

            CustomNode <int>        tree1 = new CustomNode <int>();
            KarthicBinaryTree <int> tree  = new KarthicBinaryTree <int>();

            tree.Root = BuildSpecialTreeFromPreorderArray(preorderarray, leafarray, 0).treenode;

            string output = tree.PreOrderTraversal(tree.Root);


            bool result = String.Equals(this.textBox5.Text, output.Substring(0, output.LastIndexOf(',')), StringComparison.OrdinalIgnoreCase);
        }
        private void button7_Click(object sender, EventArgs e)
        {
            int[] weights  = AlgorithmHelper.ConvertCommaSeparetedStringToInt(this.textBox6.Text);
            int[] values   = AlgorithmHelper.ConvertCommaSeparetedStringToInt(this.textBox1.Text);
            int   capacity = Convert.ToInt32(this.textBox2.Text);

            //int[] weights = new int[] { 3, 2, 1 };
            //int[] values = new int[] { 5, 3, 4 };
            //int capacity = 5;


            var result = FindKnackSnapMaxValueWithItemsUsingDP(weights, values, capacity);

            this.textBox3.Text = result.Max.ToString();
            this.textBox4.Text = result.Items;
        }
Esempio n. 27
0
        private void button6_Click(object sender, EventArgs e)
        {
            int[] input1 = AlgorithmHelper.ConvertCommaSeparetedStringToInt(this.textBox7.Text);
            int[] input2 = AlgorithmHelper.ConvertCommaSeparetedStringToInt(this.textBox5.Text);

            int[] input = new int[input1.Length + input2.Length];
            Array.Copy(input1, input, input1.Length);
            Array.Copy(input2, 0, input, input1.Length, input2.Length);
            Array.Sort(input);

            int[] noncommon = ArrayHelper.GetNonCommonItemsInSortedArray(input);

            string output = AlgorithmHelper.ConvertIntArrayToCommaSeparatedString(noncommon);

            this.textBox9.Text = output;
        }
        private void button10_Click_1(object sender, EventArgs e)
        {
            int size = Convert.ToInt32(this.textBox17.Text);

            int[] input = AlgorithmHelper.ConvertCommaSeparetedStringToInt(this.textBox19.Text);

            //this.textBox8.Text = maxvalue.ToString(); we need to do extra logic to do value and its easy..no time now

            List <int>    result = FindDuplicatesUsingBitLogic(input, size);
            StringBuilder sb     = new StringBuilder();

            foreach (int value in result)
            {
                sb.Append(value).Append(",");
            }
            this.textBox18.Text = sb.ToString();
        }
Esempio n. 29
0
        private void button8_Click(object sender, EventArgs e)
        {
            int[] input = AlgorithmHelper.ConvertCommaSeparetedStringToInt(this.textBox4.Text);
            int   size  = Convert.ToInt16(this.textBox5.Text);



            CustomCircularQueue <int> circularqueue = new CustomCircularQueue <int>(size);


            foreach (int i in input)
            {
                circularqueue.Enqueue(new CircularQueueItem <int>(i));
            }

            StringBuilder sb = new StringBuilder();

            //while (!circularqueue.IsEmpty)
            //{
            //  sb.Append(circularqueue.Dequeue().Data.ToString()).Append(",");
            //}


            //Test Cases:

            //The queue has 6,2,3,4,5

            //retrive the next 2..don't dequeue ...just set retrive
            circularqueue.Get(); //inside this method we set the retrive prop to true  //read the 2
            //read 3 so when we dequeue 2 won't be removed only 3
            var item1 = circularqueue.Dequeue();

            circularqueue.Get();                 //read 4
            var item2 = circularqueue.Dequeue(); // remove 5

            //dequeue two items...2 shouldn't be dequeued only 3 and 4



            //Now the buffer is full and 2 and 3 are read so
            //when we add new item it should overwrite 4 instead of 2
            //circularqueue.Enqueue(7);
            //circularqueue.Enqueue(8);

            this.textBox3.Text = sb.ToString();
        }
Esempio n. 30
0
        private void button9_Click(object sender, EventArgs e)
        {
            //print all subarray and find who values is o
            int[] input = AlgorithmHelper.ConvertCommaSeparetedStringToInt(this.textBox16.Text);

            List <String> result = new List <string>();

            List <SubsetValue> allsubsets = GetAllSubsetsCustom(input, 0, result);

            StringBuilder sb = new StringBuilder();

            foreach (string s in result)
            {
                sb.Append("(").Append(s).Append(")").Append(",");
            }
            this.textBox11.Text = sb.ToString();
        }