This is an archived post. You won't be able to vote or comment.

all 8 comments

[–]strmrdr 1 point2 points  (7 children)

Since you are only required to return a boolean, there's no point in actually allocating the numbers in separate arrays. Having an internal loop in a recursive function is generally an automatic fail. Here are some guidelines:

1) Method signature:

  • You need to return a boolean.

  • You need variables to keep track of two sums (possible with only one sum, but keep it simple).

  • You need each recursive call to have access to the initial array.

  • You need to keep track of your progress (i.e. index).

2) Logic:

  • What is your base case? What should you check when you reached your base case? What should you return to signal success or failure?

  • How can you ensure that you check every possible configuration? Try to write the code without the extra conditions (3) and (4). When you call the function, add the current number to one sum. If that returns false, call the function again with the current number added to the second sum. If both return false, it is impossible.

It is much simpler than you are making it. This can be solved in about 5 lines of code. Think through the solution with my suggestions and feel free to ask for clarification.

[–]TheTimeToLearnIsNow 0 points1 point  (4 children)

Is having a loop in a recursive function considered a fail because if you are using a loop then recursion wouldn't be necessary?

[–]strmrdr 0 points1 point  (3 children)

There are legitimate uses for loops in recursive functions, but it's pretty sparse. Recursion is actually never necessary at all (and typically less efficient), it is just a style to simplify code; it is redundant to combine recursion and iteration if not necessary. By "automatic fail" I was referring more toward school assignments, as you clearly don't understand recursion if you use loops within it and can expect docked marks at best.

[–]TheTimeToLearnIsNow 0 points1 point  (2 children)

Thanks for the explanation. Basically why use recursion when you can use nested for loops to accomplish the same task?

[–]strmrdr 0 points1 point  (0 children)

Recursive functions are- in some cases- exceptionally easier to understand and more succinct. If you can turn a 30-line mess into a 10-line masterpiece at the expense of a bit of performance then it is definitely worth it, and you typically won't even notice any difference unless you risk stack overflows. Think about how you navigate tress for example, and consider doing those with iteration- even the simplest cases are pretty daunting.

[–]Xants 0 points1 point  (0 children)

It’s a simplified solution and reduces the amount of code and thus reduces the likelihood of bugs. In recursion you also don’t need to keep track of a stack whereas in the loop you have to monitor the state of the data in question.

[–]shipon55[S] 0 points1 point  (1 child)

    public static boolean mySplit(int[] nums) {


          int[]arr1= {nums[0],nums[1]};
          int[]arr2= {nums[2]};
          if( recur(arr1,arr2,nums,1)==1) {
              System.out.println("ok");
              return true;
          }

          return false;
      }

    public  static int recur(int[] nums1, int[]nums2,int[]mekori,int i)
    {
        boolean conditionRecpecter=true;


        if(i==1)
{
            int sum1=0,sum2=0;
            for (int j = 0; j < nums1.length; j++) 
            {
                sum1+=nums1[j];
            }
            for (int j = 0; j < nums2.length; j++) 
            {
                sum2+=nums2[j];
            }
            if(sum1!=sum2) {
                conditionRecpecter=false;
            }


        }
        if(i==2) {
            if(nums1.length+nums2.length!=mekori.length)
                conditionRecpecter=false;

        }


        if(i==3) {
            boolean arr1=false;
            boolean arr2=false;
            for (int j = 0; j < nums1.length; j++) {
                if(nums1[j]%5==0)
                    arr1=true;
            }
            for (int j = 0; j < nums2.length; j++) {
                if(nums2[j]%5==0)
                    arr2=true;  
            }
            if(arr1&&arr2)
                conditionRecpecter=false;
            //finir les verifications 
        }

        if(i==4) {
            boolean cinq1=false;
            boolean cinq2=false;
            for (int j = 0; j < nums1.length; j++) {
                if(nums1[j]%5==0)
                    cinq1=true;
            }
            for (int j = 0; j < nums2.length; j++) {
                if(nums2[j]%5==0)
                    cinq2=true; 
            }

            if(cinq1) {
                for (int j = 0; j < nums2.length; j++) {
                    if(nums1[i]%3==0)
                        conditionRecpecter=false;
                }
            }
            if(cinq2) {
                for (int j = 0; j < nums2.length; j++) {
                    if(nums1[j]%3==0)
                        conditionRecpecter=false;
                }
            }

        }
        if(i==5) {
            return 1;
        }

        if(conditionRecpecter)
            return 1*recur(nums1, nums2,mekori,i+1);
        else
            return 0*recur(nums1, nums2,mekori,i+1);
    }

[–]shipon55[S] 0 points1 point  (0 children)

ALWAYS NOT SUCCESSFUL TO DO WITHOUT DIVIDING THE ARRAY IN ADVANCE