Skip to content

Commit 9e11c04

Browse files
author
Abhay Bhat
committed
645-set-mismatch complete
1 parent 528ab27 commit 9e11c04

File tree

5 files changed

+154
-3
lines changed

5 files changed

+154
-3
lines changed

0-template/Solution.java

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
public class Solution {
2+
3+
}

0-template/readme.md

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
## 0. Problem Name
2+
### Brief Description
3+
4+
**Example:**
5+
6+
[<problem-name> on leetcode](https://leetcode.com/problems/<problem-name>/description/)
7+
8+
## Solution
9+
### Intuition
10+
11+
### Approach
12+
13+
### Time Complexity
14+
15+
### Space Complexity

645-set-mismatch/Solution.java

+65
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/**
2+
* This class provides a solution for finding the error numbers in an array. Error numbers are numbers that are missing and numbers that are repeated.
3+
* Provided only one is missing and one is repeated and the array contains only positive numbers from 1 to n, where n is the length of the array.
4+
*/
5+
public class Solution {
6+
/**
7+
* Finds the error numbers in the given array.
8+
*
9+
* @param nums The array of integers.
10+
* @return An array containing the error numbers, where the first element is the missing number and the second element is the repeating number.
11+
*/
12+
public int[] findErrorNums(int[] nums) {
13+
int xor = 0;
14+
15+
// XOR all the numbers from 1 to nums.length and all the numbers in the nums array.
16+
// The result is the XOR of the missing and repeating numbers.
17+
for(int i = 1; i <= nums.length; i++) {
18+
xor ^= i;
19+
xor ^= nums[i-1];
20+
}
21+
22+
// Initialize a counter i and a variable xor1 with the value of xor.
23+
// Start a loop that will run up to 31 times. This is because the maximum value of n is 2^31 - 1(integer limit).
24+
// In each iteration, check if the least significant bit of xor1 is 1.
25+
// If it is, break the loop as we've found the first set bit.
26+
// If it's not, shift the bits of xor1 one place to the right, effectively dividing it by 2.
27+
int i = 0;
28+
int xor1 = xor;
29+
for(i = 0; i < 30; i++) {
30+
if((xor1 & 1) == 1)
31+
break;
32+
xor1 >>= 1;
33+
}
34+
35+
// This code separates the numbers into two groups based on the checker bit.
36+
// If the checker bit in a number is 1, it is XORed with xor1.
37+
// If the checker bit in a number is 0, it is XORed with xor2.
38+
// This is done for both the numbers from 1 to nums.length and the numbers in the nums array.
39+
// The result is that xor1 and xor2 hold the missing and repeating numbers, but it's not known which is which.
40+
int checker = i;
41+
int xor2 = 0;
42+
xor1 = 0;
43+
for(i = 1; i <= nums.length; i++) {
44+
if(((i >> checker) & 1) == 1)
45+
xor1 ^= i;
46+
else
47+
xor2 ^= i;
48+
49+
if(((nums[i-1] >> checker) & 1) == 1)
50+
xor1 ^= nums[i-1];
51+
else
52+
xor2 ^= nums[i-1];
53+
}
54+
55+
// This code finds the missing and repeating numbers.
56+
// It does so by checking if the missing number is in the nums array.
57+
// If it is, then the missing number is xor1 and the repeating number is xor2.
58+
// If it's not, then the missing number is xor2 and the repeating number is xor1.
59+
for(i = 0; i < nums.length; i++) {
60+
if(xor1 == nums[i])
61+
return new int[] {xor1, xor2};
62+
}
63+
return new int[] {xor2, xor1};
64+
}
65+
}

645-set-mismatch/readme.md

+68
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
## 645. Set Mismatch
2+
### Brief Description
3+
4+
- Given a set of integers `nums`, which originally contains all the numbers from 1 to n.
5+
- Due to some error, one of the numbers in `nums` got duplicated to another number in the set
6+
- This results in repetition of one number and loss of another number.
7+
8+
Find the number that occurs twice and the number that is missing and return them in the form of an array.
9+
10+
**Example:**
11+
>**Input:** nums = [1,2,2,4] \
12+
**Output:** [2,3]
13+
14+
[set-mismatch on leetcode](https://leetcode.com/problems/set-mismatch/description/)
15+
16+
## Solution
17+
### Intuition
18+
Use xor to find the repeated and missing values.
19+
20+
### Approach
21+
since all the number are from 1 - n we traverse from 1 - n and xor all the values and xor them with the array elements, we will end up with the xor of the missing element and repeated element
22+
23+
```x
24+
example:
25+
arr = [6,4,5,2,1,7,4]
26+
27+
result_xor =
28+
(1 ^ 2 ^ 3 ^ 4 ^ 5 ^ 6 ^ 7) ^
29+
(6 ^ 4 ^ 5 ^ 2 ^ 1 ^ 7 ^ 4)
30+
= 3 ^ 4
31+
```
32+
33+
now based on the least significant set bit ($i^{th}$ bit) in the resulting XOR we can divide the number array into 2 parts one part with the $i^{th}$ bit set and the other set with $i^{th}$ bit is not set.
34+
35+
```x
36+
Why does this work?
37+
```
38+
Well since if the ith bit is set in the result_xor it means that $i^{th}$ bit is set in only one of the 2 numbers forming the xor hence when we partition the array into 2 parts we will end up with 2 xors one having the missing number and one having the repeated number.
39+
40+
```x
41+
from the above example we have
42+
result_xor = 3 ^ 4 = 7
43+
in 7 0th bit is set hence i = 0
44+
45+
so we divide the array and index into 2 parts based
46+
on the rightmost bit (p1 is part1(right bit 0) and
47+
p2 is part2(right bit 1))
48+
49+
the array --- the indices(1-indexed)
50+
6 - 110 - p1 1 - 001 - p2
51+
4 - 100 - p1 2 - 010 - p1
52+
5 - 101 - p2 3 - 011 - p2
53+
2 - 010 - p1 4 - 100 - p1
54+
1 - 001 - p2 5 - 101 - p2
55+
7 - 111 - p2 6 - 110 - p1
56+
4 - 100 - p1 7 - 111 - p2
57+
58+
xor_p1 = (6 ^ 4 ^ 2 ^ 4) ^ (2 ^ 4 ^ 6) = 4
59+
xor_p2 = (5 ^ 1 ^ 7) ^ (1 ^ 3 ^ 5 ^ 7) = 3
60+
```
61+
62+
Iterate over the array again to check which one of xor_p1 and xor_p2 is present in the array and return answer appropriately.
63+
64+
### Time Complexity
65+
$O(n)$ -> since the array is being traversed in one direction
66+
67+
### Space Complexity
68+
$O(1)$ -> no extra space used only integers to store XOR values

818-race-car/readme.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@ When you get an instruction `'R'`, your car does the following:
1414
- Your position stays the same.
1515

1616
**Example:**
17-
- sequence: `AAR`
18-
- your car goes to positions `0 --> 1 --> 3 --> 3`
19-
- your speed goes to `1 --> 2 --> 4 --> -1`
17+
> sequence: `AAR` \
18+
your car goes to positions `0 --> 1 --> 3 --> 3` \
19+
your speed goes to `1 --> 2 --> 4 --> -1`
2020
2121
Given a target position target, return the length of the shortest sequence of instructions to get there.
2222

0 commit comments

Comments
 (0)