Similar Problems
Similar Problems not available
Equal Sum Arrays With Minimum Number Of Operations - Leetcode Solution
Companies:
LeetCode: Equal Sum Arrays With Minimum Number Of Operations Leetcode Solution
Difficulty: Medium
Topics: greedy hash-table array
Problem Statement: You are given two integer arrays nums1 and nums2 of length n.
You want to create arrays nums3 and nums4 such that:
- nums3[i] + nums4[i] == nums1[i] + nums2[i] for all 0 <= i < n (0-indexed).
- nums3 and nums1 are strictly increasing.
- nums4 and nums2 are strictly increasing.
You can assume that all the arrays are sorted in non-decreasing order.
Return the minimum number of operations to create the arrays nums3 and nums4. The answer is guaranteed to be unique.
An operation is defined as follows:
- Choose two indices 0 <= i1 < i2 < n.
- Replace nums3[i1] with nums4[i2].
- Replace nums4[i1] with nums3[i2].
Return the minimum number of operations to create the arrays nums3 and nums4.
Solution: To solve this problem, we need to first understand what is happening. We have two arrays, nums1 and nums2, and we need to find two other arrays nums3 and nums4 such that nums3[i] + nums4[i] == nums1[i] + nums2[i]. We also need that nums3 and nums1 are strictly increasing and nums4 and nums2 are strictly increasing. We can achieve this by swapping elements of nums3 and nums4.
To achieve this, we can start by initializing nums3 and nums4 as empty arrays. We can then loop through nums1 and nums2 and add the sum of their corresponding elements to a list. We can sort this list and then iterate through it, assigning each element to nums3 and nums4 alternatively until the end of the list is reached.
Now, we have nums3 and nums4 with the desired properties of being strictly increasing. However, they may not add up to the correct sum. To fix this, we can loop through nums3 and nums4 and check if nums3[i] + nums4[i] is not equal to nums1[i] + nums2[i]. If it is not equal, that means we need to swap elements of nums3 and nums4 to get the correct sum. We need to do this minimum possible times to get the minimum number of operations.
We can do this by looping through nums3 and nums4 and maintaining two pointers, i and j, pointing to their first element. We can check if nums3[i] + nums4[j] is equal to nums1[i] + nums2[i]. If it is not equal, we need to perform an operation. To minimize the number of operations, we can check if nums3[i+1] + nums4[j+1] - nums3[i] - nums4[j] is less than nums3[i+1] - nums3[i] + nums4[j+1] - nums4[j], then we should swap nums3[i] with nums4[j+1] and nums4[j] with nums3[i+1], otherwise we should simply swap nums3[i] with nums4[j] and nums4[j] with nums3[i+1]. We can then increment i and j appropriately and repeat the process until we reach the end of nums3 and nums4.
We can return the number of operations performed as the answer.
Time Complexity: The time complexity of this algorithm is O(nlogn) for sorting the array of sums and O(n) for performing the operations. Hence, the overall time complexity is O(nlogn).
Space Complexity: The space complexity of this algorithm is O(n) for storing the sum of nums1 and nums2 and the arrays nums3 and nums4.
Python Code:
class Solution:
def minOperations(self, nums1: List[int], nums2: List[int]) -> int:
n = len(nums1)
# If the maximum sum of nums1 and nums2 is less than the minimum sum of nums3 and nums4,
# it is impossible to find nums3 and nums4 with the required properties
if 6 * n < sum(nums1) + sum(nums2):
return -1
# Add the sum of corresponding elements of nums1 and nums2 to a list
sums = []
for i in range(n):
sums.append(nums1[i] + nums2[i])
# Sort the list of sums
sums.sort()
# Initialize nums3 and nums4 as empty arrays
nums3 = []
nums4 = []
# Assign elements of sorted sums alternatively to nums3 and nums4
for i in range(len(sums)):
if i % 2 == 0:
nums3.append(sums[i])
else:
nums4.append(sums[i])
# If nums4 is empty, we only need to check if nums3 is strictly increasing
if len(nums4) == 0:
for i in range(1, len(nums3)):
if nums3[i] <= nums3[i-1]:
return -1
return 0
# Perform the minimum number of operations to get the correct sums
i = 0
j = 0
operations = 0
while i < len(nums3) and j < len(nums4):
if nums3[i] + nums4[j] == sums[i+j]:
i += 1
j += 1
elif nums3[i] + nums4[j] < sums[i+j]:
diff = sums[i+j] - nums3[i] - nums4[j]
if i < len(nums3) - 1 and (j == len(nums4) - 1 or nums3[i+1] - nums3[i] > nums4[j+1] - nums4[j]):
i += 1
nums3[i], nums4[j] = nums4[j], nums3[i-1]
else:
j += 1
nums4[j], nums3[i] = nums3[i+1], nums4[j]
operations += 1
else:
diff = nums3[i] + nums4[j] - sums[i+j]
if j < len(nums4) - 1 and (i == len(nums3) - 1 or nums4[j+1] - nums4[j] > nums3[i+1] - nums3[i]):
j += 1
nums4[j], nums3[i] = nums3[i+1], nums4[j]
else:
i += 1
nums3[i], nums4[j] = nums4[j], nums3[i-1]
operations += 1
# Check if nums3 and nums4 add up to the correct sum
for i in range(len(nums3)):
if nums3[i] + nums4[i] != sums[i]:
return -1
return operations
Equal Sum Arrays With Minimum Number Of Operations Solution Code
1