• RSS
  • Facebook
  • Twitter

Knowledge is Power.

  • Who you are ?

    Working on machines without understanding them ? Then you should be here..

  • Where you are ?

    Geographical location should not become a barrier to Share our knowledge.

  • What do you do ?

    Puzzles and Interview question are intended to be discussed here.

    Monday, July 8, 2013


    Problem Statement
    You are given a set of positive integers. your task is to solve partition problem.

    Lets define the partition problem.

    Partition problem is the task of deciding whether a given multiset of positive integers can be partitioned into two subsets S1 and S2 such that the sum of the elements in S1 equals the sum of the elements in S2.

    Example 1:
    array[] = {1, 5, 11, 5}
    Output: Yes
    This array can be divided into to subsets {1, 5, 5} and {11} these two have equal sum.

    Example 2: 
    array[] = {1, 5, 3}
    Output: No
    This array cant be divided into two subsets of equal sum.

    ===============================================================================

    The level set was easy but this was really a nice question, took half hour to code it.. :P

    Well, Most of us get the answer just by reading the question... "Divide the array such that sum of each partition is equal", but as soon we we try to write the code, out mind keeps wondering "how the hell do I implement this ?"

    I thought for some time and then realized that, The worst case of forming the partitions for N element is; One partition of 0 elements and the other of N elements, but this wont help us.

    Now lets move forward in the same line. One partition of N-1 elements and the other of 1 element.This is our first step.

    This can be easily achieved with a for loop. But, the problem is, there are many different ways for selection 2 partitions each of N-1 and 1 elements respectively. To be precise they are N ways.

    First thing to be taken care of is to cover all the N ways. Next, every time the partition should be increase and decrease respectively keeping in mind all the cases.

    Here is the code,

    #include
    #include

    char* partition(int input1[])
    {
               int i=0,n,total=0,loop,round,s1=0;
               while(input1[i]>-1)
               {
                    if(input1[i]==0)return "Invalid";
                    total+=input1[i++];
               }
               n=i;
               round=0;
               while(round < N)
               {
                    loop=round;
                    for(i=0;i<=round-1;i++)
                    s1=s1+input1[i];
                    while(loop < round)
                   {
                       s1=s1+input1[loop];
                       if(s1==(total-s1))return "Yes";
                       s1=s1-input1[loop];
                       loop++;
                   }
                   s1=0;
                   round++;
                }
                return "No";
    }


    Let me know if you have any doubts.

    2 comments:

    1. This comment has been removed by the author.

      ReplyDelete
    2. Setting loop=round and then looping "while (loop < round)" won't work - perhaps you meant loop=0?
      I'm not sure your algorithm works, even apart from the error, a recursive solution is probably needed.

      If it does you can make some efficiancy improvements.
      Note that each part of the partition will have a value of total/2 so:
      1 Set desired=total/2.
      2 If this is not an integer, return No.
      3 Use this to compare against s1 (saves a subtraction on each inner iteration)
      4 You can also exit if the running total is > desired at any point.


      In terms of style and readability the second and third while loops should probably be for loops, and it would be nice to initialise s1 near the top of the loop, rather than at the end.

      ReplyDelete