diff --git a/Notes/Exercise/Questions/Exercise-04.md b/Notes/Exercise/Questions/Exercise-04.md index b02ecfc..c598d96 100644 --- a/Notes/Exercise/Questions/Exercise-04.md +++ b/Notes/Exercise/Questions/Exercise-04.md @@ -1 +1 @@ -**Write a function that accepts an array of strings and return a new array that only contains the strings that start with the character 'd'. Use Big O Notation to describe the time complexity of the function.** \ No newline at end of file +**Write a function that accepts an array of strings and return a new array that only contains the strings that start with the character 'd'. Use Big O Notation to describe the time complexity of the function.** diff --git a/Notes/Exercise/Solutions/Solution-01.md b/Notes/Exercise/Solutions/Solution-01.md new file mode 100644 index 0000000..b55dca5 --- /dev/null +++ b/Notes/Exercise/Solutions/Solution-01.md @@ -0,0 +1,11 @@ +**Use Big O Notation to describe the time complexity of the following function that determines whether a given yaer is a leap year:** + +```js + +function isLeapYear(year){ + return (year % 100 === 0) ? (year % 400 === 0) : (year % 4 === 0); +} + +```` + +The time complexity for `isLeapYear` is: `O(1)`. Regardless of the input the algorithm will still go through the same number of operations during operation. \ No newline at end of file diff --git a/Notes/Exercise/Solutions/Solution-02.md b/Notes/Exercise/Solutions/Solution-02.md new file mode 100644 index 0000000..129809d --- /dev/null +++ b/Notes/Exercise/Solutions/Solution-02.md @@ -0,0 +1,16 @@ +**Use Big O Notation to describe the time complexity of the following function that sums up all the numbers from a given array:** + +```js + +const sumOfArray = (array) => { + let sum = 0; + + for(let i = 0; i < array.length; i++){ + sum += array[i]; + } + return sum; +} + +``` + +The time complexity for `sumOfArray` is: `O(n)`. The number of operations that will be used in the for loop will increase as the input increases. \ No newline at end of file diff --git a/Notes/Exercise/Solutions/Solution-03.md b/Notes/Exercise/Solutions/Solution-03.md new file mode 100644 index 0000000..6556506 --- /dev/null +++ b/Notes/Exercise/Solutions/Solution-03.md @@ -0,0 +1,3 @@ +**Imagine you have a chessboard and put a single grain of rice on one square. On the second square, you put 2 grains of rice, since that is double the amount of rice on the previous square. On the third square, you put 4 grains and on the fourth square you put 8 grains. The pattern continues like that.** + +**Write a function calculates which square you will need to place a certain number of rice grains. For example, for 16 grains, the function will return 5. Use Big O Notation to describe the time complexity of that function.** \ No newline at end of file diff --git a/Notes/Exercise/Solutions/Solution-04.md b/Notes/Exercise/Solutions/Solution-04.md new file mode 100644 index 0000000..a4a011f --- /dev/null +++ b/Notes/Exercise/Solutions/Solution-04.md @@ -0,0 +1,8 @@ +**Write a function that accepts an array of strings and return a new array that only contains the strings that start with the character 'd'. Use Big O Notation to describe the time complexity of the function.** + +```js +const returnString = (arr) => { + return arr.filter(string => string.toLowerCase().startsWith('d')); +} +``` +The above function will have a time complexity of `O(n)`. This is because the filteration operations will increase as the input increases. \ No newline at end of file diff --git a/Notes/Exercise/Solutions/Solution-05.md b/Notes/Exercise/Solutions/Solution-05.md new file mode 100644 index 0000000..429455c --- /dev/null +++ b/Notes/Exercise/Solutions/Solution-05.md @@ -0,0 +1,16 @@ +**The following function calculates the median from an ordered array. Describe its time complexity in terms of Big O Notation:** + +```js + +const median (array) => { + const middle = Math.floor(array.length / 2); + + if(array.length % 2 === 0) { + return (array[middle - 1] + array[middle]) / 2; + } + return array[middle]; +} + +``` + +The time complexity for `median` is: `O(1)`. Regardless of the length of the input array accessing the middle elements will take constant time. \ No newline at end of file