- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Program to find dropped correct sensor value from the faulty list in Python
Suppose we have two lists nums1 and nums2, they are representing sensor metrics. Each list contains unique values, so a ≠ b. One of these two lists are holding accurate sensor metrics but the other one contains faulty. In the faulty list one value, that is not the last value was dropped and a wrong value was placed to the end of that list. We have to find the actual value that was dropped.
So, if the input is like nums1 = [5, 10, 15] nums2 = [10, 15, 8], then the output will be 5, as first list nums1 holds the actual values = [5, 10, 15], in the second array, that is dropped and 8 is inserted at the end.
To solve this, we will follow these steps −
- low := 0
- high :=
- size of nums1 - 1
- while low < high, do
- mid := floor of (low + high) / 2
- if nums1[mid] is same as nums2[mid], then
- low := mid + 1
- otherwise,
- high := mid
- return nums1[low] if nums1[low + 1] is same as nums2[low] otherwise nums2[low]
Example
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] nums1 = [5, 10, 15] nums2 = [10, 15, 8] print(solve(nums1, nums2))
Input
[5, 10, 15], [10, 15, 8]
Output
5
- Related Articles
- Python program to find the maximum and minimum value node from a doubly linked list
- Python program to find the maximum and minimum value node from a circular linked list
- Program to find folded list from a given linked list in Python
- Program to find linked list intersection from two linked list in Python
- Program to find largest kth index value of one list in Python
- Program to find only even indexed elements from list in Python
- Program to find sum of odd elements from list in Python
- Program to find mutual followers from a relations list in Python
- Program to find airports in correct order in Python?
- How to find the element from a Python list with a maximum value?
- How to find the element from a Python list with a minimum value?
- Python program to find N largest elements from a list
- Python program to find word score from list of words
- Java Program to copy value from one list to another list
- Program to find longest common prefix from list of strings in Python

Advertisements