Hello! I was solving this Leetcode problem yesterday.
I'll rewrite the problem statement once again for convenience
I would suggest you to solve (in your head or code) this before going further
Now, lets say I have $$$q$$$ Queries of Type:
- $$$1$$$ $$$L$$$ $$$R$$$ $$$k$$$ where-
- $$$L$$$ and $$$R$$$ represent the left and rite endpoints of a subarray in $$$nums$$$ inclusive.
- $$$k$$$ represents the number of operations that can be used to make the entire subarray equal.
For every query, we need to return $$$true$$$ or $$$false$$$ if we are able to make the entire subarray equal.
Here are my observations and approach:
Not completely positive of my approach so would like to hear any corrections, improvements or suggestions.
So far, we've only seen static/immutable arrays. Lets say I also have a query of Type 2, where
- $$$2$$$ $$$p$$$ :
nums[p] *= -1. (flipping the value at index $$$p$$$).
In this case, how do i device an efficient solution when considering queries of both types? Any Ideas?










Auto comment: topic has been updated by happywobbuffet (previous revision, new revision, compare).
Some observations:
Pretty cool observation!. I just overcomplicated it.
You can view this problem as an instance of Gaussian elimination over $$$GF(2)$$$ (in other words, XOR). First of all, change $$$1 \rightarrow 1$$$ and $$$-1 \rightarrow 0$$$. Now you have a binary string.
For example, let's say initially you have the string $$$abcd$$$. Denote an operation at the $$$i$$$-th index as $$$p_i$$$. Note that operating on an index twice does nothing. So in essence, we have the following equations if we want to make everything equal to $$$0$$$:
We can further write
Now we can eliminate to get
This means that, we need to take the prefix XOR, check that the XOR of the whole array is $$$0$$$, and then count the number of $$$1$$$-s in the prefix XOR array and check if it is $$$\leq k$$$.
This isn't the only possibility of course, you can also make everything equal to $$$1$$$. To handle that, you can just flip every bit and run the same procedure. So we'll not talk about this case, but remember to handle it (just make another array and run the procedure on both arrays and check if either one works).
So now you have an $$$O(n + q)$$$ solution to your first variant: You know how to count the number of $$$1$$$-s in the prefix XOR of a subarray $$$[L, R]$$$: maintain a prefix sum of how many $$$1$$$-s are there in the prefix XOR array, and if $$$\text{prefix_XOR}[L-1]$$$ is $$$1$$$, you have $$$R-L+1-\text{count}_1(L, R)$$$ number of $$$1$$$-s (you get the idea). Also remember to check the whole subarray XOR is $$$0$$$.
To solve your variant with type $$$2$$$ updates, I think you can see we need a Fenwick tree now.
very nice approach. took me a while to understand it!
I'm still very new to the realm of Fenwick/Segment Tree like Data Structures, could you help with some hints?
Well, a Fenwick tree can maintain and get prefix sums and prefix XORs across updates. That's all you require right? You can look up how to implement a Fenwick tree here.