Created
November 7, 2014 15:55
-
-
Save anonymous/6c83894ecee7016a771d to your computer and use it in GitHub Desktop.
GetProductsOfAllIntsExceptAtIndex
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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