Given task: "Write function named 'mysplit' that gets an int array and calls a recursive method named 'MySplit'.The function MySplit needs to check if the current array can be divided into 2 groups which follow these conditions:
*The Sum of each group must be equal.
*The occurence of each number is only once in each group(this clause is meant to prevent the user from adding/duplicating numbers in order to get equality between the 2 groups=first clause).
*All the numbers that can be divided by 5 must be in the same group.
*All the numbers that can be divided by 3 (and not by 5) must be in the second group.
I have written this code, but reached a dead end.I fail to fulfil the first 2 clauses of the task and cant think of a way to insert it in my code.
This the code I have written:
public static boolean mySplit(int[] nums)
{
return MySplit(nums,0,0,0);
}
public static boolean MySplit(int[]arr,int result_5,int result_3,int pointer)//2 groups: 'result_5' & 'result_3' and pointer to iterate through an array
{
if(arr[pointer]%5==0)
{
return MySplit(arr,result_5+arr[pointer],result_3,pointer++);
}
else if(arr[pointer]%3==0)
{
return MySplit(arr,result_5,result_3+arr[pointer],pointer++);
}
}
*Edit: **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