An experiment is being conducted in a lab. To ensure accuracy, there aretwo sensors collecting data simultaneously. You are given two arrays
sensor1 and sensor2, where sensor1[i] and sensor2[i] are the ith
data points collected by the two sensors.
However, this type of sensor has a chance of being defective, which causes
exactly one data point to be dropped. After the data is dropped, all the data points to the right of the dropped data are shifted one place to the left, and the last data point is replaced with some random value. It is guaranteed that this random value will not be equal to the dropped value.
For example, if the correct data is [1,2,_**3**_ ,4,5] and 3 is dropped, the sensor could return [1,2,4,5,_**7**_] (the last position can be any value, not just 7).
We know that there is a defect in at most one of the sensors. Return the sensor number (1or2 _) with the defect. If there isno defect in either sensor or if it isimpossible to determine the defective sensor, return _-1.
Input: sensor1 =[2,3,4,5], sensor2 =[2,1,3,4]Output: 1Explanation: Sensor 2 has the correct values.The second data point from sensor 2is dropped, and the last value of sensor 1is replaced by a 5.
Example 2:
1
2
3
4
Input: sensor1 =[2,2,2,2,2], sensor2 =[2,2,2,2,5]Output: -1Explanation: It is impossible to determine which sensor has a defect.Dropping the last value for either sensor could produce the output for the other sensor.
Example 3:
1
2
3
4
Input: sensor1 =[2,3,2,2,3,2], sensor2 =[2,3,2,3,2,7]Output: 2Explanation: Sensor 1 has the correct values.The fourth data point from sensor 1is dropped, and the last value of sensor 1is replaced by a 7.
We compare the two arrays from left to right. The first index where they differ is a candidate for the dropped value. We check if removing that value from either sensor can make the rest of the arrays match (except for the last value, which can be any value except the dropped one). If both are possible, or neither is possible, return -1. Otherwise, return the sensor number with the defect.
classSolution {
public:int badSensor(vector<int>& s1, vector<int>& s2) {
int n = s1.size(), i =0;
while (i < n && s1[i] == s2[i]) ++i;
if (i == n-1|| i == n) return-1;
bool f1 = true, f2 = true;
for (int j = i; j < n-1; ++j) {
if (s1[j+1] != s2[j]) f1 = false;
if (s2[j+1] != s1[j]) f2 = false;
}
if (f1 &&!f2) return1;
if (!f1 && f2) return2;
return-1;
}
};
classSolution {
publicintbadSensor(int[] s1, int[] s2) {
int n = s1.length, i = 0;
while (i < n && s1[i]== s2[i]) i++;
if (i == n-1 || i == n) return-1;
boolean f1 =true, f2 =true;
for (int j = i; j < n-1; ++j) {
if (s1[j+1]!= s2[j]) f1 =false;
if (s2[j+1]!= s1[j]) f2 =false;
}
if (f1 &&!f2) return 1;
if (!f1 && f2) return 2;
return-1;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
classSolution {
funbadSensor(s1: IntArray, s2: IntArray): Int {
val n = s1.size
var i = 0while (i < n && s1[i] == s2[i]) i++if (i == n-1|| i == n) return -1var f1 = truevar f2 = truefor (j in i until n-1) {
if (s1[j+1] != s2[j]) f1 = falseif (s2[j+1] != s1[j]) f2 = false }
returnwhen {
f1 && !f2 ->1 !f1 && f2 ->2else-> -1 }
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
classSolution:
defbadSensor(self, sensor1: list[int], sensor2: list[int]) -> int:
n = len(sensor1)
i =0while i < n and sensor1[i] == sensor2[i]:
i +=1if i == n-1or i == n:
return-1 f1 = all(sensor1[j+1] == sensor2[j] for j in range(i, n-1))
f2 = all(sensor2[j+1] == sensor1[j] for j in range(i, n-1))
if f1 andnot f2:
return1ifnot f1 and f2:
return2return-1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
impl Solution {
pubfnbad_sensor(sensor1: Vec<i32>, sensor2: Vec<i32>) -> i32 {
let n = sensor1.len();
letmut i =0;
while i < n && sensor1[i] == sensor2[i] { i +=1; }
if i == n-1|| i == n { return-1; }
letmut f1 =true;
letmut f2 =true;
for j in i..n-1 {
if sensor1[j+1] != sensor2[j] { f1 =false; }
if sensor2[j+1] != sensor1[j] { f2 =false; }
}
if f1 &&!f2 { return1; }
if!f1 && f2 { return2; }
-1 }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
classSolution {
badSensor(sensor1: number[], sensor2: number[]):number {
constn=sensor1.length;
leti=0;
while (i<n&&sensor1[i] ===sensor2[i]) i++;
if (i===n-1||i===n) return-1;
letf1=true, f2=true;
for (letj=i; j<n-1; ++j) {
if (sensor1[j+1] !==sensor2[j]) f1=false;
if (sensor2[j+1] !==sensor1[j]) f2=false;
}
if (f1&&!f2) return1;
if (!f1&&f2) return2;
return-1;
}
}