Nikita and the Game

Sort by

recency

|

28 Discussions

|

  • + 0 comments

    Why consider only first valid partition?

    From the constraint that the numbers in the sequence are greater than or equal to 0 ( note that it has no negative numbers), we can see that the only way a subarray can be partitioned such that it has more than one valid 'X' is by including some zeros as suffix of first half of the partition or prefix of second half of the partition.

    eg-{1,4,3,0,0,0,0,2,3,3}

    X can be anything from 3 to 6. So in effect you only need to consider only 3,any other partition would also yield the same result in effect.

    I think some people got confused because they did not check the constraints and thought the numbers could be negative as well.

    Also the order of the numbers do not change. Some people are trying to solve a completely different problem by reordering the numbers.

  • + 0 comments

    Why does the editorial use dp when the problem does not have any overlapping substructure. My non-dp solution passed easily.

  • + 0 comments

    The editorial says " It should be noted that maximum points for a subarray can be calculated by considering only the first valid 'X' for a subarray (rather than by considering all valid 'X')"

    how can you prove it?

  • + 0 comments

    Editorial code is wrong, if you try this input:

    3
    8
    1 1 2 2 3 3 6 6
    8
    6 6 1 1 3 3 2 2
    8
    1 6 6 1 2 2 3 3
    

    All of sets are the same(items in different order), and you will get 3, 2, 0, output! The code doesn't check all possible partitions:

    (6, 6) or (3, 3, 2, 2, 1, 1) vs
    (6, 3, 3) or (6, 2, 2, 1, 1) vs
    (6, 3, 2, 1) or (6, 3, 2, 1) vs
    

    Also it failse in this case:

    1
    5
    3 4 5 6 8 
    

    It outputs 0, instead of 1, (3,4,6),(5,8) The problem seems to be NP-Hard, so I don't think there is any optimal solution.

  • + 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;
    }