Skip to content

Commit 3fddfdf

Browse files
Added more cyclic sort problems
1 parent e3fc252 commit 3fddfdf

File tree

5 files changed

+214
-0
lines changed

5 files changed

+214
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package com.Sorting_Problems._04_Cyclic_Sort_Problems;
2+
/*
3+
Given an array nums of n integers where nums[i] is in the range [1, n],
4+
return an array of all the integers in the range [1, n] that do not appear in nums.
5+
6+
Example 1:
7+
Input: nums = [4,3,2,7,8,2,3,1]
8+
Output: [5,6]
9+
*/
10+
import java.util.ArrayList;
11+
public class _06_Find_Disappeared_Numbers {
12+
public static void main(String[] args) {
13+
int[] arr = {4,3,2,7,8,2,3,1};
14+
System.out.println(findDisappearedNumbers(arr));
15+
}
16+
static ArrayList<Integer> findDisappearedNumbers(int[] nums) {
17+
int i = 0;
18+
while(i < nums.length) {
19+
int correct = nums[i] -1;
20+
if(nums[i] != nums[correct]) {
21+
swap(nums,i,correct);
22+
} else { i++; }
23+
}
24+
25+
// just find missing numbers
26+
ArrayList<Integer> ans = new ArrayList<>();
27+
for (int index = 0; index < nums.length; index++) {
28+
if(nums[index] != index+1) {
29+
ans.add(index + 1);
30+
}
31+
}
32+
return ans;
33+
}
34+
static void swap(int[] arr, int first, int second) {
35+
int temp = arr[first];
36+
arr[first] = arr[second];
37+
arr[second] = temp;
38+
}
39+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package com.Sorting_Problems._04_Cyclic_Sort_Problems;
2+
/*
3+
Given an array of integers nums containing n + 1 integers
4+
where each integer is in the range [1, n] inclusive.
5+
There is only one repeated number in nums, return this repeated number.
6+
You must solve the problem without modifying the array nums
7+
and uses only constant extra space.
8+
9+
Example 1:
10+
Input: nums = [1,3,4,2,2]
11+
Output: 2
12+
*/
13+
public class _07_Find_Duplicate {
14+
public static void main(String[] args) {
15+
int[] arr = {1,3,4,2,2};
16+
System.out.println("Duplicate: "+findDuplicate(arr));
17+
}
18+
static int findDuplicate(int[] nums) {
19+
int i = 0;
20+
while(i < nums.length) {
21+
22+
if(nums[i] != i + 1) {
23+
int correct = nums[i] -1;
24+
if(nums[i] != nums[correct]) {
25+
swap(nums,i,correct);
26+
} else {
27+
return nums[i];
28+
}
29+
} else {
30+
i++;
31+
}
32+
}
33+
return -1;
34+
}
35+
static void swap(int[] arr, int first, int second) {
36+
int temp = arr[first];
37+
arr[first] = arr[second];
38+
arr[second] = temp;
39+
}
40+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package com.Sorting_Problems._04_Cyclic_Sort_Problems;
2+
3+
import java.util.ArrayList;
4+
import java.util.Arrays;
5+
6+
/*
7+
Given an integer array nums of length n where all the integers of nums
8+
are in the range [1, n] and each integer appears once or twice,
9+
return an array of all the integers that appears twice.
10+
11+
You must write an algorithm that runs in O(n) time and uses only constant extra space.
12+
13+
Example 1:
14+
Input: nums = [4,3,2,7,8,2,3,1]
15+
Output: [2,3]
16+
*/
17+
public class _08_Find_all_Duplicates_in_Array {
18+
public static void main(String[] args) {
19+
int[] arr = {4,3,2,7,8,2,3,1};
20+
System.out.println("Duplicate Elements: "+ findDuplicates(arr));
21+
}
22+
static ArrayList<Integer> findDuplicates(int[] nums) {
23+
int i = 0;
24+
while(i < nums.length) {
25+
int correct = nums[i] - 1;
26+
if (nums[i] != nums[correct]) {
27+
swap(nums, i, correct);
28+
} else {
29+
i++;
30+
}
31+
}
32+
33+
// just find missing numbers
34+
ArrayList<Integer> ans = new ArrayList<>();
35+
for (int index = 0; index < nums.length; index++) {
36+
if(nums[index] != index+1) {
37+
ans.add(nums[index]);
38+
}
39+
}
40+
41+
return ans;
42+
}
43+
static void swap(int[] arr, int first, int second) {
44+
int temp = arr[first];
45+
arr[first] = arr[second];
46+
arr[second] = temp;
47+
}
48+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package com.Sorting_Problems._04_Cyclic_Sort_Problems;
2+
3+
import java.util.Arrays;
4+
5+
/*
6+
You have a set of integers s, which originally contains all the numbers from 1 to n.
7+
Unfortunately, due to some error, one of the numbers in s got duplicated to another number in the set,
8+
which results in repetition of one number and loss of another number.
9+
You are given an integer array nums representing the data status of this set after the error.
10+
Find the number that occurs twice and the number that is missing
11+
and return them in the form of an array.
12+
13+
Example 1:
14+
Input: nums = [1,2,2,4]
15+
Output: [2,3]
16+
*/
17+
public class _09_Set_Mismatch {
18+
public static void main(String[] args) {
19+
int[] arr = {1,2,2,5,4};
20+
System.out.println(Arrays.toString(findErrorNums(arr)));
21+
}
22+
static int[] findErrorNums(int[] nums) {
23+
int i = 0;
24+
while(i < nums.length) {
25+
int correct = nums[i] - 1;
26+
if(nums[i] != nums[correct]) {
27+
swap(nums,i,correct);
28+
} else { i++; }
29+
}
30+
31+
//search for first missing number
32+
for(int index = 0; index < nums.length; index++) {
33+
if(nums[index] != index + 1) {
34+
return new int[] {nums[index], index+1};
35+
}
36+
}
37+
// case 2
38+
return new int[] {-1,-1};
39+
}
40+
static void swap(int[] arr, int first, int second) {
41+
int temp = arr[first];
42+
arr[first] = arr[second];
43+
arr[second] = temp;
44+
}
45+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package com.Sorting_Problems._04_Cyclic_Sort_Problems;
2+
/*
3+
Given an unsorted integer array nums, return the smallest missing positive integer.
4+
You must implement an algorithm that runs in O(n) time and uses constant extra space.
5+
6+
Example 1:
7+
Input: nums = [1,2,0]
8+
Output: 3
9+
10+
Example 2:
11+
Input: nums = [3,4,-1,1]
12+
Output: 2
13+
*/
14+
public class _10_First_Missing_Positive {
15+
public static void main(String[] args) {
16+
int[] arr = {1,2,0};
17+
System.out.println(firstMissingPositive(arr));
18+
}
19+
static int firstMissingPositive(int[] nums) {
20+
int i = 0;
21+
while(i < nums.length) {
22+
int correct = nums[i] - 1;
23+
if(nums[i] > 0 && nums[i] <= nums.length && nums[i] != nums[correct]) {
24+
swap(nums,i,correct);
25+
} else { i++; }
26+
}
27+
28+
//search for first missing number
29+
for(int index = 0; index < nums.length; index++) {
30+
if(nums[index] != index + 1) {
31+
return index + 1;
32+
}
33+
}
34+
// case 2
35+
return nums.length + 1;
36+
}
37+
static void swap(int[] arr, int first, int second) {
38+
int temp = arr[first];
39+
arr[first] = arr[second];
40+
arr[second] = temp;
41+
}
42+
}

0 commit comments

Comments
 (0)