Skip to content

Commit 6633d3f

Browse files
committed
Add set mismatch problem
1 parent 186dc95 commit 6633d3f

File tree

11 files changed

+135
-2
lines changed

11 files changed

+135
-2
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ List of Programs related to data structures and algorithms
6464
| 23 | Disappeared numbers | [Source](https://github.com/sudheerj/datastructures-algorithms/blob/master/src/javascript/algorithms/array/23.disappearedNumbers/disappearedNumbers.js) | [JavaScript](https://livecodes.io/?console=open&x=https://github.com/sudheerj/datastructures-algorithms/blob/master/src/javascript/algorithms/array/23.disappearedNumbers/disappearedNumbers.js) | [Documentation](https://github.com/sudheerj/datastructures-algorithms/blob/master/src/javascript/algorithms/array/23.disappearedNumbers/disappearedNumbers.md) | Easy | Array traversal |
6565
| 24 | Identical pairs | [Source](https://github.com/sudheerj/datastructures-algorithms/blob/master/src/javascript/algorithms/array/24.identicalPairs/identicalPairs.js) | [JavaScript](https://livecodes.io/?console=open&x=https://github.com/sudheerj/datastructures-algorithms/blob/master/src/javascript/algorithms/array/24.identicalPairs/identicalPairs.js) | [Documentation](https://github.com/sudheerj/datastructures-algorithms/blob/master/src/javascript/algorithms/array/24.identicalPairs/identicalPairs.md) | Easy | Array traversal & map |
6666
| 25 | Destination City | [Source](https://github.com/sudheerj/datastructures-algorithms/blob/master/src/javascript/algorithms/array/25.destinationCity/destinationCity.js) | [JavaScript](https://livecodes.io/?console=open&x=https://github.com/sudheerj/datastructures-algorithms/blob/master/src/javascript/algorithms/array/25.destinationCity/destinationCity.js) | [Documentation](https://github.com/sudheerj/datastructures-algorithms/blob/master/src/javascript/algorithms/array/25.destinationCity/destinationCity.md) | Easy | Array traversal & set |
67+
| 26 | Set mismatch | [Source](https://github.com/sudheerj/datastructures-algorithms/blob/master/src/javascript/algorithms/array/26.setMismatch/setMismatch.js) | [JavaScript](https://livecodes.io/?console=open&x=https://github.com/sudheerj/datastructures-algorithms/blob/master/src/javascript/algorithms/array/25.setMismatch/setMismatch.js) | [Documentation](https://github.com/sudheerj/datastructures-algorithms/blob/master/src/javascript/algorithms/array/26.setMismatch/setMismatch.md) | Easy | Array traversal & marking numbers |
6768

6869
### String
6970

src/java1/algorithms/array/destinationCity/DestinationCity.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ This problem is solved with the help of array traversal and hash set for storing
2626

2727
2. Initialize a `fromCities` set variale to store the list of from cities in paths. The given paths array is traversed and `fromCities` is populated with all from city values.
2828

29-
3. Iterate over given array(`path`) agin, return the toCity value as destination city if that city doesn't exist inside `fromCities`. This is because only destination city cannot have a outgoing path.
29+
3. Iterate over given array(`path`) again, return the toCity value as destination city if that city doesn't exist inside `fromCities`. This is because only destination city cannot have a outgoing path.
3030

3131
**Time and Space complexity:**
3232
This algorithm has a time complexity of `O(n)`, where `n` is the number of path elements in an array. This is because we need to iterate over all the elements at most twice for finding the destination city.
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package java1.algorithms.array.setMismatch;
2+
3+
import java.util.Arrays;
4+
5+
public class SetMismatch {
6+
private static int[] findDuplicateAndMissing(int[] nums){
7+
int length = nums.length;
8+
int duplicate = -1;
9+
int missing = -1;
10+
11+
for (int num : nums) {
12+
num = Math.abs(num);
13+
if(nums[num-1]<0) {
14+
duplicate = num;
15+
} else {
16+
nums[num-1] = -nums[num-1];
17+
}
18+
}
19+
20+
for (int i=0; i< length; i++) {
21+
if(nums[i]>0) {
22+
missing = i+1;
23+
}
24+
}
25+
26+
return new int[]{duplicate, missing};
27+
}
28+
public static void main(String[] args) {
29+
int[] nums1 = new int[]{1,3,3,4};
30+
int[] nums2 = new int[]{1,1};
31+
32+
System.out.println(Arrays.toString(findDuplicateAndMissing(nums1)));
33+
System.out.println(Arrays.toString(findDuplicateAndMissing(nums2)));
34+
}
35+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
**Description:**
2+
Given an integer array `nums` representing a set of numbers, where the set originally contained all integers from 1 to `n`. However, due to an error, one number in the set has been duplicated, and one number is missing. Find the duplicated number and the missing number in the array.
3+
4+
### Examples
5+
Example 1:
6+
7+
Input: nums = [1,3,3,4]
8+
Output: [ 3, 2 ]
9+
10+
Example 2:
11+
12+
Input: nums = [1,1]
13+
Output: [ 1, 2 ]
14+
15+
**Algorithmic Steps**
16+
This problem is solved with the help of array traversal and marking the numbers which exists by negating them. The algorithmic approach can be summarized as follows:
17+
18+
1. Create a function named `findDuplicateAndMissing`, which accepts input array(`nums`) to find the duplicated and missing numbers in the form of an array.
19+
20+
2. Calculate integers length with in `length` variable, and initialize `duplicated` and `missing` numbers to -1.
21+
22+
3. Iterate over given array(`nums`) to find the duplicated value through marking of each number.
23+
24+
4. Iterate over given array(`nums`) again to find the missing number by checking each number is greater than zero or not.
25+
26+
5. Return an array with duplicated and missing values in the form of an array.
27+
28+
**Time and Space complexity:**
29+
This algorithm has a time complexity of `O(n)`, where `n` is the number of elements in an array. This is because we need to iterate over all the elements at most twice for finding the duplicated and missing numbers.
30+
31+
It takes constant time complexity of `O(1)` without using any datastructures other than constant variables.
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package java1.algorithms.hashmap.minOperationsAlternating;
2+
3+
public class MinOperationsAlternating {
4+
5+
}

src/java1/algorithms/hashmap/minOperationsAlternating/MinOperationsAlternating.md

Whitespace-only changes.

src/javascript/algorithms/array/25.destinationCity/destinationCity.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ This problem is solved with the help of array traversal and hash set for storing
2626

2727
2. Initialize a `fromCities` set variale to store the list of from cities in paths. The given paths array is traversed and `fromCities` is populated with all from city values.
2828

29-
3. Iterate over given array(`path`) agin, return the toCity value as destination city if that city doesn't exist inside `fromCities`. This is because only destination city cannot have a outgoing path.
29+
3. Iterate over given array(`path`) again, return the toCity value as destination city if that city doesn't exist inside `fromCities`. This is because only destination city cannot have a outgoing path.
3030

3131
**Time and Space complexity:**
3232
This algorithm has a time complexity of `O(n)`, where `n` is the number of path elements in an array. This is because we need to iterate over all the elements at most twice for finding the destination city.
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
function findDuplicateAndMissing(nums){
2+
const length = nums.length;
3+
let duplicate = -1;
4+
let missing = -1;
5+
6+
for (let num of nums) {
7+
num = Math.abs(num);
8+
if(nums[num-1]<0) {
9+
duplicate = num;
10+
} else {
11+
nums[num-1] = -nums[num-1];
12+
}
13+
}
14+
15+
for (let i=0; i<length; i++) {
16+
if(nums[i] >0) {
17+
missing = i+1;
18+
}
19+
}
20+
21+
return [duplicate, missing];
22+
}
23+
24+
const nums1 = [1,3,3,4];
25+
const nums2 = [1,1];
26+
console.log(findDuplicateAndMissing(nums1));
27+
console.log(findDuplicateAndMissing(nums2));
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
**Description:**
2+
Given an integer array `nums` representing a set of numbers, where the set originally contained all integers from 1 to `n`. However, due to an error, one number in the set has been duplicated, and one number is missing. Find the duplicated number and the missing number in the array.
3+
4+
### Examples
5+
Example 1:
6+
7+
Input: nums = [1,3,3,4]
8+
Output: [ 3, 2 ]
9+
10+
Example 2:
11+
12+
Input: nums = [1,1]
13+
Output: [ 1, 2 ]
14+
15+
**Algorithmic Steps**
16+
This problem is solved with the help of array traversal and marking the numbers which exists by negating them. The algorithmic approach can be summarized as follows:
17+
18+
1. Create a function named `findDuplicateAndMissing`, which accepts input array(`nums`) to find the duplicated and missing numbers in the form of an array.
19+
20+
2. Calculate integers length with in `length` variable, and initialize `duplicated` and `missing` numbers to -1.
21+
22+
3. Iterate over given array(`nums`) to find the duplicated value through marking of each number.
23+
24+
4. Iterate over given array(`nums`) again to find the missing number by checking each number is greater than zero or not.
25+
26+
5. Return an array with duplicated and missing values in the form of an array.
27+
28+
**Time and Space complexity:**
29+
This algorithm has a time complexity of `O(n)`, where `n` is the number of elements in an array. This is because we need to iterate over all the elements at most twice for finding the duplicated and missing numbers.
30+
31+
It takes constant time complexity of `O(1)` without using any datastructures other than constant variables.
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
function minOperations(){
2+
3+
}

src/javascript/algorithms/hashtable/13.minOperationsAlternating/minOperationsAlternating.md

Whitespace-only changes.

0 commit comments

Comments
 (0)