Karan Kumar a year agoUsing QuickSort, time complexity O(N) const solve = (intArray) => { // quick sort using recursion const quickSort = (arr) => { if (arr.length < 2) return arr let p = arr[arr.length-1] // pivot let l = []; let r = []; for (let i=0; i<arr.length-1; i++){ arr[i]**2 > p**2 ? r.push(arr[i]) : l.push(arr[i]) } return [...quickSort(l), p, ...quickSort(r)] } return quickSort(intArray) }; CancelSubmit
Abdelhamid Ismail3 years agoconst solve = (intArray) => { return intArray.sort((a,b) => Math.pow(a,2)-Math.pow(b,2) ) }; CancelSubmit
Comments
Karan Kumar a year ago
Using QuickSort, time complexity O(N)
Abdelhamid Ismail3 years ago