Computational Complexity: O(n)
Converting arr1 to a set takes O(m) time, where m is the length of arr1.
Checking each element in arr2 takes O(n) time, where n is the length of arr2.
Set membership checking is O(1) on average.
| function createArrayOfFunctions(y) { | |
| var arr = []; | |
| for (var i = 0; i < y; i++) { | |
| // Use an IIFE to capture the current value of i | |
| arr[i] = (function(index) { | |
| return function(x) { | |
| return x + index; | |
| }; | |
| })(i); | |
| } |
| def nextFibonacci(numbers): | |
| results = [] | |
| for num in numbers: | |
| a, b = 0, 1 | |
| # Generate Fibonacci numbers until we find one > num | |
| while b <= num: | |
| a, b = b, a + b | |
| results.append(b) | |
Computational Complexity: O(n)
Converting arr1 to a set takes O(m) time, where m is the length of arr1.
Checking each element in arr2 takes O(n) time, where n is the length of arr2.
Set membership checking is O(1) on average.
| def isSubset(arr1, arr2): | |
| # Convert arr1 to a set for O(1) lookups | |
| set1 = set(arr1) | |
| # Check if every element in arr2 exists in set1 | |
| for elem in arr2: | |
| if elem not in set1: | |
| return False | |
| return True |