Blind 75
Missing Number
- Problem
- LC 268
- Category
- Binary
- File
- blind75_LC268MissingNumber.java
- Path
- pkg5leetcode/blind75/blind75_LC268MissingNumber.java
- Package
- pkg5leetcode.blind75
- Command
- java pkg5leetcode/blind75/blind75_LC268MissingNumber.java
- Approach
- XOR all indices and values; pairs cancel leaving missing.
- Complexity
- Time O(n), Space O(1)
There is no in-browser runner. This is the file from the curriculum, unchanged.
1package pkg5leetcode.blind75;2 3/*4 * Missing Number | LC 2685 * APPROACH: XOR all indices and values; pairs cancel leaving missing.6 * COMPLEXITY: Time O(n), Space O(1)7 */8public class blind75_LC268MissingNumber {9 static int missingNumber(int[] nums) {10 int x = nums.length;11 for (int i = 0; i < nums.length; i++) x ^= i ^ nums[i];12 return x;13 }14 15 public static void main(String[] args) {16 check(missingNumber(new int[]{3, 0, 1}) == 2, "case1");17 check(missingNumber(new int[]{0, 1}) == 2, "case2");18 System.out.println("all tests passed");19 }20 21 static void check(boolean cond, String name) {22 if (!cond) throw new AssertionError("FAILED: " + name);23 System.out.println(" PASS " + name);24 }25}