Minimum Deletions to Make Array Sharp Problem
Description
LeetCode Problem 4083.
You are given an integer array nums.
You may delete any number of elements from nums. The remaining elements keep their original order.
The resulting array a is sharp if the bitwise XOR of every pair of adjacent elements is non-zero. That is, a[i] XOR a[i + 1] != 0 for every index i such that 0 <= i < a.length - 1. An array with at most one element is sharp.
Return the minimum number of deletions needed to make nums sharp.
Bitwise XOR compares the corresponding bits of two integers. A bit in the result is 1 if the corresponding bits differ, and 0 otherwise.
Example 1:
Input: nums = [1,1,2,2,2,3]
Output: 3
Explanation:
- The pairs at indices (0, 1), (2, 3), and (3, 4) each have an XOR of 0.
- Deleting one 1 and two 2s leaves [1, 2, 3], where 1 XOR 2 == 3 and 2 XOR 3 == 1 are both non-zero. Therefore, the answer is 3.
Example 2:
Input: nums = [4,4,9,4]
Output: 1
Explanation:
- The pair at indices (0, 1) has an XOR of 0.
- Deleting either of these two 4s leaves [4, 9, 4], where 4 XOR 9 == 13 and 9 XOR 4 == 13 are both non-zero.
Constraints:
2 <= nums.length <= 1001 <= nums[i] <= 100
Solution
Approach: Count adjacent equal pairs
The XOR of two numbers is 0 exactly when they are equal, so the array is sharp when no two neighbours are equal.
Split the array into runs of equal values. A run of length k must shrink to a single element, which means k - 1 deletions, and that is also the number of adjacent equal pairs inside the run. Neighbouring runs hold different values, so keeping one element of each run leaves a sharp array. The answer is therefore the number of indices i with nums[i] == nums[i - 1].
Complexity: O(n) time and O(1) extra space.
class Solution:
def minDeletions(self, nums: List[int]) -> int:
return sum(x == y for x, y in zip(nums, nums[1:]))