Thread: partition question

  1. #1
    Banned
    Join Date
    Oct 2008
    Posts
    1,535

    partition question

    write a function called
    Code:
     int partition(int arr[], int size)
    which input an array of integers and decides whether
    it is possible to divide the array into two groups
    so the sums of both the groups will be equal.
    Code:
    for example arr = {1,2,2,3,5,6,1}
    its could be divided into {2,2,6}  and {1,1,3,5} both of their sums is 10
    so they are equal and the function needs to return 1.

    the function must be recursive without loops.
    you are allowed to use only one external recursive function

    there are 7! (groups in this example) that needs to be tested

    what is the systematic way of doing this
    Last edited by transgalactic2; 01-14-2009 at 04:18 PM.

  2. #2
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,337
    Also, in your example, {2,3,5} and {1,1,2,6} are also equal.

    I think the solution will come down to these things:

    1) If the total_sum of all #s is negative, it can't be done.
    2) Else, since the total_sum is positive, you must be able to find a subset of numbers that total to total_sum/2.
    Mainframe assembler programmer by trade. C coder when I can.

  3. #3
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    -1 -1 -1 -1 -1 -1

    the total sum is negative an we can split then into two equal sums

    so it contradicts it
    ??

  4. #4
    FOSS Enthusiast
    Join Date
    Jun 2008
    Posts
    64
    my first idea is to get the sum and divide it by 2. now we have the sum for both partitions.
    Then I'd traverse the array and check if I can substract elements until I get zero.

  5. #5
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,337
    You are correct. I meant ODD, not NEGATIVE. Duh me.

    And I meant EVEN, not POSITIVE. Duh me again.
    Mainframe assembler programmer by trade. C coder when I can.

  6. #6
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    Code:
    if i take {1,2,2,3,5,6,1}
    the sum is 20
    how to traverse a 1d array?

  7. #7
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    Quote Originally Posted by Dino View Post
    You are correct. I meant ODD, not NEGATIVE. Duh me.

    And I meant EVEN, not POSITIVE. Duh me again.
    wow its easy then its looks

    thanks

  8. #8
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by Dino View Post
    2) Else, since the total_sum is even, you must be able to find a subset of numbers that total to total_sum/2.
    You sure about that?

    Code:
    { 2, 4, 8, 16, 32 }
    No two subsets will have equal sums, and half of the total sum is 31, and there is no subset which sums to that, either.

    I'm not sure, but I think this is an NP-complete problem. That doesn't mean it can't be solved, just that it's slow, and there is no "trick" to doing it fast.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  9. #9
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    i am looking for an example to contradict this odd even technique
    its too simple

  10. #10
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Well, it's a pretty obvious proof that it works: if the total is n, and you've broken it up into two pieces with equal sums, then those sums must need be n/2. If you can find it (there's no guarantee that you can), then there you go.

    It seems to split up pretty straightforwardly into a recursive technique (either the current element is part of the set or it isn't, so two cases), although as mentioned it may not be speedy.

  11. #11
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,337
    Quote Originally Posted by brewbuck View Post
    You sure about that?

    Code:
    { 2, 4, 8, 16, 32 }
    No two subsets will have equal sums, and half of the total sum is 31, and there is no subset which sums to that, either.
    Well, then, that array can't be "partitioned". It passes the sum=even test, but it does not pass the "sum of some subset" == sum/2.

    (edit - when I said "must", I meant you must be able to find a sum of 1/2 the total in order for the array to be partitionable - not "must" in the sense that there must always be an even split)
    Last edited by Dino; 01-14-2009 at 05:29 PM.
    Mainframe assembler programmer by trade. C coder when I can.

  12. #12
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by Dino View Post
    Well, then, that array can't be "partitioned". It passes the sum=even test, but it does not pass the "sum of some subset" == sum/2.
    I just don't see the usefulness of this test, because finding a subset with sum == total/2 is no different from finding a subset which sums to ANY given number. It doesn't help simplify the problem.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  13. #13
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,337
    I think it super simplifies the problem. If the total is 20, then just find some subset that equals 10. If you can't, the array is not partitionable. If the total is -6, find some subset that equals -3.

    Dino (taking the lazy man's approach to making difficult problems simple)
    Mainframe assembler programmer by trade. C coder when I can.

  14. #14
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by brewbuck View Post
    I just don't see the usefulness of this test, because finding a subset with sum == total/2 is no different from finding a subset which sums to ANY given number. It doesn't help simplify the problem.
    Given that the problem is "find total/2", it seems like a pretty important step, though.

  15. #15
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    what is the systematic way of picking members ?

    there are many possibilities

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Reading a (possibly EXT2) partition with Windows
    By nickname_changed in forum Linux Programming
    Replies: 5
    Last Post: 08-20-2003, 07:43 PM
  2. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 09:47 PM
  3. opengl DC question
    By SAMSAM in forum Game Programming
    Replies: 6
    Last Post: 02-26-2003, 09:22 PM
  4. A change in LILO, and partition expanding
    By civix in forum Tech Board
    Replies: 2
    Last Post: 01-08-2003, 03:01 PM
  5. hardware interaction in c
    By vineetwadwekar in forum C Programming
    Replies: 6
    Last Post: 03-29-2002, 09:01 AM