数组,其值是每个其他整数的乘积

时间:2014-03-26 20:28:00

标签: java arrays multiplication

通过电话我被问到以下面试问题:

Given an array of integers, produce an array whose values are the product of every other integer 

excluding the current index. 

Example: 

[4, 3, 2, 8] -> [3*2*8, 4*2*8, 4*3*8, 4*3*2] -> [48, 64, 96, 24]

我给出了以下答案

import java.math.BigInteger;
import java.util.Arrays;

public class ProductOfAnArray {

    public static void main(String[] args) {

        try {
            System.out.println(Arrays.toString(ProductOfAnArray
                    .calcArray(new int[] { 4, 3, 2, 8 })));
            System.out.println(Arrays.toString(ProductOfAnArray
                    .calcArray(new int[] { 4, 0, 2, 8 })));
            System.out.println(Arrays.toString(ProductOfAnArray
                    .calcArray(new int[] { 4, 0, 2, 0 })));
            System.out.println(Arrays.toString(ProductOfAnArray
                    .calcArray(new int[] {})));
            System.out
                    .println(Arrays.toString(ProductOfAnArray
                            .calcArray(new int[] { 4, 3, 2, 8, 3, 2, 4, 6, 7,
                                    3, 2, 4 })));
            System.out
                    .println(Arrays.toString(ProductOfAnArray
                            .calcArray(new int[] { 4, 3, 2, 8, 3, 2, 4, 6, 7,
                                    3, 2, 4 })));
            System.out.println(Arrays.toString(ProductOfAnArray
                    .calcArray(new int[] { 4432432, 23423423, 34234, 23423428,
                            4324243, 24232, 2342344, 64234234, 4324247,
                            4234233, 234422, 234244 })));
        } catch (Exception e) {
            // debug exception here and log.
        }
    }

    /*
     * Problem: Given an array of integers, produce an array whose values are
     * the product of every other integer excluding the current index.
     * 
     * Assumptions. Input array cannot be modified. input is an integer array
     * "produce an array" - type not specified for output array
     * 
     * Logic explanation:
     * 
     * Assume we have array [a,b,c,d] Let multiple be multiple of each element
     * in array. Hence multiple = 0 if one of the element is 0; To produce the
     * output. Ans at i -> multiple divided by the value at i. if 2 numbers are
     * 0 then entire output will be 0 because atleast one 0 will be a multiple
     * if 1 number is 0 then every thing else will be 0 except that index whole
     * value is to be determined
     * 
     */
    public static BigInteger[] calcArray(final int[] inp) throws Exception {
        if (inp == null)
            throw new Exception("input is null");

        int cnt = 0;
        BigInteger multiple = new BigInteger("1");
        boolean foundZero = false;

        for (int i : inp) {
            if (i == 0) {
                cnt++;
                foundZero = true;
                if (cnt < 2)
                    continue;
                else
                    break;
            }
            multiple = multiple.multiply(BigInteger.valueOf(i));
        }

        BigInteger ans[] = new BigInteger[inp.length];

        for (int i = 0; i < inp.length; i++) {
            if (foundZero) {
                if (cnt < 2) {
                    ans[i] = (inp[i] == 0) ? multiple : new BigInteger("0");
                } else {
                    ans[i] = new BigInteger("0");
                }
            } else {
                ans[i] = multiple.divide(BigInteger.valueOf(inp[i]));
            }
        }
        return ans;
    }

}

但我没被选中。我希望得到我的答案的反馈,看看有什么不对。

谢谢。

4 个答案:

答案 0 :(得分:1)

我过去以同样的方式尝试过。没选择:)。 我尝试在第一个循环中捕获零索引。并简单地将产品分配给该索引(我使用双数组,默认值为0) - 所以如果找到零,则不需要再次迭代。

在第一个循环中检查产品是无穷大Double.isInfinite,如果是,则打破循环 - 没有找到剩余产品的点(假设输入是高数量的大数量)

public static double[] arrayProducts(int[] input) {
    int length = input.length;
    double product = 1;
    boolean zeroFound = false;
    int zeroIndex = 0;
    double[] output = null;
    for (int i = 0; i < length; i++) {
        if (input[i] == 0) {
            if (zeroFound) {
                throw new ProductArrayException(0, true, zeroIndex,
                        ZERO_FOUND_EXPECTION);
            }
            zeroFound = true;
            zeroIndex = i;
        } else {
            product = product * input[i];
            if (Double.isInfinite(product)) {
                throw new ProductArrayException(0, true, zeroIndex,
                        INFINITY_FOUND_EXPECTION);
            }
        }
    }

    if (zeroFound) {
        throw new ProductArrayException(product, false, zeroIndex,
                ZERO_FOUND_EXPECTION);
    } else {
        output = new double[length];
        for (int i = 0; i < length; i++) {
            output[i] = product / input[i];
        }
    }
    return output;
}

答案 1 :(得分:0)

这可能是面试官追求的解决方案:

    int[] arr = new int[]{4,3,2,8};
    int[] newarr = new int[arr.length];

    int product = 1;
    for (int i=0; i < arr.length; i++) {
        product = product * arr[i];
    }

    for (int i=0; i < arr.length; i++) {
        newarr[i] = product / arr[i];
        System.out.print(newarr[i] + " ");
    }

答案 2 :(得分:0)

给定整数数组arr[]

int[]   arr = { 4, 3, 2, 8 };
int[]   prod = new int[arr.length];

for (int i = 0;  i < arr.length;  i++)
{
    prod[i] = 1;
    for (int j = 0;  j < arr.length;  j++)
        if (j != i)    // Exclude element at the current index
            prod[i] *= arr[j];
}

生成的产品数组为prod[]

答案 3 :(得分:0)

通常对于这个问题,不计算除法的答案,它必须是一个有效的时间和空间算法。

看下面产生O(n)时间&amp; O(n)空间

public int[] otherIndexProduct(int array[]) {

    int results[] = new int[array.length];

    results[0] = 1;

    for (int i = 0; i < array.length - 1; i++) {

        results[i + 1] = results[i] * array[i];
    }

    int before = 1;

    for (int i = array.length - 1; i >= 0; i--) {

        results[i] *= before;
        before *= array[i];
    }

    return results;
}

有一种有趣的另一种方法来实现递归,它也可以在不使用其他数组的情况下更新输入数组

public int product(int array[], int post, int index) {

    int prev = 1;

    if (index < array.length) {

        prev = product(array, post * array[index], index + 1);
        int current = array[index];
        array[index] = post * prev;
        prev *= current;
    }

    return prev;
}