-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathminimum-swaps-2.js
More file actions
73 lines (67 loc) · 1.51 KB
/
Copy pathminimum-swaps-2.js
File metadata and controls
73 lines (67 loc) · 1.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
/*
*
* ------------------
* | MINIMUM SWAP |
* ------------------
*
* You are given an unordered array consisting
* of consecutive integers without any duplicates.
* You are allowed to swap any two elements. You
* need to find the minimum number of swaps required
* to sort the array in ascending order.
*
* e.g
*
* arr = [7,1,3,2,4,5,6]
*
* i arr swap (indices)
* 0 [7, 1, 3, 2, 4, 5, 6] swap (0,3)
* 1 [2, 1, 3, 7, 4, 5, 6] swap (0,1)
* 2 [1, 2, 3, 7, 4, 5, 6] swap (3,4)
* 3 [1, 2, 3, 4, 7, 5, 6] swap (4,5)
* 4 [1, 2, 3, 4, 5, 7, 6] swap (5,6)
* 5 [1, 2, 3, 4, 5, 6, 7]
*
* returns 5
*
*
* */
/*
*
* ***** SOLUTION
*
* */
function minimumSwaps(arr) {
// initialize swaps counter
let swapCounter = 0;
// loop over arr
for(let i = 0; i < arr.length; i++) {
// if arr[i] !== i + 1
if(arr[i] !== i + 1){
// find index of correct value
const correctIndex = arr.indexOf(i + 1)
// swatp current index with index of correct value
const currValue = arr[i];
arr[i] = arr[correctIndex];
arr[correctIndex] = currValue;
// increase swaps counter
swapCounter ++;
}
}
// return swaps counter
return swapCounter;
}
/*
*
* ***** TESTS
*
* */
const arr1 = [1, 3, 2]; // 1
const arr2 = [1, 4, 2, 3]; // 2
const arr3 = [4, 1, 2, 3]; // 3
const arr4 = [2, 3, 4, 1, 5] // 3
const arr5 = [ 4, 3, 1, 2 ] // 3
const arr6 = [7, 1, 3, 2, 4, 5, 6] // 5
//minimumSwaps(arr1);
//minimumSwaps(arr2);
console.log(minimumSwaps(arr6));