Nikita and the Game

Sort by

recency

|

29 Discussions

|

  • + 0 comments

    include

    include

    include

    include

    using namespace std; int arraySplittingHelper(const vector& arr, int start, int end, const vector& prefixSum) { if (start >= end) return 0;

    long long total_sum = prefixSum[end + 1] - prefixSum[start];
    if (total_sum % 2 != 0) return 0;
    
    long long target_sum = total_sum / 2;
    for (int i = start; i < end; ++i) {
        long long left_sum = prefixSum[i + 1] - prefixSum[start];
        if (left_sum == target_sum) {
            // Recursively calculate splits for left and right partitions
            return 1 + max(
                arraySplittingHelper(arr, start, i, prefixSum),
                arraySplittingHelper(arr, i + 1, end, prefixSum)
            );
        }
    }
    
    return 0;
    

    }

    int arraySplitting(const vector& arr) { int n = arr.size(); // Create prefix sum array vector prefixSum(n + 1, 0); for (int i = 0; i < n; ++i) { prefixSum[i + 1] = prefixSum[i] + arr[i]; } return arraySplittingHelper(arr, 0, n - 1, prefixSum); } int main() { int t; cin >> t; vector results;

    while (t--) {
        int n;
        cin >> n;
        vector<int> arr(n);
        for (int i = 0; i < n; ++i) {
            cin >> arr[i];
        }
        results.push_back(arraySplitting(arr));
    }
    
    for (int result : results) {
        cout << result << endl;
    }
    
    return 0;
    

    }

  • + 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.