/O(n)_space_complexity.js Secret
Created
January 6, 2022 23:05
O(n) or Linear Complexity Gist
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const array = [50, 24, 13, 64, 85] | |
function add_numbers(array) { | |
let total = 0 //O(1) --> constant complexity (a variable is initialized) | |
for (let i = 0; i < array.length; i++) { //O(n) --> linear complexity (we loop through an arra, memory space is allocated to each element of the array) | |
total += array[i] //O(1) --> constant complexity (a variable is re-assigned) | |
} | |
return total | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment