The Faulty Sensor problem presents two integer arrays, sensor1
and sensor2
, of equal length. These arrays record a sequence of measurements from two sensors. Exactly one of the sensors is faulty, meaning at some index it produces an incorrect value and then continues to output incorrect values for all subsequent indices. The other sensor is always correct.
Your task is to determine which sensor is faulty. If sensor1
is faulty, return the value at the first differing index from sensor1
. If sensor2
is faulty, return the value at the first differing index from sensor2
. If it is not possible to determine the faulty sensor (for example, if both sensors have identical readings), return -1
.
Constraints:
To solve this problem, we need to identify the first index where sensor1
and sensor2
differ. Since only one sensor is faulty and starts producing incorrect values from the first difference onward, the correct sensor will continue matching the other sensor's prior values.
A naive (brute-force) approach is to compare both arrays at every index, but since the arrays only differ starting at one point, we can optimize this by:
The key insight is: the correct sensor will "line up" with the other sensor’s values from the next index onward, while the faulty sensor will keep producing wrong values.
Here is a step-by-step guide to solving the Faulty Sensor problem:
i
where sensor1[i] != sensor2[i]
.
sensor1
is faulty: Check if all subsequent values in sensor2
(from i+1
onward) match sensor1
(from i
onward). If so, sensor1
is faulty.
sensor2
is faulty: Check if all subsequent values in sensor1
(from i+1
onward) match sensor2
(from i
onward). If so, sensor2
is faulty.
sensor1
is faulty, return sensor1[i]
.sensor2
is faulty, return sensor2[i]
.-1
.This approach is efficient because it leverages the problem's constraints, ensuring only one scan past the first mismatch is necessary.
Example:
sensor1 = [2,3,4,5]
sensor2 = [2,3,1,5]
sensor1
is faulty:
sensor2[3]
== sensor1[2]
? 5 == 4? Nosensor2
is faulty:
sensor1[3]
== sensor2[2]
? 5 == 1? No-1
(in this case, both sensors seem faulty after the mismatch, which violates problem constraints, but for this example, let's consider when one matches).
Another Example:
sensor1 = [1,2,3,4]
sensor2 = [1,2,4,4]
sensor1
is faulty:
sensor2[3]
== sensor1[2]
? 4 == 3? Nosensor2
is faulty:
sensor1[3]
== sensor2[2]
? 4 == 4? Yessensor2
is faulty. Return sensor2[2]
= 4.
Brute-force Approach:
If we compared every pair and checked all possible faulty scenarios, the time complexity could reach O(n^2), which is inefficient.
Optimized Approach:
The described method only requires:
The Faulty Sensor problem is solved by identifying the first index where the two sensor arrays differ, then checking which sensor continues to align with the other’s values. This approach is efficient, requiring only a single pass and no extra space. The key insight is that the correct sensor’s subsequent values will match the other's prior values, allowing us to confidently identify the faulty sensor and return the correct answer.
class Solution:
def badSensor(self, sensor1, sensor2):
n = len(sensor1)
for i in range(n):
if sensor1[i] != sensor2[i]:
# Check if sensor1 is faulty
is_sensor1_faulty = True
for j in range(i+1, n):
if sensor2[j] != sensor1[j-1]:
is_sensor1_faulty = False
break
if is_sensor1_faulty:
return sensor1[i]
# Check if sensor2 is faulty
is_sensor2_faulty = True
for j in range(i+1, n):
if sensor1[j] != sensor2[j-1]:
is_sensor2_faulty = False
break
if is_sensor2_faulty:
return sensor2[i]
return -1
return -1
class Solution {
public:
int badSensor(vector<int>& sensor1, vector<int>& sensor2) {
int n = sensor1.size();
for (int i = 0; i < n; ++i) {
if (sensor1[i] != sensor2[i]) {
// Check if sensor1 is faulty
bool isSensor1Faulty = true;
for (int j = i + 1; j < n; ++j) {
if (sensor2[j] != sensor1[j - 1]) {
isSensor1Faulty = false;
break;
}
}
if (isSensor1Faulty)
return sensor1[i];
// Check if sensor2 is faulty
bool isSensor2Faulty = true;
for (int j = i + 1; j < n; ++j) {
if (sensor1[j] != sensor2[j - 1]) {
isSensor2Faulty = false;
break;
}
}
if (isSensor2Faulty)
return sensor2[i];
return -1;
}
}
return -1;
}
};
class Solution {
public int badSensor(int[] sensor1, int[] sensor2) {
int n = sensor1.length;
for (int i = 0; i < n; i++) {
if (sensor1[i] != sensor2[i]) {
// Check if sensor1 is faulty
boolean isSensor1Faulty = true;
for (int j = i + 1; j < n; j++) {
if (sensor2[j] != sensor1[j - 1]) {
isSensor1Faulty = false;
break;
}
}
if (isSensor1Faulty) return sensor1[i];
// Check if sensor2 is faulty
boolean isSensor2Faulty = true;
for (int j = i + 1; j < n; j++) {
if (sensor1[j] != sensor2[j - 1]) {
isSensor2Faulty = false;
break;
}
}
if (isSensor2Faulty) return sensor2[i];
return -1;
}
}
return -1;
}
}
var badSensor = function(sensor1, sensor2) {
let n = sensor1.length;
for (let i = 0; i < n; i++) {
if (sensor1[i] !== sensor2[i]) {
// Check if sensor1 is faulty
let isSensor1Faulty = true;
for (let j = i + 1; j < n; j++) {
if (sensor2[j] !== sensor1[j - 1]) {
isSensor1Faulty = false;
break;
}
}
if (isSensor1Faulty) return sensor1[i];
// Check if sensor2 is faulty
let isSensor2Faulty = true;
for (let j = i + 1; j < n; j++) {
if (sensor1[j] !== sensor2[j - 1]) {
isSensor2Faulty = false;
break;
}
}
if (isSensor2Faulty) return sensor2[i];
return -1;
}
}
return -1;
};