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
Program to find dropped correct sensor value from the faulty list in Python
Suppose we have two lists nums1 and nums2 representing sensor metrics. Each list contains unique values where no two elements are equal. One list holds accurate sensor metrics while the other contains faulty data. In the faulty list, one value (not the last) was dropped and a wrong value was placed at the end. We need to find the actual value that was dropped.
For example, if nums1 = [5, 10, 15] and nums2 = [10, 15, 8], the output will be 5. The first list nums1 holds the actual values [5, 10, 15], while in the second array, 5 was dropped and 8 was inserted at the end.
Algorithm Steps
To solve this problem, we use binary search with the following steps ?
- Initialize
low = 0 - Initialize
high = len(nums1) - 1 - While
low , do:- Calculate
mid = (low + high) // 2 - If
nums1[mid] == nums2[mid], thenlow = mid + 1 - Otherwise,
high = mid
- Calculate
- Return
nums1[low]ifnums1[low + 1] == nums2[low], otherwise returnnums2[low]
Example Implementation
Let us see the following implementation to get better understanding ?
def solve(nums1, nums2):
low, high = 0, len(nums1) - 1
while low < high:
mid = (low + high) // 2
if nums1[mid] == nums2[mid]:
low = mid + 1
else:
high = mid
return nums1[low] if nums1[low + 1] == nums2[low] else nums2[low]
# Test with example data
nums1 = [5, 10, 15]
nums2 = [10, 15, 8]
result = solve(nums1, nums2)
print(f"Dropped value: {result}")
Dropped value: 5
How It Works
The algorithm uses binary search to efficiently find the point where the two arrays start to differ. When nums1[mid] == nums2[mid], it means the dropped element is in the right half, so we search there. When they differ, the dropped element is in the left half or at the current position.
Testing with Different Cases
def solve(nums1, nums2):
low, high = 0, len(nums1) - 1
while low < high:
mid = (low + high) // 2
if nums1[mid] == nums2[mid]:
low = mid + 1
else:
high = mid
return nums1[low] if nums1[low + 1] == nums2[low] else nums2[low]
# Test multiple cases
test_cases = [
([5, 10, 15], [10, 15, 8]),
([1, 2, 3, 4], [2, 3, 4, 9]),
([7, 14, 21], [14, 21, 5])
]
for i, (nums1, nums2) in enumerate(test_cases, 1):
result = solve(nums1, nums2)
print(f"Case {i}: nums1={nums1}, nums2={nums2}")
print(f"Dropped value: {result}")
print()
Case 1: nums1=[5, 10, 15], nums2=[10, 15, 8] Dropped value: 5 Case 2: nums1=[1, 2, 3, 4], nums2=[2, 3, 4, 9] Dropped value: 1 Case 3: nums1=[7, 14, 21], nums2=[14, 21, 5] Dropped value: 7
Conclusion
This binary search approach efficiently finds the dropped sensor value in O(log n) time complexity. The algorithm compares elements at the midpoint to determine which half contains the discrepancy, making it much faster than a linear search approach.
