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

                ??一站式輕松地調用各大LLM模型接口,支持GPT4、智譜、豆包、星火、月之暗面及文生圖、文生視頻 廣告
                # 允許負數的數組中成對乘積的最大和 > 原文: [https://www.geeksforgeeks.org/maximum-sum-of-pairwise-product-in-an-array-with-negative-allowed/](https://www.geeksforgeeks.org/maximum-sum-of-pairwise-product-in-an-array-with-negative-allowed/) 給定 n 個元素的數組。 查找成對乘法的最大和。 總和可以更大,所以取 10 ^ 9 + 7 為 mod。 如果存在奇數個元素,則可以將任意一個元素(不成對)相加。 **示例**: ``` Input : arr[] = {-1, 4, 5, -7, -4, 9, 0} Output : 77 So to get the maximum sum, the arrangement will be {-7, -4}, {-1, 0}, {9, 5} and {4}. So the answer is (-7*(-4))+((-1)*0)+(9*5)+(4) ={77}. Input : arr[] = {8, 7, 9} Output : 79 Answer is (9*8) +(7) = 79. ``` 1-對給定的數組進行排序。 2-首先,將負數從頭開始成對相乘,然后將其加到 total_sum 中。 3-第二,將正數從最后一個乘以 total_sum。 4-檢查負數和正數兩個計數是否均為奇數,然后加上最后一對 的乘積,即最后一個負數和正數左。 5-或如果一個計數中的任何一個為奇數,則在左側添加該元素。 6-返回和。 ## C++ ```cpp // C++ program for above implementation #include <bits/stdc++.h> #define Mod 1000000007 using namespace std; // Function to find the maximum sum long long int findSum(int arr[], int n) { ????long long int sum = 0; ????// Sort the array first ????sort(arr, arr + n); ????// First multiply negative numbers pairwise ????// and sum up from starting as to get maximum? ????// sum.? ????int i = 0; ????while (i < n && arr[i] < 0) { ????????if (i != n - 1 && arr[i + 1] <= 0) { ????????????sum = (sum + (arr[i] * arr[i + 1]) % Mod) % Mod; ????????????i += 2; ????????} ????????else ????????????break; ????} ????// Second multiply positive numbers pairwise ????// and summed up from the last as to get maximum? ????// sum. ????int j = n - 1; ????while (j >= 0 && arr[j] > 0) { ????????if (j != 0 && arr[j - 1] > 0) { ????????????sum = (sum + (arr[j] * arr[j - 1]) % Mod) % Mod; ????????????j -= 2; ????????} ????????else ????????????break; ????} ????// To handle case if positive and negative ????// numbers both are odd in counts. ????if (j > i) ????????sum = (sum + (arr[i] * arr[j]) % Mod) % Mod; ????// If one of them occurs odd times ????else if (i == j) ????????sum = (sum + arr[i]) % Mod; ????return sum; } // Drivers code int main() { ????int arr[] = { -1, 9, 4, 5, -4, 7 }; ????int n = sizeof(arr) / sizeof(arr[0]); ????cout << findSum(arr, n); ????return 0; } ```
                  <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>

                              哎呀哎呀视频在线观看