📜  产品阵列难题|套装3

📅  最后修改于: 2021-04-17 16:33:42             🧑  作者: Mango

给定一个由N个整数组成的数组arr [] ,任务是构造一个大小相同的乘积数组,而不使用除法运算符( ‘/’ ),以使每个数组元素等于arr []的所有元素的乘积除了arr [i]

例子:

方法:想法是使用log()和exp()函数代替log10()和pow()。以下是关于同一方面的一些观察结果:

  • 假设M是所有数组元素的乘积,则在i位置的输出数组元素将等于M / arr [i]。
  • 可以通过使用对数和exp函数的属性来执行两个数的除法。
    • log(a) - log(b) = log(a/b)
    • exp(log(a/b)) = a/b
    • exp(log(a) - log(b)) = a/b
  • 没有为小于零的数字定义对数函数,因此要分别维护这种情况。

请按照以下步骤解决问题:

  • 初始化两个变量,例如product = 1Z = 1 ,以存储数组和零元素计数的乘积。
  • 如果arr [i]不等于0,则遍历数组并将乘积乘以arr [i] 。否则,将Z的计数增加1
  • 遍历数组arr []并执行以下操作:
    • 如果Z1arr [i]不为零,则将arr [i]更新为arr [i] = 0 继续。
    • 否则,如果Z1arr [i]0,则将arr [i]更新为乘积并继续。
    • 否则,如果Z大于1,则将arr [i]分配为0并继续。
    • 现在,使用上述公式找到abs(product)/ abs(arr [i])的值,并将其存储在变量curr中
    • 如果arr [i]product的值为负,或者arr [i]product为正,则将arr [i]分配为curr
    • 否则,将arr [i]分配为-1 * curr
  • 完成上述步骤后,打印数组arr []

下面是上述方法的实现:

C++
// C++ program for the above approach
#include 
using namespace std;
 
// Fucntion to form product array
// with O(n) time and O(1) space
void productExceptSelf(int arr[],
                       int N)
{
    // Stores the product of array
    int product = 1;
 
    // Stores the count of zeros
    int z = 0;
 
    // Traverse the array
    for (int i = 0; i < N; i++) {
 
        // If arr[i] is not zero
        if (arr[i])
            product *= arr[i];
 
        // If arr[i] is zero then
        // increment count of z by 1
        z += (arr[i] == 0);
    }
 
    // Stores the absolute value
    // of the product
    int a = abs(product), b;
    for (int i = 0; i < N; i++) {
 
        // If Z is equal to 1
        if (z == 1) {
 
            // If arr[i] is not zero
            if (arr[i])
                arr[i] = 0;
 
            // Else
            else
                arr[i] = product;
            continue;
        }
 
        // If count of 0s at least 2
        else if (z > 1) {
 
            // Assign arr[i] = 0
            arr[i] = 0;
            continue;
        }
 
        // Store absoulute value of arr[i]
        int b = abs(arr[i]);
 
        // Find the value of a/b
        int curr = round(exp(log(a) - log(b)));
 
        // If arr[i] and product both
        // are less than zero
        if (arr[i] < 0 && product < 0)
            arr[i] = curr;
 
        // If arr[i] and product both
        // are greater than zero
        else if (arr[i] > 0 && product > 0)
            arr[i] = curr;
 
        // Else
        else
            arr[i] = -1 * curr;
    }
 
    // Traverse the array arr[]
    for (int i = 0; i < N; i++) {
        cout << arr[i] << " ";
    }
}
 
// Driver Code
int main()
{
    int arr[] = { 10, 3, 5, 6, 2 };
    int N = sizeof(arr) / sizeof(arr[0]);
 
    // Function Call
    productExceptSelf(arr, N);
 
    return 0;
}


Java
// Java program to implement
// the above approach
import java.util.*;
 
class GFG{
 
// Fucntion to form product array
// with O(n) time and O(1) space
static void productExceptSelf(int arr[],
                       int N)
{
    // Stores the product of array
    int product = 1;
 
    // Stores the count of zeros
    int z = 0;
 
    // Traverse the array
    for (int i = 0; i < N; i++) {
 
        // If arr[i] is not zero
        if (arr[i] != 0)
            product *= arr[i];
 
        // If arr[i] is zero then
        // increment count of z by 1
        if (arr[i] == 0)
            z += 1;
    }
 
    // Stores the absolute value
    // of the product
    int a = Math.abs(product);
    for (int i = 0; i < N; i++) {
 
        // If Z is equal to 1
        if (z == 1) {
 
            // If arr[i] is not zero
            if (arr[i] != 0)
                arr[i] = 0;
 
            // Else
            else
                arr[i] = product;
            continue;
        }
 
        // If count of 0s at least 2
        else if (z > 1) {
 
            // Assign arr[i] = 0
            arr[i] = 0;
            continue;
        }
 
        // Store absoulute value of arr[i]
        int b = Math.abs(arr[i]);
 
        // Find the value of a/b
        int curr = (int)Math.round(Math.exp(Math.log(a) - Math.log(b)));
 
        // If arr[i] and product both
        // are less than zero
        if (arr[i] < 0 && product < 0)
            arr[i] = curr;
 
        // If arr[i] and product both
        // are greater than zero
        else if (arr[i] > 0 && product > 0)
            arr[i] = curr;
 
        // Else
        else
            arr[i] = -1 * curr;
    }
 
    // Traverse the array arr[]
    for (int i = 0; i < N; i++) {
        System.out.print(arr[i] + " ");
    }
}
 
// Driver Code
public static void main(String args[])
{
    int arr[] = { 10, 3, 5, 6, 2 };
    int N = arr.length;
 
    // Function Call
    productExceptSelf(arr, N);
}
}
 
