public void searchForValue(int valueToFind, int[] arr, int sizeOfArray) {
for (int it = 0; it < sizeOfArray ; it++) {
if (arr[it] == valueToFind) {
System.out.println("Success");
break;
}
}
if (it == sizeOfArray)
System.out.println("Failure");
return;
}
T : O(n)
S : O(1)
function indexOf(array, element, offset = 0) {
// split array in half
const half = parseInt(array.length / 2);
const current = array[half];
if(current === element) {
return offset + half;
} else if(element > current) {
const right = array.slice(half);
return indexOf(right, element, offset + half);
} else {
const left = array.slice(0, half)
return indexOf(left, element, offset);
}
}
T : O(log n)
S : O(log n)
function hasDuplicates(n) {
for (let outer = 0; outer < n.length; outer++) {
for (let inner = 0; inner < n.length; inner++) {
if(outer === inner) continue;
if(n[outer] === n[inner]) {
return true;
}
}
}
return false;
}
T : O(n^2)
S : O(1)
public int fibo(int n) {
System.out.println("Calling fibonacci with input: " + n);
if (n < 2) {
return n;
}
return fibo(n - 1) + fibo(n - 2);
}
T : O(2^n)
S : O(n) (Why? Consider the recursion call stack size also)