📜  产品阵列拼图 |设置 3

📅  最后修改于: 2021-09-07 02:51:58             🧑  作者: Mango

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

例子:

方法:想法是使用 log() 和 exp() 函数而不是 l og10()和 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的计数加
  • 遍历数组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.


Javascript


输出:
180 600 360 300 900

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

替代方法:有关替代方法,请参阅本文之前的帖子:

  • 产品阵列拼图
  • 产品阵列拼图 | 2套

如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live