Skip to content

Instantly share code, notes, and snippets.

Created November 7, 2014 15:55
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 anonymous/6c83894ecee7016a771d to your computer and use it in GitHub Desktop.
Save anonymous/6c83894ecee7016a771d to your computer and use it in GitHub Desktop.
GetProductsOfAllIntsExceptAtIndex
import java.util.Arrays;
public class Playground {
public static void main(String[] args) {
int[] testArray = new int[] {1, 7, 3, 4};
System.out.println(Arrays.toString(testArray));
//GetProductsOfAllIntsExceptAtIndex(testArray);
System.out.println(Arrays.toString(GetProductsOfAllIntsExceptAtIndex(testArray)));
}
public static int[] GetProductsOfAllIntsExceptAtIndex(int[] array) {
int[] productArray = new int[array.length];
for (int i = 0; i < array.length; i++)
{
productArray[i] = GetProductsWithSkippedIndex(array, i);
}
return productArray;
}
public static int GetProductsWithSkippedIndex(int[] array, int indexToSkip) {
int product = 1;
for (int i= 0; i < array.length; i++)
{
if (i != indexToSkip)
{
product = product * array[i];
}
}
return product;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment