Sunday, February 10, 2013

Three Partition SubSet Sum Problem




Hey Guys I was checking out a book on algorithms I came across this problem (Author S.Dasgupta et al).

Here is the Question :


Sorry for the pic above didn't wanna paste text as the summation symbol was coming as crap.
So the program below doesn't use dynamic programming. But it gets the job done :) it is not neat and needs a lot of refinement too :(. But u get the core idea here.

Filename PartionThree.java

/*---------------------------------------BEGIN----------------------------------------------------*/
public class PartitionThree {
    public static void main(String args[]){
        //int[] arr = { 1, 2, 3, 4, 4, 5, 8};
        //int[] arr = {3,3,2,4,5,1};
        int[] arr = {2,2,3,5};
        int sum = arrSum(arr,arr.length);
        int[] arr1 = new int[arr.length];
        int[] arr2 = new int[arr.length];
        int[] arr3 = new int[arr.length];
        boolean ret = findPartition(sum/3, arr.length, arr, arr1,0, arr2,0, arr3,0);
        if(ret == false)
            System.out.println("Not Found Finally");
    }

    static boolean findPartition(
            int sum,
            int n,
            int[] arr,
            int[] arr1,int n1,
            int[] arr2,int n2,
            int[] arr3,int n3) {
        if(found == true) {
            return false;
        }
        int s1 = arrSum(arr1,n1);
        int s2 = arrSum(arr2,n2);
        int s3 = arrSum(arr3,n3);
        if(n<0) {
            return false;
        } else if((s1>sum) || (s2>sum) ||(s3>sum)){
            return false;           
        } else if((sum == s1)&& (sum == s2)&&(sum == s3)) {
            if(n==0 && arr1.length > 0 && arr2.length > 0 && arr3.length > 0) {
                System.out.print("Found ");
                printArr(arr1,n1);printArr(arr2,n2);printArr(arr3,n3);
                found = true;
                return true;       
            } else {
                return false;
            }           
        } else if(n==0) {
            return false;
        } else {
            arr1[n1] = arr[n-1];
            boolean ret1 = findPartition(sum,n-1,arr,arr1,n1+1,arr2,n2,arr3,n3);
           
            arr2[n2] = arr[n-1];
            boolean ret2 = findPartition(sum,n-1,arr,arr1,n1,arr2,n2+1,arr3,n3);
           
            arr3[n3] = arr[n-1];
            boolean ret3 = findPartition(sum,n-1,arr,arr1,n1,arr2,n2,arr3,n3+1);
           
            return (ret1||ret2||ret3);
        }       
    }
   
    static int arrSum(int arr[],int n) {
        int sum =0;
        for(int i=0;i < n;i++) {
            sum += arr[i];
        }
        return sum;
    }
    static void printArr(int[] arr,int n) {
        System.out.print("[");
        for(int i=0;i < n;i++) {
            System.out.print(arr[i] + " ");
        }
        System.out.print("] ");
    }
    static boolean found = false;
}
/*---------------------------------------END----------------------------------------------------*/

I will solve this using dynamic programming soon. Also please note that we can extend the above algorithm to handle n-partition. It is a highly inefficient algorithm turns out to be a (No. of partition) ^ N complexity.

Would love to here from you.

I miss hanging out in sankey tank.


















Friday, February 8, 2013

Algo from the web and books

What the fuck comes next ?

Short introduction here ... If you are here then I can guess 2 things about you 
    1. You want to learn a few common algo questions for tech interviews 
    2. You don't want to open the books(Cormen et al)

Word of advice when people say dude "Fuckers asked me questions from advanced data structures" ... Don't go and google Data Structures books ... U need algo .. 


Firstly Algos have many parts I would classify them as
1. Lists , Stacks , Queues ,Sort and Search
2. Trees
3. Graphs
4. Dynamic Programming 
5. Really fucked up problems like Travelling Salesman :)


So the following are sites i checked out during my enlightening journey in the world of fundoo algos. I am an Indian from Bangalore , so I don't think I have a problem with any accent whether it is Uncle Sam speaking or Swamiji from India. This is important when I share videos lectures from YouTube.



Lists , Stacks , Queues ,Sort and Search :

http://ftp.tudelft.nl/TUDelft/oilie/Birmingham/Introduction%20to%20Algorithms.pdf

Well , I recommend Cormen for learning about sorting and searching principles. Please go through QuickSort (Randomized one too), HeapSort , Divide and Conquer .

Remember this book doesn't pick any particular language, so I hope you can write code once you know the concept.  You can give the complicated math a miss.

Very Very Important to know all the fundamentals well as the above concepts form the building blocks for the tough problems ahead.


http://www.careercup.com/page?pid=amazon-interview-questions


The above site is simply the most awesome set of questions , a lot of cool code has been written here . I recommend that u stick to questions asked in 2012 only :).



Trees : 


One of the best sites for Trees is GeeksForGeeks . Mostly if you are a fresher going for interviews for paychecks >1 mil rupees, you have to be absolutely through with all questions here . I counted like 3 pages with roughly 16 questions per page . That is like 48 ? .. well some are dumb and some are repeats . So please go on and learn the logic for all of them.


Another thing I have noticed is that not many companies ask you questions about these 

1. Red-Black Trees 
2. AVL Trees 
3. Binomial Trees 

I think it is better to go through it once , but know only the core logic.



Graphs : 


Well this may not be needed for freshers.

TODO :)


Dynamic Programming :

The thing about DP is that there is a concept and then many applications. It is a kinda chapter where the concept cannot be applied easily, so we need to work through all the fucking examples. Please search for the coin change problem :) , famous simple and can be easily applied. 

https://www.youtube.com/watch?v=CB425OsE4Fo

Watch this video for 30 min only till he comes up with the recursive function . Indian teacher and indian Student. Quite nice.


Phd level Interview questions :


Well after all this there are a set of problems which are asked JUST TO CHECK THE APPROACH (what a truck load of crap) . These will not have a straight forward answer. 

https://www.youtube.com/watch?v=e0tGC6ZQdQE

This video is absolutely wonderful , I love the way he explains it breaking it down. You will need prior knowledge of graphs for this.

Other Concepts you should know:

1. String Matching / Pattern searching etc.

There are a few methods that u can achieve this 

Naive

Rabin-Karp

Automata -

    This video gives a little bit of intro into what it automata.
     http://www.youtube.com/watch?v=GwsU2LPs85U

     These are a few practice questions u might want to look at to reinforce the concepts
     http://www.cs.cmu.edu/~tom7/211/fsm1-answers.html 

Knuth Morris Pratt

U will definitely be asked 1 question about pattern search :) . So be prepared. Please read this chapter from Cormen :).


Cool Blogs  :

This dude claims his algo is super efficient please check it out.


Ok Boys , That is it from me
Best of Luck with Algo !
.
.