Skip to content

Instantly share code, notes, and snippets.

@rohan-varma
Created September 9, 2016 13:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rohan-varma/b1002da9173735040c34f76626121380 to your computer and use it in GitHub Desktop.
Save rohan-varma/b1002da9173735040c34f76626121380 to your computer and use it in GitHub Desktop.
int* getProductsOfAllIntsExceptAtIndexFaster(int* arr, int len) {
int* beforeIndex = new int[len]; //array that keeps track of products before that index
beforeIndex[0] = 1; // no products before the 0th index
for(int i = 1; i < len ; i ++) {
beforeIndex[i] = beforeIndex[i-1] * arr[i-1]; //multiply by the element at the index before the current one.
}
... }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment