• API
• FAQ
• Tools
• Archive
daily pastebin goal
56%
SHARE
TWEET

Untitled

a guest Sep 26th, 2018 51 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
1. Determine the Big-O complexity:
2.
3. Even or odd: constant time complexity 0(1)
4.   function isEven(value){
5.     if (value % 2 == 0){
6.       return true;
7.     }
8.     else
9.       return false;
10.   }
11.   // No matter the size of your input, the algorithm will take the same amount of time to complete.
12.   // Examples of constant time complexity are: Adding, dividing, accessing an array item.
13.
14. Are you here?: Polynomial 0(n^k)
15.   function areYouHere(arr1, arr2) {
16.     for (let i=0; i<arr1.length; i++) {
17.         const el1 = arr1[i];
18.         for (let j=0; j<arr2.length; j++) {
19.             const el2 = arr2[j];
20.             if (el1 === el2) return true;
21.         }
22.     }
23.     return false;
24.   }
25.   // The easiest way to understand polynomial time complexity is with nested loops.
26.   // An algorithm that requires two levels of looping over an input would be O(n^2).
27.
28. Doubler: linear run time complexity 0(n)
29.   function doubleArrayValues(array) {
30.     for (let i=0; i<array.length; i++) {
31.         array[i] *= 2;
32.     }
33.     return array;
34.   }
35.   //
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy.

Top