-
Notifications
You must be signed in to change notification settings - Fork 104
Expand file tree
/
Copy pathArray Manipulation.js
More file actions
28 lines (23 loc) · 908 Bytes
/
Copy pathArray Manipulation.js
File metadata and controls
28 lines (23 loc) · 908 Bytes
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
/*
Description:
Given an array of positive integers, replace every element with the least greater element to its right. If there is no greater element to its right, replace it with -1. For instance, given the array
[8, 58, 71, 18, 31, 32, 63, 92, 43, 3, 91, 93, 25, 80, 28],
the desired output is
[18, 63, 80, 25, 32, 43, 80, 93, 80, 25, 93, -1, 28, -1, -1].
Your task is to create a function "arrayManip()" that takes in an array as its argument, manipulates the array as described above, then return the resulting array.
Note: Return a new array, rather than modifying the passed array.
*/
function arrayManip(array){
for (let i=0;i<array.length;i++){
let arr=[];
for (let j=i+1;j<array.length;j++){
if (array[i]<array[j]) arr.push(array[j]);
}
let min=Math.min(...arr)
if (arr.length>0){
array[i]=min
} else {array[i]=-1};
arr=[];
}
return array
}