FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
hackerrankSolutions-JavaScript/Sorting/lily'sHomework.js at master · greatnonso7/hackerrankSolutions-JavaScript · GitHub
greatnonso7
/
hackerrankSolutions-JavaScript
Public
forked from
aditiraj/hackerrankSolutions-JavaScript
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
hackerrankSolutions-JavaScript
/
Sorting
/
lily'sHomework.js
Copy path
More file actions
More file actions
Latest commit
History
History
History
62 lines (51 loc) · 1.59 KB
Breadcrumbs
hackerrankSolutions-JavaScript
/
Sorting
/
lily'sHomework.js
Copy path
File metadata and controls
62 lines (51 loc) · 1.59 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
function
lilysHomework
(
arr
)
{
/* Sort the array in ascending order and descending order as both meet the given requirement. Compare the sorted arrays to the original
array and if the elements do not match perform the swap and count the swap opertions.Print the minimum value. */
var
arrCpy
=
arr
.
slice
(
)
;
var
arrCpy2
=
arr
.
slice
(
)
;
var
indexArr
=
[
]
;
var
count
=
0
;
var
index
;
var
temp
;
var
decCount
=
0
;
for
(
var
i
=
0
;
i
<
arr
.
length
;
i
++
)
{
indexArr
[
arr
[
i
]
]
=
i
;
}
arr
.
sort
(
function
(
a
,
b
)
{
return
a
-
b
}
)
;
var
sortedArrCpy
=
arr
.
slice
(
)
;
var
decSort
=
sortedArrCpy
.
reverse
(
)
;
for
(
var
i
=
0
;
i
<
arr
.
length
;
i
++
)
{
if
(
arr
[
i
]
!=
arrCpy
[
i
]
)
{
temp
=
arrCpy
[
i
]
;
index
=
indexArr
[
arr
[
i
]
]
;
indexArr
[
arr
[
i
]
]
=
i
;
indexArr
[
arrCpy
[
i
]
]
=
index
;
arrCpy
[
i
]
=
arr
[
i
]
;
arrCpy
[
index
]
=
temp
;
count
++
;
}
}
indexArr
=
[
]
;
for
(
var
i
=
0
;
i
<
arrCpy2
.
length
;
i
++
)
{
indexArr
[
arrCpy2
[
i
]
]
=
i
;
}
for
(
var
i
=
0
;
i
<
decSort
.
length
;
i
++
)
{
if
(
decSort
[
i
]
!=
arrCpy2
[
i
]
)
{
temp
=
arrCpy2
[
i
]
;
index
=
indexArr
[
decSort
[
i
]
]
;
indexArr
[
decSort
[
i
]
]
=
i
;
indexArr
[
arrCpy2
[
i
]
]
=
index
;
arrCpy2
[
i
]
=
decSort
[
i
]
;
arrCpy2
[
index
]
=
temp
;
decCount
++
;
}
}
return
Math
.
min
(
count
,
decCount
)
;
}
function
main
(
)
{
var
n
=
parseInt
(
readLine
(
)
)
;
arr
=
readLine
(
)
.
split
(
' '
)
;
arr
=
arr
.
map
(
Number
)
;
var
result
=
lilysHomework
(
arr
)
;
process
.
stdout
.
write
(
""
+
result
+
"\n"
)
;
}
Back
|
FazBrowse Home
|
New Git URL