Hey, I'm just looking at some coding questions that google have asked before in their interviews and trying them out etc.
Basically there are a few questions where they ask you to do an algorithm to solve in O(n) or O(log(n)) etc.
But it has been so long since I did this in college I can't remember how to get the correct Notation to see if my algorithm is correct.
Anyway I have the question below
There is an array A[N] of N numbers. You have to compose an array Output[N] such that Output will be equal to multiplication of all the elements of A[N] except A. For example Output[0] will be multiplication of A[1] to A[N-1] and Output[1] will be multiplication of A[0] and from A[2] to A[N-1]. Solve it without division operator and in O(n).
I implemented this in a couple of minutes and I am just wondering if someone could help me figure out how to convert the complexity into Big O notation.
Thanks
private ArrayList setArray(ArrayList input) {
ArrayList output= new ArrayList();
float full=1;
for (int i = 0; i<input.size(); i++) {
full = full*Integer.parseInt(input.get(i).toString());
}
for (int i = 0; i<input.size(); i++) {
output.set(i, divide(full,Integer.parseInt(input.get(i).toString())));
}
return output;
}
private float divide(float whole, float divider) {
float targetRemainder = whole%divider, currentRemainder=whole;
float result=0;
while (currentRemainder!=targetRemainder) {
currentRemainder = currentRemainder-divider;
result ++;
}
result=result + currentRemainder;
return result;
}