Given an input integer array arr. For all elements of the array, find the product of all elements except current element.
| The java method should accept following input parameters: arr (int array).
|
| Initialize array called arrayOfProducts with length-arr.length, which will be used to store the products of all elements except current element.
|
|
While finding products of array elements, if any element is 0, then product of all elements become 0. So, to handle this special case of 0, we should first iterate through arr and find number of 0s in the array.
|
| Iterate through the array using a for loop and find the count of zeros. If number of zeros is 0 find product of all element prodOfElements in the same for loop.
|
| If count of zeroes is more than 1, then populate 0 in all indices of arrayOfProducts.
|
| If count of zeroes is 1, then for the arr index that has zero, populate product of all other index values of arr except idx in arrayOfProducts[idx]. For all other indices, populate 0.
|
| If count of zeroes is 0, then iterate through the arr with a for loop:
For each arr[idx], populate arrayOfProducts[idx] with (prodOfElements/ arr[idx]).
After above for loop ends return arrayOfProducts.
|
Below fully running code can be copied and run on Eclipse or other Java IDEs. Refer the classname in code below. If the class name below is "A", save the code below to a file named A.java before running it.
Be sure to try your own test cases to enhance your understanding !
You can also tweak the code to optimize or add enhancements and custom features.
public class ArrayProductOfAllElementsExceptSelf {
public int[] arrayProductOfAllElementsExceptSelf(int[] arr) throws Exception{
int countOfZeros=0; int indexOfZero=0;
int prodOfElements=1;
int arrayofProducts[]=new int[arr.length];
for(int idx=0; idx < arr.length; idx++) {
if (arr[idx]==0) {
countOfZeros++;
indexOfZero=idx;
}
else {
prodOfElements*=arr[idx];
}
}
for(int idx=0; idx < arr.length; idx++) {
if (countOfZeros==0) {
arrayofProducts[idx]=prodOfElements/arr[idx];
}if (countOfZeros==1) {
if (idx==indexOfZero) {
arrayofProducts[idx]=prodOfElements;
}else {
arrayofProducts[idx]=0;
}
}if (countOfZeros>1) {
arrayofProducts[idx]=0;
}
}
return arrayofProducts;
}
public static void main(String[] args) {
ArrayProductOfAllElementsExceptSelf ap=new ArrayProductOfAllElementsExceptSelf();
int[] arrayofProducts;
try {
int[] intArray1 = {1,2,3,4,5};
printArraySummary(intArray1, "Original Array");
arrayofProducts=ap.arrayProductOfAllElementsExceptSelf(intArray1);
printArraySummary(arrayofProducts, "Array Of Products");
int[] intArray2 = {1,1,1,1,1};
printArraySummary(intArray2, "Original Array");
arrayofProducts=ap.arrayProductOfAllElementsExceptSelf(intArray2);
printArraySummary(arrayofProducts, "Array Of Products");
int[] intArray3 = {0,0,0,0,0};
printArraySummary(intArray3, "Original Array");
arrayofProducts=ap.arrayProductOfAllElementsExceptSelf(intArray3);
printArraySummary(arrayofProducts, "Array Of Products");
int[] intArray4 = {0,1,2,3,4};
printArraySummary(intArray4, "Original Array");
arrayofProducts=ap.arrayProductOfAllElementsExceptSelf(intArray4);
printArraySummary(arrayofProducts, "Array Of Products");
int[] intArray5 = {1,2,3,4,0};
printArraySummary(intArray5, "Original Array");
arrayofProducts=ap.arrayProductOfAllElementsExceptSelf(intArray5);
printArraySummary(arrayofProducts, "Array Of Products");
int[] intArray6 = {0,2,3,4,0};
printArraySummary(intArray6, "Original Array");
arrayofProducts=ap.arrayProductOfAllElementsExceptSelf(intArray6);
printArraySummary(arrayofProducts, "Array Of Products");
int[] intArray7 = {1,0,3,0,5};
printArraySummary(intArray7, "Original Array");
arrayofProducts=ap.arrayProductOfAllElementsExceptSelf(intArray7);
printArraySummary(arrayofProducts, "Array Of Products");
int[] intArray8 = {1,2,0,4,5};
printArraySummary(intArray8, "Original Array");
arrayofProducts=ap.arrayProductOfAllElementsExceptSelf(intArray8);
printArraySummary(arrayofProducts, "Array Of Products");
}catch (Exception exception) {
System.out.print("Exception: "+ exception);
}
}
public static void printArraySummary(int[] intArray, String label) throws Exception {
// Case 1: The input Array is null !!
if (intArray == null) { System.out.println("\n\n Input Array was null !! \n"); return; }
// Case 2: Print input Array by index (first to last)
System.out.println();
System.out.println("************************************************************************");
System.out.print(label+" : ");
int arrayIndex=0;
for (arrayIndex=0; arrayIndex< intArray.length; arrayIndex++) {
System.out.print(intArray[arrayIndex]);
if (arrayIndex< intArray.length-1) {System.out.print(",");}
}
System.out.println();
System.out.println("*************************************************************************");
System.out.println();
Thread.sleep(2000);
return;
}
}