Make Array Zero By Subtracting Equal Amounts In JavaIntroduction:Given an integer array, we want to make all the elements in the array equal, and we have to provide the output with the minimum number of steps to reduce the array elements to zero. Approach 1:Using brute force approachJava Code: Output: Original Array : [1, 5, 0, 3, 5]
Modified Array : [0, 0, 0, 0, 0]
Number of Operations : 3
Explanation: - The MakeArrayZeroBruteForce class contains the main and helper methods: makeArrayZero and allElementsZero.
- The main method initializes an array based on the provided input and prints its original state. Then, it calls the makeArrayZero method, which uses the brute force to make all elements zero. Afterward, it prints the modified array and the number of operations are required.
- The makeArrayZero method repeatedly and subtracts and the minimum positive value from all positive elements until all elements becomes the It uses a while loop that continues as long as the not all elements are zero
- The inner for of loop finds the minimum positive value in the array.
- Another for loop subtracts the calculated minimum value from all positive elements.
- The allElementsZero method checks if all elements in the array are zero. It returns true if all elements are zero or otherwise false.
Time Complexity: - The outer while loop runs until all elements are zero, which can take up to max(nums) steps.
- The inner for loop finding the of minimum positive value runs in O(n) time.
- The second of for loop subtracts the minimum value from all positive elements in O(n) time.
- Overall, the worst-case time complexity of this brute force approach is O(n * max(nums)), where n is the number of elements in the array.
Space Comp lexity: - The space complexity is O(1), is as the of algorithm uses constant extra space regard less of the input size.
Dry Run of Code: Input: nums = [1, 5, 0, 3, 5]. Initial State: nums = [1, 5, 0, 3, 5] Operations: 0 Iteration 1: - Find the minimum positive value in the array: min = 1
- Subtract min from all positive elements:
- nums = [0, 4, 0, 2, 4]
Operations: 1 Iteration 2: - Find the minimum positive value in the array: min = 2
- Subtract min from all positive elements:
- nums = [0, 2, 0, 0, 2]
Operations: 2 Iteration 3: - Find the minimum positive value in the array: min = 2
- Subtract min from all positive elements:
- nums = [0, 0, 0, 0, 0]
Operations: 3 Final State: - nums = [0, 0, 0, 0, 0]
- Total Operations: 3
Approach 2:Using priority queueJava Code: Output: Minimum number of operations to make every element in nums equal to 0 is : 3
Explanation : - The minimumOperations method takes an array num as input and aims to make all elements in the and array equal to zero with the minimum number of operations. The code use a max heap (priority queue) to keep track of the array's largest elements.
- The code goes through the array in each or iteration and adds all non-zero elements to the max heap.
- After adding the elements, the largest element (max heap's top) is retrieved and subtracted from all non-zero elements in the array.
- The loop continues for until all elements in the array become zero.
- The ans variable keeps track of the number of operations needed to achieve this.
Time Complexity : - The while loop runs until all elements are zero, and in each iteration, the code processes all non-zero elements in the array and performs and operations on them.
- The maximum number of iterations is the maximum element in the array (max(nums)), which can be considered as O(n).
- Inside the while loop, the max heap operations (offer, poll, clear) each have a time complexity of O(log n), where n is the number of the elements in the max heap.
Space Complexity : - The code uses a max heap to store non-zero elements, so the space complexity is O(n) due to the max heap.
Dry run of the code : nums = [1, 5, 0, 3, 5]. Initial State: nums = [1, 5, 0, 3, 5] ans = 0 Iteration 1 : - Add non-zero elements to the max heap: maxHeap = [5, 3, 5, 1]
- Poll the largest element from max heap (5) and subtract it from all non-zero elements:
- nums = [1, 0, 0, 3, 0]
- Increment ans to 1
Iteration 2 : - Add non-zero elements to the max heap: maxHeap = [3, 1]
- Poll the largest element from max heap (3) and subtract it from all non-zero elements:
- nums = [1, 0, 0, 0, 0]
- Increment ans to 2
Iteration 3 : - Add non-zero of type elements to max heap: maxHeap = [1]
- Poll the largest element from max heap (1) and subtract it from all non-zero elements:
- nums = [0, 0, 0, 0, 0]
- Increment ans to 3
- The while loop condition is now met, and the iterations will stop.
Final State : - nums = [0, 0, 0, 0, 0]
- ans = 3
Approach 3:Using SortingJava Code : Output: minimum number of operations to make every element in nums equal to 0 is : 3
Explanation: - The Solution class contains the solution method minimumOperations and a helper method subtract.
- The subtract method takes an array of nums, an index, and a value as input It subtracts the value that is provided from the array's of the elements beginning at the specified index.The minimumOperations method aims to make all elements in the array equal to zero by subtracting equal amounts. It first sorts the input array in ascending order.
- The code then iterates through the sorted array. For each non-zero element encountered, it subtracts that element's value from itself and all subsequent elements. This operation represents subtracting the maximum value from all the elements.
- The count variable keeps track of the number of operations performed.
Time Complexity: - The sorting procedure requires O (n log n) time, where n is the number of array entries.
- The loop iterating through the sorted array runs in O(n) of the in the worst case.
- As a result, the overall time complexity is determined by sorting and is O (n log n).
Space Complexity: - The input array of and the helper variables define the space complexity, which results in O(n) space complexity.
Dry Run of code: nums = [1, 5, 0, 3, 5]. Initial State: - nums = [1, 5, 0, 3, 5]
- count = 0
Sorting Step: - After sorting: nums = [0, 1, 3, 5, 5]
Iteration 1: - Current element: nums[0] = 0 (already zero)
- No operation is needed, move to the next element
Iteration 2: - Current element: nums[1] = 1
- Subtract 1 from all elements to the right (index 2 to 4):
- nums = [0, 0, 2, 4, 4]
- Increment count to 1
Iteration 3: - Current element: nums[2] = 2
- Subtract 2 from all elements to the right (index 3 to 4):
- nums = [0, 0, 0, 2, 2]
- Increment count to 2
Iteration 4: - Current element: nums[3] = 2
- Subtract 2 from all elements to the right (index 4):
- nums = [0, 0, 0, 0, 0]
- Increment count to 3
Iteration 5: - All elements are zero, the loop terminates
Final State: - nums = [0, 0, 0, 0, 0]
- count = 3
Approach 4:Using Greedy AlgorithmJava Code: Output: Minimum number of operations to make every element in nums equal to 0 is : 3
Explanation: - The Solution class contains the solution method minimumOperations and a helper method subtract.
- The subtraction method inputs an array of nums, an index, and a value. It subtracts the given value from the array elements starting from the specified index.
- The minimumOperations function seeks to reduce all array elements to zero by subtracting equal amounts.. It first sorts the input array in ascending order.
- The code then iterates through the sorted array. For each non-zero element encountered, it subtracts that element's value from itself and all subsequent elements. This operation represents subtracting the maximum value from all the elements.
- The count variable keeps track of the number of operations performed.
Time Complexity: - The sorting operation takes O(n log n) time, where n is the number of elements in the array.
- The loop iterating through the sorted array runs in O(n) time.
- Inside the loop, the subtract method also processes the remaining elements in O(n) time in the worst case.
- Therefore, the sorting dominates the overall time complexity and is O(n log n).
Space Complexity: - The input array and the helper variables determine the space complexity, resulting in O(n) space complexity.
Approach 4 :Using HahSetJava Code: Output: Explanation: - The Solution class contains the solution method minimumOperations. The solution relies on the observation that the minimum number of operations required to make all elements equal to zero equals the number of distinct positive elements in the array.
- The code initializes a HashSet named set to keep track of distinct positive elements. The loop iterates through each element in the array. If the element is positive (n > 0), it is added to the set.
- After processing all elements, the size of the set is returned as the result. This size represents the minimum number of operations needed to equal all elements to zero.
Time Complexity: - The loop iterates through all elements in the array once.
- Adding an element to a hash set has an average time complexity of O(1), assuming a good hash function.
- The overall time complexity is O(n), where n is the number of elements in the array.
Space Complexity: - The space complexity is O(k), where k is the array's number of distinct positive elements.
- In the worst case, when all positive elements are distinct, the space complexity could be O(n).
|