Nikita and the Game

  • + 0 comments

    Please help me with my code, cant understand where it went wrong - https://www.hackerrank.com/contests/hourrank-7/challenges/array-splitting/submissions/code/5838224

    private static int solve(int[] arr, int start, int end, long totalSum) {

        // cant divide more when sum is odd
        // return when start and end point are same
        if ((totalSum % 2 != 0) || (start == end)) {
            return 0;
        }
    
        long reqSum = totalSum / 2;
        long tempSum = 0;
    
        for (int i = start; i <= end; i++) {
    
            tempSum += arr[i];
    
            if (tempSum == reqSum) {
                // solution++;
                if (i - start > end - i - 1) {
                    return 1 + solve(arr, start, i, reqSum);
                } else if (i - start < end - i - 1) {
                    return 1 + solve(arr, i + 1, end, reqSum);
                } else if (i - start == end - i - 1) {
                    return 1 + (Math.max(solve(arr, start, i, reqSum),
                            solve(arr, i + 1, end, reqSum)));
                }
                // break;
            }
        }
        return 0;
    }