Want Help Cracking FAANG?

(Then click this)

×
Back to Question Bank

1826. Faulty Sensor - Leetcode Solution

Problem Description

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:

  • There is exactly one index where the sensors first differ, and after that, all subsequent values from the faulty sensor are incorrect.
  • Do not reuse elements; only the first difference is considered.
  • Both arrays have the same length.

Thought Process

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:

  • Finding the first index where the arrays differ.
  • Checking which sensor continues to match the other sensor’s values (excluding the first differing value).
This ensures we only scan the arrays once and make a decision based on the pattern of differences after the first mismatch.

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.

Solution Approach

Here is a step-by-step guide to solving the Faulty Sensor problem:

  1. Find the first mismatch: Iterate over both arrays to locate the first index i where sensor1[i] != sensor2[i].
  2. Determine which sensor is faulty:
    • Assume sensor1 is faulty: Check if all subsequent values in sensor2 (from i+1 onward) match sensor1 (from i onward). If so, sensor1 is faulty.
    • Assume sensor2 is faulty: Check if all subsequent values in sensor1 (from i+1 onward) match sensor2 (from i onward). If so, sensor2 is faulty.
  3. Return the answer:
    • If sensor1 is faulty, return sensor1[i].
    • If sensor2 is faulty, return sensor2[i].
    • If neither condition is met, return -1.

This approach is efficient because it leverages the problem's constraints, ensuring only one scan past the first mismatch is necessary.

Example Walkthrough

Example:
sensor1 = [2,3,4,5]
sensor2 = [2,3,1,5]

  1. Compare indices:
    • Index 0: 2 == 2
    • Index 1: 3 == 3
    • Index 2: 4 != 1 ← first mismatch
  2. Check if sensor1 is faulty:
    • Does sensor2[3] == sensor1[2]? 5 == 4? No
  3. Check if sensor2 is faulty:
    • Does sensor1[3] == sensor2[2]? 5 == 1? No
  4. Both checks failed, so return -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]

  • Index 0: 1 == 1
  • Index 1: 2 == 2
  • Index 2: 3 != 4 ← first mismatch
  • Check if sensor1 is faulty:
    • Does sensor2[3] == sensor1[2]? 4 == 3? No
  • Check if sensor2 is faulty:
    • Does sensor1[3] == sensor2[2]? 4 == 4? Yes
  • So, sensor2 is faulty. Return sensor2[2] = 4.

Time and Space Complexity

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:

  • O(n) time: One pass to find the first mismatch, and a second (partial) pass to check subsequent matches.
  • O(1) space: No extra data structures are needed, just a few variables.
This is efficient and leverages the constraints given in the problem statement.

Summary

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.

Code Implementation

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;
};