0

Write a function named mySplit that accepts an array of int, and calls a recursive reference function. Function MySplit should check whether the numbers in the array can be divided into 2 groups,

  1. The sum of the numbers in each group will be the same.

  2. Do not ignore or add numbers in the first array

  3. All numbers that are a multiple of 5 must be in the same group.

  4. All numbers that are duplicates of 3 (and not multiples of 5) must be in the second group.

I started writing the code, but I'm looking for some different ideas. Everything should be written in the recursive function and the boolean function should just return true or false

Examples:

mySplit ([1, 1]) → true

mySplit ([1, 1, 1]) → false

mySplit ([2, 4, 2]) → true

mySplit ([5, 21, 8, 15, 7]) → true

mySplit ([15, 10, 5]) → false

mySplit ([15, 8, 7]) → true

My code:

 public static boolean mySplit(int[] nums)
        {

            int []arr1=null;
            int []arr2 = null;  
            int index_1=0;int index_2=0;

            for (int i = 0; i < nums.length; i++) 
            {
                if(nums[i]%5==0)
                {
                    arr1[index_1]+=nums[i];
                    index_1++;
                }
                if(nums[i]%3==0 && (!(nums[i]%5==0)))
                {
                    arr2[index_2]=nums[i];
                    index_2++;
                }

            }

        }
        public static int myRecur(int[] nums,int[] nums1,int quelbdika)
        {

            if(quelbdika>4)
                return 0;
            boolean flag=true;

            if(quelbdika==1) 
            {
                int somm1=0,somm2=0;
                for (int i = 0; i < nums1.length; i++)
                {
                    somm2+=nums1[i];
                }
                for (int i = 0; i < nums.length; i++) 
                {
                    somm1+=nums[i];
                }
                if(somm1!=somm2)
                    flag=false;
            }

            if(flag)
                return 1+myRecur(nums,nums1,quelbdika+1);
            else {
                return 0+myRecur(nums,nums1,quelbdika+1);
            }
        }
    }
Jakob Sachs
  • 669
  • 4
  • 24
  • 2
    This seems to be a homework question, which in general is not a problem. However, try to be as specific as possible: what have you tried, what is not working right now, what is the current output. We can help you to solve problems, but we will not solve it for you. Good luck! – Ewoud May 29 '18 at 13:17
  • can not manage to share my Array in my recursion function to check my conditions – simon francis May 29 '18 at 13:19
  • 1
    what do you mean by share the array? – dave May 29 '18 at 13:19
  • I have to check if I can share my Array according to the 4 conditions for that it is necessary to test all the possibiler of creation of table – simon francis May 29 '18 at 13:26
  • With 'share' do you mean 'divide'? I'm afraid it's still not clear what you mean. – Ewoud May 29 '18 at 13:29
  • exactly divided – simon francis May 29 '18 at 13:30
  • 1
    [Questions asking for homework help must include a summary of the work you've done so far to solve the problem, and a description of the difficulty you are having solving it.](https://stackoverflow.com/help/on-topic) I'm especially missing the last part. Try to update your question. Meanwhile, I think that @patrick-parker 's answer gives you a good idea how to solve this problem. – Ewoud May 29 '18 at 13:45
  • See this [answer](https://stackoverflow.com/a/50679983/3992939) – c0der Jun 04 '18 at 11:58

1 Answers1

0

Here's a recursive way of looking at this function.

  • You start with two empty "groups" and a bag full of numbers.
  • At each step along the way, you draw a number out of the bag and try putting it in the first group, to see how that works out, then putting it in the second group, to see how that works out.
  • You do this until the bag is empty or you found a solution.
  • However, there is a little complication. That is, some of the numbers are "stuck together." So whenever you pull a number out of the bag, you must check to see if some of the other numbers are stuck together with it. If so, you will pull them out of the bag as well and do with them the same as you do with the first number.

Following the above approach should allow you to solve this problem recursively.

Patrick Parker
  • 4,863
  • 4
  • 19
  • 51
  • I think it would be simpler to divide the numbers into group `a` : can be divided by 5, group `b`: can be divided by 3, and group `c`: others. After that find permutation by adding elements from `c` to `a` or `b` – c0der Jun 04 '18 at 10:35
  • @c0der unfortunately the question was asked with poor English. When I read rules 3 and 4 it leaves open the possibility for the fives to be grouped together with the threes in the “second” group. – Patrick Parker Jun 05 '18 at 00:34