<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ThinkChat2.0新版上線,更智能更精彩,支持會話、畫圖、視頻、閱讀、搜索等,送10W Token,即刻開啟你的AI之旅 廣告
                # 產品數組難題 > 原文: [https://www.geeksforgeeks.org/a-product-array-puzzle/](https://www.geeksforgeeks.org/a-product-array-puzzle/) 給定 n 個整數的數組 arr [],構造乘積數組 prod [](大小相同),以使 prod [i]等于 arr []的所有元素(除 arr [i]之外)的乘積。 在`O(n)`時間中解決不帶除算符的**。** **示例**: ``` Input: arr[] = {10, 3, 5, 6, 2} Output: prod[] = {180, 600, 360, 300, 900} 3 * 5 * 6 * 2 product of other array elements except 10 is 180 10 * 5 * 6 * 2 product of other array elements except 3 is 600 10 * 3 * 6 * 2 product of other array elements except 5 is 360 10 * 3 * 5 * 2 product of other array elements except 6 is 300 10 * 3 * 6 * 5 product of other array elements except 2 is 900 Input: arr[] = {1, 2, 3, 4, 5} Output: prod[] = {120, 60, 40, 30, 24 } 2 * 3 * 4 * 5 product of other array elements except 1 is 120 1 * 3 * 4 * 5 product of other array elements except 2 is 60 1 * 2 * 4 * 5 product of other array elements except 3 is 40 1 * 2 * 3 * 5 product of other array elements except 4 is 30 1 * 2 * 3 * 4 product of other array elements except 5 is 24 ``` **樸素的解決方案**: **方法**:創建兩個額外的空間,即兩個額外的數組以存儲從開始到索引的所有數組元素的乘積,另一個數組則存儲從數組末尾開始的所有數組元素的乘積。 該索引的數組。 要獲得不包含該索引的乘積,請將前綴乘積(直到索引 i-1)乘以后綴乘積(直到索引 i + 1)。 **算法**: 1. 創建兩個長度為 *n* 的數組*前綴*和*后綴*,即原始數組的長度,初始化*前綴[0] = 1* 和*后綴[n-1] = 1* ,以及另一個用于存儲乘積的數組。 2. 從第二個索引到結束遍歷數組。 3. 對于每個索引 *i* ,將*前綴[i]* 更新為 *prefix [i] =前綴[i-1] * array [i-1]* ,即存儲 從數組開始到乘積最高為 *i-1* 索引。 4. 從倒數第二個索引開始遍歷數組。 5. 對于每個索引 *i* ,將*后綴[i]* 更新為*后綴[i] =后綴[i + 1] * array [i + 1]* ,即存儲 從數組末尾開始直到 *i + 1* 索引的乘積 6. 從頭到尾遍歷數組。 7. 對于每個索引 *i* ,輸出將為 *prefix [i] *后綴[i]* ,即該元素之外的數組元素的乘積。 ## C++ ```cpp // C++ implementation of above approach #include <bits/stdc++.h> using namespace std; /* Function to print product array? for a given array arr[] of size n */ void productArray(int arr[], int n) { ????// Base case ????if (n == 1) { ????????cout << 0; ????????return; ????} ????/* Allocate memory for temporary? arrays left[] and right[] */ ????int* left = new int[sizeof(int) * n]; ????int* right = new int[sizeof(int) * n]; ????/* Allocate memory for the product array */ ????int* prod = new int[sizeof(int) * n]; ????int i, j; ????/* Left most element of left? array is always 1 */ ????left[0] = 1; ????/* Rightmost most element of right? array is always 1 */ ????right[n - 1] = 1; ????/* Construct the left array */ ????for (i = 1; i < n; i++) ????????left[i] = arr[i - 1] * left[i - 1]; ????/* Construct the right array */ ????for (j = n - 2; j >= 0; j--) ????????right[j] = arr[j + 1] * right[j + 1]; ????/* Construct the product array using? ????????left[] and right[] */ ????for (i = 0; i < n; i++) ????????prod[i] = left[i] * right[i]; ????/* print the constructed prod array */ ????for (i = 0; i < n; i++) ????????cout << prod[i] << " "; ????return; } /* Driver code*/ int main() { ????int arr[] = { 10, 3, 5, 6, 2 }; ????int n = sizeof(arr) / sizeof(arr[0]); ????cout << "The product array is: \n"; ????productArray(arr, n); } // This is code is contributed by rathbhupendra ``` ## C ``` #include <stdio.h> #include <stdlib.h> /* Function to print product array? for a given array arr[] of size n */ void productArray(int arr[], int n) { ????// Base case ????if (n == 1) { ????????printf("0"); ????????return; ????} ????/* Allocate memory for temporary? arrays left[] and right[] */ ????int* left = (int*)malloc( ????????sizeof(int) * n); ????int* right = (int*)malloc( ????????sizeof(int) * n); ????/* Allocate memory for the product array */ ????int* prod = (int*)malloc( ????????sizeof(int) * n); ????int i, j; ????/* Left most element of left array? is always 1 */ ????left[0] = 1; ????/* Rightmost most element of right? array is always 1 */ ????right[n - 1] = 1; ????/* Construct the left array */ ????for (i = 1; i < n; i++) ????????left[i] = arr[i - 1] * left[i - 1]; ????/* Construct the right array */ ????for (j = n - 2; j >= 0; j--) ????????right[j] = arr[j + 1] * right[j + 1]; ????/* Construct the product array using? ????left[] and right[] */ ????for (i = 0; i < n; i++) ????????prod[i] = left[i] * right[i]; ????/* print the constructed prod array */ ????for (i = 0; i < n; i++) ????????printf("%d ", prod[i]); ????return; } /* Driver program to test above functions */ int main() { ????int arr[] = { 10, 3, 5, 6, 2 }; ????int n = sizeof(arr) / sizeof(arr[0]); ????printf("The product array is: \n"); ????productArray(arr, n); ????getchar(); } ``` ## Java ```java class ProductArray { ????/* Function to print product array? ????for a given array arr[] of size n */ ????void productArray(int arr[], int n) ????{ ????????// Base case ????????if (n == 1) { ????????????System.out.print(0); ????????????return; ????????} ????????// Initialize memory to all arrays ????????int left[] = new int[n]; ????????int right[] = new int[n]; ????????int prod[] = new int[n]; ????????int i, j; ????????/* Left most element of left array? is always 1 */ ????????left[0] = 1; ????????/* Rightmost most element of right? array is always 1 */ ????????right[n - 1] = 1; ????????/* Construct the left array */ ????????for (i = 1; i < n; i++) ????????????left[i] = arr[i - 1] * left[i - 1]; ????????/* Construct the right array */ ????????for (j = n - 2; j >= 0; j--) ????????????right[j] = arr[j + 1] * right[j + 1]; ????????/* Construct the product array using? ????????left[] and right[] */ ????????for (i = 0; i < n; i++) ????????????prod[i] = left[i] * right[i]; ????????/* print the constructed prod array */ ????????for (i = 0; i < n; i++) ????????????System.out.print(prod[i] + " "); ????????return; ????} ????/* Driver program to test the aboe function */ ????public static void main(String[] args) ????{ ????????ProductArray pa = new ProductArray(); ????????int arr[] = { 10, 3, 5, 6, 2 }; ????????int n = arr.length; ????????System.out.println("The product array is : "); ????????pa.productArray(arr, n); ????} } // This code has been contributed by Mayank Jaiswal ``` ## Python3 ```py # Python implementation of the above approach? # Function to print product array for a given array? # arr[] of size n? def productArray(arr, n):? ????# Base case ????if(n == 1): ????????print(0) ????????return ????# Allocate memory for temporary arrays left[] and right[]? ????left = [0]*n? ????right = [0]*n? ????# Allocate memory for the product array? ????prod = [0]*n? ????# Left most element of left array is always 1? ????left[0] = 1 ????# Rightmost most element of right array is always 1? ????right[n - 1] = 1 ????# Construct the left array? ????for i in range(1, n):? ????????left[i] = arr[i - 1] * left[i - 1]? ????# Construct the right array? ????for j in range(n-2, -1, -1):? ????????right[j] = arr[j + 1] * right[j + 1]? ????# Construct the product array using? ????# left[] and right[]? ????for i in range(n):? ????????prod[i] = left[i] * right[i]? ????# print the constructed prod array? ????for i in range(n):? ????????print(prod[i], end =' ')? # Driver code? arr = [10, 3, 5, 6, 2]? n = len(arr)? print("The product array is:")? productArray(arr, n)? # This code is contributed by ankush_953? ``` ## C# ```cs using System; class GFG { ????/* Function to print product array? ????for a given array arr[] of size n */ ????static void productArray(int[] arr, int n) ????{ ????????// Base case ????????if (n == 1) { ????????????Console.Write(0); ????????????return; ????????} ????????// Initialize memory to all arrays ????????int[] left = new int[n]; ????????int[] right = new int[n]; ????????int[] prod = new int[n]; ????????int i, j; ????????/* Left most element of left array? ????????is always 1 */ ????????left[0] = 1; ????????/* Rightmost most element of right? ????????array is always 1 */ ????????right[n - 1] = 1; ????????/* Construct the left array */ ????????for (i = 1; i < n; i++) ????????????left[i] = arr[i - 1] * left[i - 1]; ????????/* Construct the right array */ ????????for (j = n - 2; j >= 0; j--) ????????????right[j] = arr[j + 1] * right[j + 1]; ????????/* Construct the product array using? ????????left[] and right[] */ ????????for (i = 0; i < n; i++) ????????????prod[i] = left[i] * right[i]; ????????/* print the constructed prod array */ ????????for (i = 0; i < n; i++) ????????????Console.Write(prod[i] + " "); ????????return; ????} ????/* Driver program to test the aboe function */ ????public static void Main() ????{ ????????int[] arr = { 10, 3, 5, 6, 2 }; ????????int n = arr.Length; ????????Console.Write("The product array is :\n"); ????????productArray(arr, n); ????} } // This code is contributed by nitin mittal. ``` ## PHP ```php <?php? // Function to print product? // array for a given array? // arr[] of size n? function productArray($arr, $n)? {? ????// Base case ????if($n == 1) { ????????echo "0"; ????????return; ????} ????// Initialize memory? ????// to all arrays? ????$left = array();? ????$right = array();? ????$prod = array();? ????$i; $j;? ????// Left most element of? ????// left array is always 1? ????$left[0] = 1;? ????// Rightmost most element of? ????// right array is always 1? ????$right[$n - 1] = 1;? ????// Construct the left array? ????for ($i = 1; $i < $n; $i++)? ????????$left[$i] = $arr[$i - 1] *? ????????????????????$left[$i - 1];? ????// Construct the right array? ????for ($j = $n - 2; $j >= 0; $j--)? ????????$right[$j] = $arr[$j + 1] *? ????????????????????$right[$j + 1];? ????// Construct the product array? ????// using left[] and right[]? ????for ($i = 0; $i < $n; $i++)? ????????$prod[$i] = $left[$i] *? ????????????????????$right[$i];? ????// print the constructed prod array? ????for ($i = 0; $i < $n; $i++)? ????????echo $prod[$i], " ";? ????return;? }? // Driver Code? $arr = array(10, 3, 5, 6, 2);? $n = count($arr);? echo "The product array is : \n";? productArray($arr, $n);? // This code has been contributed by anuj_67.? ?>? ``` **輸出**: ``` The product array is : 180 600 360 300 900 ``` **復雜度分析**: * **時間復雜度**:`O(n)`。 數組需要遍歷 3 次,因此時間復雜度為`O(n)`。 * **空間復雜度**:`O(n)`。 需要兩個額外的數組和一個用于存儲輸出的數組,因此空間復雜度為`O(n)` **注意**:可以優化上述方法以在空間復雜度`O(1)`中工作。 感謝 Dileep 提供以下解決方案。 **有效解決方案**: **方法**:在先前的解決方案中,創建了兩個額外的數組來存儲前綴和后綴,在此解決方案中,將前綴和后綴乘積存儲在輸出數組(或乘積數組)本身中。 從而減少了所需的空間。 **Algorithm:** 1. 創建一個數組*乘積*,并將其值初始化為 1,并將變量 *temp* = 1。 2. 從頭到尾遍歷數組。 3. 對于每個索引 *i* ,將*乘積[i]* 更新為*乘積[i] = temp* 和 *temp = temp * array [i]* , 即從數組開始將產品存儲到 *i-1* 索引。 4. 初始化 temp = 1 并從最后一個索引開始遍歷數組。 5. 對于每個索引 *i* ,將*產品[i]* 更新為*產品[i] =產品[i] * temp* 和 *temp = temp * array [i ]* ,即乘以從數組末尾到 *i + 1* 索引的乘積。 6. 打印產品數組。 ## C++ ``` // C++ implementation of above approach #include <bits/stdc++.h> using namespace std; /* Function to print product array? for a given array arr[] of size n */ void productArray(int arr[], int n) { ????// Base case ????if (n == 1) { ????????cout << 0; ????????return; ????} ????int i, temp = 1; ????/* Allocate memory for the product array */ ????int* prod = new int[(sizeof(int) * n)]; ????/* Initialize the product array as 1 */ ????memset(prod, 1, n); ????/* In this loop, temp variable contains product of? ???????elements on left side excluding arr[i] */ ????for (i = 0; i < n; i++) { ????????prod[i] = temp; ????????temp *= arr[i]; ????} ????/* Initialize temp to 1? ????for product on right side */ ????temp = 1; ????/* In this loop, temp variable contains product of? ???????elements on right side excluding arr[i] */ ????for (i = n - 1; i >= 0; i--) { ????????prod[i] *= temp; ????????temp *= arr[i]; ????} ????/* print the constructed prod array */ ????for (i = 0; i < n; i++) ????????cout << prod[i] << " "; ????return; } // Driver Code int main() { ????int arr[] = { 10, 3, 5, 6, 2 }; ????int n = sizeof(arr) / sizeof(arr[0]); ????cout << "The product array is: \n"; ????productArray(arr, n); } // This code is contributed by rathbhupendra ``` ## Java ```java class ProductArray { ????void productArray(int arr[], int n) ????{ ????????// Base case ????????if (n == 1) { ????????????System.out.print("0"); ????????????return; ????????} ????????int i, temp = 1; ????????/* Allocate memory for the product array */ ????????int prod[] = new int[n]; ????????/* Initialize the product array as 1 */ ????????for (int j = 0; j < n; j++) ????????????prod[j] = 1; ????????/* In this loop, temp variable contains product of ???????????elements on left side excluding arr[i] */ ????????for (i = 0; i < n; i++) { ????????????prod[i] = temp; ????????????temp *= arr[i]; ????????} ????????/* Initialize temp to 1 for product on right side */ ????????temp = 1; ????????/* In this loop, temp variable contains product of ???????????elements on right side excluding arr[i] */ ????????for (i = n - 1; i >= 0; i--) { ????????????prod[i] *= temp; ????????????temp *= arr[i]; ????????} ????????/* print the constructed prod array */ ????????for (i = 0; i < n; i++) ????????????System.out.print(prod[i] + " "); ????????return; ????} ????/* Driver program to test above functions */ ????public static void main(String[] args) ????{ ????????ProductArray pa = new ProductArray(); ????????int arr[] = { 10, 3, 5, 6, 2 }; ????????int n = arr.length; ????????System.out.println("The product array is : "); ????????pa.productArray(arr, n); ????} } // This code has been contributed by Mayank Jaiswal ``` ## Python3 ```py # Python3 program for A Product Array Puzzle def productArray(arr, n): ????# Base case ????if n == 1: ????????print(0) ????????return ????i, temp = 1, 1 ????# Allocate memory for the product array? ????prod = [1 for i in range(n)] ????# Initialize the product array as 1? ????# In this loop, temp variable contains product of ????# elements on left side excluding arr[i]? ????for i in range(n): ????????prod[i] = temp ????????temp *= arr[i] ????# Initialize temp to 1 for product on right side? ????temp = 1 ????# In this loop, temp variable contains product of ????# elements on right side excluding arr[i]? ????for i in range(n - 1, -1, -1): ????????prod[i] *= temp ????????temp *= arr[i] ????# Print the constructed prod array? ????for i in range(n): ????????print(prod[i], end = " ") ????return # Driver Code arr = [10, 3, 5, 6, 2] n = len(arr) print("The product array is: n") productArray(arr, n) # This code is contributed by mohit kumar ``` ## C# ``` using System; class GFG { ????static void productArray(int[] arr, int n) ????{ ????????// Base case ????????if (n == 1) { ????????????Console.Write(0); ????????????return; ????????} ????????int i, temp = 1; ????????/* Allocate memory for the product ????????array */ ????????int[] prod = new int[n]; ????????/* Initialize the product array as 1 */ ????????for (int j = 0; j < n; j++) ????????????prod[j] = 1; ????????/* In this loop, temp variable contains ????????product of elements on left side ????????excluding arr[i] */ ????????for (i = 0; i < n; i++) { ????????????prod[i] = temp; ????????????temp *= arr[i]; ????????} ????????/* Initialize temp to 1 for product on? ????????right side */ ????????temp = 1; ????????/* In this loop, temp variable contains ????????product of elements on right side? ????????excluding arr[i] */ ????????for (i = n - 1; i >= 0; i--) { ????????????prod[i] *= temp; ????????????temp *= arr[i]; ????????} ????????/* print the constructed prod array */ ????????for (i = 0; i < n; i++) ????????????Console.Write(prod[i] + " "); ????????return; ????} ????/* Driver program to test above functions */ ????public static void Main() ????{ ????????int[] arr = { 10, 3, 5, 6, 2 }; ????????int n = arr.Length; ????????Console.WriteLine("The product array is : "); ????????productArray(arr, n); ????} } // This code is contributed by nitin mittal. ``` ## PHP ```php <?php // PHP program for? // A Product Array Puzzle function productArray($arr, $n)? ????{ ????????// Base case ????????if ($n == 1) { ????????????echo "0"; ????????????return; ????????} ????????$i; $temp = 1; ????????/* Allocate memory for? ???????????the productarray */ ????????$prod = array(); ????????/* Initialize the product? ???????????array as 1 */ ????????for( $j = 0; $j < $n; $j++) ????????????$prod[$j] = 1; ????????/* In this loop, temp? ???????????variable contains ???????????product of elements ???????????on left side ???????????excluding arr[i] */ ????????for ($i = 0; $i < $n; $i++)? ????????{ ????????????$prod[$i] = $temp; ????????????$temp *= $arr[$i]; ????????} ????????/* Initialize temp to 1? ???????????for product on right ???????????side */ ????????$temp = 1; ????????/* In this loop, temp? ???????????variable contains ???????????product of elements? ???????????on right side? ???????????excluding arr[i] */ ????????for ($i = $n - 1; $i >= 0; $i--)? ????????{ ????????????$prod[$i] *= $temp; ????????????$temp *= $arr[$i]; ????????} ????????/* print the constructed ???????????prod array */ ????????for ($i = 0; $i < $n; $i++) ????????????echo $prod[$i], " "; ????????return; ????} ????????// Driver Code???? ????????$arr = array(10, 3, 5, 6, 2); ????????$n = count($arr); ????????echo "The product array is : \n"; ????????productArray($arr, $n); // This code is contributed by anuj_67\. ?> ``` **輸出**: ``` The product array is : 180 600 360 300 900 ``` **復雜度分析**: * **時間復雜度**:`O(n)`。 原始數組僅需要遍歷一次,因此時間復雜度是恒定的。 * **空間復雜度**:`O(n)`。 即使刪除了多余的數組,空間復雜度仍然為`O(n)`,因為仍然需要乘積數組。 [**產品數組拼圖| 集 2(`O(1)`空間)**](https://www.geeksforgeeks.org/product-array-puzzle-set-2-o1-space/) **相關問題**: [根據數組中所有元素的異或構造,將同一索引處的元素除外](https://www.geeksforgeeks.org/construct-an-array-from-xor-of-all-elements-of-array-except-element-at-same-index/) 如果您發現上述代碼/算法有誤,請寫注釋,或者找到解決同一問題的更好方法。
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看