"No one is harder on a talented person than the person themselves" - Linda Wilkinson ; "Trust your guts and don't follow the herd" ; "Validate direction not destination" ;

December 19, 2009

Algorithms - Maximum of sub sequence numbers

Question - Find max subsequence of 3 numbers in an array of 10 numbers

A little console app
==============
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication2
{
class Program
{
     static void Main(string[] args)
    {
          Console.WriteLine("Test Output");
          int[] a = { 21, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
          int maxsum=0,currentsum = 0;
          //max subsequence of 3 numbers
         for (int i = 0; i < 10; i++) //N times
        {
            currentsum = 0;
            for (int j = i; j < i+3 && j <10 ; j++) //3N
             {
                    currentsum = currentsum + a[j];
             }
             if (currentsum > maxsum)
             maxsum = currentsum;
        }
       Console.WriteLine("Max Sum Value is ", maxsum);
    }
}
}

It is O(N Square)

Next problem Set
Write a program which prints array of numbers 1 - 100 as multiples of 3 with A's i.e.(3A,6A,9A.....) , multiples of 5 with B(5B, 10B, 20B....) and multiples of 3 and 5 with AB(15AB, 150AB.....)

TSQL Code Here - SQL 2008 R2
use TEMPDB

DECLARE @I INT
DECLARE @RESULT VARCHAR(5)
SET @I = 1
SET @RESULT = NULL
WHILE 1 = 1
BEGIN
              SET @RESULT =
                                   CASE
                                   WHEN (@I%5 = 0 AND @I%3 = 0) THEN (CONVERT(VARCHAR(10),@I)+'AB')
                                    WHEN @I%3 = 0 THEN (CONVERT(VARCHAR(5),@I)+'A')
                                    WHEN @I%5 = 0 THEN (CONVERT(VARCHAR(5),@I)+'B')
                                    END
               IF @RESULT IS NOT NULL
                     SELECT @RESULT
               SET @I = @I+1
               SET @RESULT = NULL
               IF @I > 100
               BREAK;
END

Write a program to shuffle pack of cards without using math.random....

Given an array containing zeros,+ve and -ve numbers, write a function which pushes all the zeros to the end of the array.

Write a function which takes a string and a substring as input and which deletes the substring in the main string.eg: mainstr=abcxyghixy sub=xy result should be mainstr=abcghi

Testing atoi function

Prime number in TSQL from 1 to 100

The prime number challenge – great waste of time!

Many different solutions here in link

No comments: