Skip to content

Instantly share code, notes, and snippets.

@karlmutch
Created May 2, 2015 19:03
Show Gist options
  • Save karlmutch/221e705051bb56e4cad0 to your computer and use it in GitHub Desktop.
Save karlmutch/221e705051bb56e4cad0 to your computer and use it in GitHub Desktop.
This problem comes from a Google+ group and was described as * * You have an array of integers, and for each index you want to find the product of every integer except * the integer at that index. Write a function get_products_of_all_ints_except_at_index() that takes an * array of integers and returns an array of the products.
public static BigInteger [] get_products_of_all_ints_except_at_index(BigInteger [] input)
{
ArrayList<BigInteger> productResults = new ArrayList<BigInteger>(input.length);
BigInteger masterProduct = BigInteger.ONE;
for (BigInteger anItem : input) {
masterProduct = masterProduct.multiply(anItem);
}
for (BigInteger anItem : input) {
productResults.add(masterProduct.divide(anItem));
}
return(productResults.toArray(new BigInteger[input.length]));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment