Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Check whether it is possible to make both arrays equal by modifying a single element in Python
Suppose we have two arrays nums1 and nums2 and another value k. We have to check whether both arrays can be made equal by modifying any one element from nums1 in the following way (only once): We can add any value from the range [-k, k] to any element of nums1.
So, if the input is like nums1 = [5,7,11], nums2 = [5,5,11], k = 8, then the output will be True as we can add -2 (in range [-8,8]) with nums1[1] to make it 5 then it will be same as nums2.
Algorithm
To solve this, we will follow these steps ?
- Sort both arrays
nums1andnums2 - Initialize
temp := Falseandidx := -1 - For each index i from 0 to length of
nums1? 1, do:- If
nums1[i]is not same asnums2[i], then:- If
tempis true, return False (more than one difference) - Set
temp := Trueandidx := i
- If
- If
- If
idxis -1 (no differences) or|nums1[idx] - nums2[idx]| ? k, return True - Otherwise, return False
Implementation
Let us see the following implementation to get better understanding ?
def solve(nums1, nums2, k):
nums1.sort()
nums2.sort()
temp = False
idx = -1
for i in range(len(nums1)):
if nums1[i] != nums2[i]:
if temp:
return False
temp = True
idx = i
if idx == -1 or abs(nums1[idx] - nums2[idx]) <= k:
return True
return False
# Test the function
nums1 = [5, 7, 11]
nums2 = [5, 5, 11]
k = 8
print(solve(nums1, nums2, k))
The output of the above code is ?
True
Example Walkthrough
Let's trace through the example step by step:
def solve_with_debug(nums1, nums2, k):
print(f"Original: nums1 = {nums1}, nums2 = {nums2}")
nums1.sort()
nums2.sort()
print(f"After sorting: nums1 = {nums1}, nums2 = {nums2}")
temp = False
idx = -1
for i in range(len(nums1)):
if nums1[i] != nums2[i]:
print(f"Difference found at index {i}: {nums1[i]} != {nums2[i]}")
if temp:
return False
temp = True
idx = i
difference = abs(nums1[idx] - nums2[idx]) if idx != -1 else 0
print(f"Required change: {difference}, allowed range: {k}")
return idx == -1 or difference <= k
# Test with example
nums1 = [5, 7, 11]
nums2 = [5, 5, 11]
k = 8
result = solve_with_debug(nums1, nums2, k)
print(f"Result: {result}")
Original: nums1 = [5, 7, 11], nums2 = [5, 5, 11] After sorting: nums1 = [5, 7, 11], nums2 = [5, 5, 11] Difference found at index 1: 7 != 5 Required change: 2, allowed range: 8 Result: True
Key Points
- Sorting ensures we compare elements in the same relative positions
- We can only modify one element from
nums1 - The modification range is
[-k, k], so the absolute difference must be ? k - If arrays are already equal, no modification is needed
Conclusion
This solution efficiently checks if two arrays can be made equal by modifying a single element within a given range. The key insight is sorting both arrays first and ensuring only one difference exists that can be resolved within the allowed range.