// This code is contributed by splevel62.


Python3
# Python 3 program for the above approach
import math
 
# Fucntion to form product array
# with O(n) time and O(1) space
def productExceptSelf(arr, N) :
     
    # Stores the product of array
    product = 1
 
    # Stores the count of zeros
    z = 0
 
    # Traverse the array
    for i in range(N):
 
        # If arr[i] is not zero
        if (arr[i] != 0) :
            product *= arr[i]
 
        # If arr[i] is zero then
        # increment count of z by 1
        if(arr[i] == 0):
            z += 1
     
    # Stores the absolute value
    # of the product
    a = abs(product)
    for i in range(N):
 
        # If Z is equal to 1
        if (z == 1) :
 
            # If arr[i] is not zero
            if (arr[i] != 0) :
                arr[i] = 0
 
            # Else
            else :
                arr[i] = product
            continue
         
 
        # If count of 0s at least 2
        elif (z > 1) :
 
            # Assign arr[i] = 0
            arr[i] = 0
            continue
         
 
        # Store absoulute value of arr[i]
        b = abs(arr[i])
 
        # Find the value of a/b
        curr = round(math.exp(math.log(a) - math.log(b)))
 
        # If arr[i] and product both
        # are less than zero
        if (arr[i] < 0 and product < 0):
            arr[i] = curr
 
        # If arr[i] and product both
        # are greater than zero
        elif (arr[i] > 0 and product > 0):
            arr[i] = curr
 
        # Else
        else:
            arr[i] = -1 * curr
     
    # Traverse the array arr[]
    for i in range(N):
        print(arr[i], end = " ")
     
# Driver Code
arr = [ 10, 3, 5, 6, 2 ]
N = len(arr)
 
# Function Call
productExceptSelf(arr, N)
 
# This code is contributed by code_hunt.


C#
// C# program for the above approach
using System;
class GFG
{
     
// Fucntion to form product array
// with O(n) time and O(1) space
static void productExceptSelf(int[] arr,
                       int N)
{
    // Stores the product of array
    int product = 1;
 
    // Stores the count of zeros
    int z = 0;
 
    // Traverse the array
    for (int i = 0; i < N; i++)
    {
 
        // If arr[i] is not zero
        if (arr[i] != 0)
            product *= arr[i];
 
        // If arr[i] is zero then
        // increment count of z by 1
        if (arr[i] == 0)
            z += 1;
    }
 
    // Stores the absolute value
    // of the product
    int a = Math.Abs(product);
    for (int i = 0; i < N; i++)
    {
 
        // If Z is equal to 1
        if (z == 1)
        {
 
            // If arr[i] is not zero
            if (arr[i] != 0)
                arr[i] = 0;
 
            // Else
            else
                arr[i] = product;
            continue;
        }
 
        // If count of 0s at least 2
        else if (z > 1)
        {
 
            // Assign arr[i] = 0
            arr[i] = 0;
            continue;
        }
 
        // Store absoulute value of arr[i]
        int b = Math.Abs(arr[i]);
 
        // Find the value of a/b
        int curr = (int)Math.Round(Math.Exp(Math.Log(a) - Math.Log(b)));
 
        // If arr[i] and product both
        // are less than zero
        if (arr[i] < 0 && product < 0)
            arr[i] = curr;
 
        // If arr[i] and product both
        // are greater than zero
        else if (arr[i] > 0 && product > 0)
            arr[i] = curr;
 
        // Else
        else
            arr[i] = -1 * curr;
    }
 
    // Traverse the array arr[]
    for (int i = 0; i < N; i++)
    {
        Console.Write(arr[i] + " ");
    }
}
 
// Driver Code
public static void Main(String[] args)
{
    int[] arr = { 10, 3, 5, 6, 2 };
    int N = arr.Length;
 
    // Function Call
    productExceptSelf(arr, N);
}
}
 
// This code is contributed by sanjoy_62.


输出:
180 600 360 300 900

时间复杂度: O(N)
辅助空间: O(1)

替代方法:有关替代方法,请参阅本文的先前文章:

  • 产品阵列之谜
  • 产品阵列难题|套装2