|
| 1 | +package me.ramswaroop.arrays; |
| 2 | + |
| 3 | +import java.util.Arrays; |
| 4 | + |
| 5 | +/** |
| 6 | + * Created by IntelliJ IDEA. |
| 7 | + * |
| 8 | + * @author: ramswaroop |
| 9 | + * @date: 8/15/15 |
| 10 | + * @time: 9:17 PM |
| 11 | + */ |
| 12 | +public class ProductArrayPuzzle { |
| 13 | + |
| 14 | + /** |
| 15 | + * Construct a Product Array prod[] (of same size) such that prod[i] is |
| 16 | + * equal to the product of all the elements of arr[] except arr[i]. Solve |
| 17 | + * it without division operator and in O(n). |
| 18 | + * <p/> |
| 19 | + * You can do this by taking two arrays one containing the products from |
| 20 | + * left to right elements and other containing the products from right to |
| 21 | + * left elements and then finally multiplying those two arrays gives you |
| 22 | + * the answer. But the auxiliary space complexity of this method is O(n) |
| 23 | + * as well as the space complexity is O(n). |
| 24 | + * <p/> |
| 25 | + * The below method is the optimized way to do this in O(1) auxiliary space |
| 26 | + * but the space complexity is O(n). |
| 27 | + * <p/> |
| 28 | + * AUXILIARY SPACE is extra space or temporary space used by the algorithm, |
| 29 | + * which is mostly used in algorithm where we use swapping or temporary variables. |
| 30 | + * <p/> |
| 31 | + * SPACE COMPLEXITY means total space taken by the algorithm with respect to |
| 32 | + * input size.Space complexity calculated by both auxiliary space and space used |
| 33 | + * by the input. |
| 34 | + * <p/> |
| 35 | + * For example - If we want to compare standard sorting algorithm on the basis of |
| 36 | + * then auxiliary space would be better criteria than space complexity. Merge sort |
| 37 | + * uses O(n) auxiliary space,where Insertion sort and Heap sort uses O(1) auxiliary |
| 38 | + * space. Merge sort requires Ω(n) but Heap sort requires only a constant amount. |
| 39 | + * Space complexity of all these sorting algorithm is O(n) though. |
| 40 | + * |
| 41 | + * @param a |
| 42 | + * @return |
| 43 | + */ |
| 44 | + public static int[] getProductArray(int[] a) { |
| 45 | + int[] prod = new int[a.length]; |
| 46 | + |
| 47 | + // prod array consists of products of the elements |
| 48 | + prod[0] = 1; |
| 49 | + |
| 50 | + // fill prod with products of elements from left to right excluding current element |
| 51 | + for (int i = 1; i < a.length; i++) { |
| 52 | + prod[i] = a[i - 1] * prod[i - 1]; |
| 53 | + } |
| 54 | + |
| 55 | + int temp = 1; |
| 56 | + // fill prod with products of elements from right to left excluding current element |
| 57 | + for (int i = a.length - 1; i >= 0; i--) { |
| 58 | + prod[i] *= temp; |
| 59 | + temp *= a[i]; |
| 60 | + } |
| 61 | + |
| 62 | + // final prod array is the answer |
| 63 | + return prod; |
| 64 | + } |
| 65 | + |
| 66 | + public static void main(String a[]) { |
| 67 | + System.out.println(Arrays.toString(getProductArray(new int[]{10, 3, 5, 6, 2}))); |
| 68 | + System.out.println(Arrays.toString(getProductArray(new int[]{0, 0}))); |
| 69 | + System.out.println(Arrays.toString(getProductArray(new int[]{1}))); |
| 70 | + } |
| 71 | +} |
0 commit comments