<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>

                企業??AI智能體構建引擎,智能編排和調試,一鍵部署,支持知識庫和私有化部署方案 廣告
                # 用上一個和下一個的乘法替換每個數組元素 > 原文: [https://www.geeksforgeeks.org/replace-every-array-element-by-multiplication-of-previous-and-next/](https://www.geeksforgeeks.org/replace-every-array-element-by-multiplication-of-previous-and-next/) 給定一個整數數組,請使用前一個和下一個元素的乘積更新每個元素,但以下例外。 a)第一個元素被替換為`first`和`second`。 b)將`last`元素替換為`last`和倒數第二乘積。 **示例**: ``` Input: arr[] = {2, 3, 4, 5, 6} Output: arr[] = {6, 8, 15, 24, 30} // We get the above output using following // arr[] = {2*3, 2*4, 3*5, 4*6, 5*6} ``` 資料來源:[前 25 個面試問題](https://www.geeksforgeeks.org/top-25-interview-questions/) 一個簡單的解決方案是創建一個輔助數組,將給定數組的內容復制到輔助數組。 最后遍歷輔助數組并使用復制的值更新給定的數組。 此解決方案的時間復雜度為`O(n)`,但需要`O(n)`額外空間。 一個有效的解決方案可以解決`O(n)`時間和`O(1)`空間中的問題。 這個想法是跟蹤循環中的前一個元素。 以下是此想法的實現。 ## C ``` // C++ program to update every array element with // multiplication of previous and next numbers in array #include<iostream> using namespace std; void modify(int arr[], int n) { ????// Nothing to do when array size is 1 ????if (n <= 1) ??????return; ????// store current value of arr[0] and update it ????int prev = arr[0]; ????arr[0] = arr[0] * arr[1]; ????// Update rest of the array elements ????for (int i=1; i<n-1; i++) ????{ ????????// Store current value of next interation ????????int curr = arr[i]; ????????// Update current value using previos value ????????arr[i] = prev * arr[i+1]; ????????// Update previous value ????????prev = curr; ????} ????// Update last array element ????arr[n-1] = prev * arr[n-1]; } // Driver program int main() { ????int arr[] = {2, 3, 4, 5, 6}; ????int n = sizeof(arr)/sizeof(arr[0]); ????modify(arr, n); ????for (int i=0; i<n; i++) ??????cout << arr[i] << " "; ????return 0; } ``` ## Java ```java // Java program to update every array element with // multiplication of previous and next numbers in array import java.io.*; import java.util.*; import java.lang.Math; class Multipy { ???static void modify(int arr[], int n) ????{ ????????// Nothing to do when array size is 1 ????????if (n <= 1) ????????????return; ????????// store current value of arr[0] and update it ????????int prev = arr[0]; ????????arr[0] = arr[0] * arr[1]; ????????// Update rest of the array elements ????????for (int i=1; i<n-1; i++) ????????{ ????????????// Store current value of next interation ????????????int curr = arr[i]; ????????????// Update current value using previos value ????????????arr[i] = prev * arr[i+1]; ????????????// Update previous value ????????????prev = curr; ????????} ????????// Update last array element ????????arr[n-1] = prev * arr[n-1]; ????} ????// Driver program to test above function ????public static void main(String[] args) ????{ ????????int arr[] = {2, 3, 4, 5, 6}; ????????int n = arr.length; ????????modify(arr, n); ????????for (int i=0; i<n; i++) ?????????System.out.print(arr[i]+" "); ????} } /* This code is contributed by Devesh Agrawal */ ``` ## Python3 ```py # Python program to update every array element with # multiplication of previous and next numbers in array def modify(arr, n): ?# Nothing to do when array size is 1 ????if n <= 1: ????????return ????# store current value of arr[0] and update it ????prev = arr[0] ????arr[0] = arr[0] * arr[1] ????# Update rest of the array elements ????for i in range(1, n-1): ????????# Store current value of next interation ????????curr = arr[i]; ????????# Update current value using previos value ????????arr[i] = prev * arr[i+1] ???????????# Update previous value ????????prev = curr ????# Update last array element ????arr[n-1] = prev * arr[n-1] # Driver program arr = [2, 3, 4, 5, 6] n = len(arr) modify(arr, n) for i in range (0, n): ????print(arr[i],end=" ") # This code is contributed by # Smitha Dinesh Semwal ``` ## C# ```cs // C# program to update every array? // element with multiplication of // previous and next numbers in array using System; class GFG? { ????static void modify(int []arr, int n) ????{ ????????// Nothing to do when array size is 1 ????????if (n <= 1) ????????????return; ????????// store current value of arr[0] and update it ????????int prev = arr[0]; ????????arr[0] = arr[0] * arr[1]; ????????// Update rest of the array elements ????????for (int i=1; i<n-1; i++) ????????{ ????????????// Store current value of next interation ????????????int curr = arr[i]; ????????????// Update current value using previos value ????????????arr[i] = prev * arr[i+1]; ????????????// Update previous value ????????????prev = curr; ????????} ????????// Update last array element ????????arr[n-1] = prev * arr[n-1]; ????} ????// Driver program to test above function ????public static void Main() ????{ ????????int []arr = {2, 3, 4, 5, 6}; ????????int n = arr.Length; ????????modify(arr, n); ????????for (int i=0; i<n; i++) ????????Console.Write(arr[i]+" "); ????} } // This code is contributed by Sam007 ``` ## PHP ```php <?php // PHP program to update every array? // element with multiplication of previous? // and next numbers in array function modify(&$arr, $n) { ????// Nothing to do when array size is 1 ????if ($n <= 1) ????return; ????// store current value of arr[0] ????// and update it ????$prev = $arr[0]; ????$arr[0] = $arr[0] * $arr[1]; ????// Update rest of the array elements ????for ($i = 1; $i < $n - 1; $i++) ????{ ????????// Store current value of? ????????// next interation ????????$curr = $arr[$i]; ????????// Update current value using? ????????// previos value ????????$arr[$i] = $prev * $arr[$i + 1]; ????????// Update previous value ????????$prev = $curr; ????} ????// Update last array element ????$arr[$n-1] = $prev * $arr[$n - 1]; } // Driver Code $arr = array (2, 3, 4, 5, 6); $n = sizeof($arr); modify($arr, $n); for ($i = 0; $i < $n; $i++) echo $arr[$i] ." "; // This code is contributed? // by ChitraNayal ?> ``` 輸出: ``` 6 8 15 24 30 ```
                  <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>

                              哎呀哎呀视频在线观看