💎一站式轻松地调用各大LLM模型接口,支持GPT4、智谱、星火、月之暗面及文生图 广告
# 给定数组所有旋转中`i * arr [i]`的最大和 > 原文: [https://www.geeksforgeeks.org/maximum-sum-iarri-among-rotations-given-array/](https://www.geeksforgeeks.org/maximum-sum-iarri-among-rotations-given-array/) 给定`n`个整数的数组`arr[]`,找到使`i * arr [i]`的值的总和最大化的最大值,其中`i`从 0 到`n-1`变化。 **示例**: ``` Input: arr[] = {8, 3, 1, 2} Output: 29 Explanation: Lets look at all the rotations, {8, 3, 1, 2} = 8*0 + 3*1 + 1*2 + 2*3 = 11 {3, 1, 2, 8} = 3*0 + 1*1 + 2*2 + 8*3 = 29 {1, 2, 8, 3} = 1*0 + 2*1 + 8*2 + 3*3 = 27 {2, 8, 3, 1} = 2*0 + 8*1 + 3*2 + 1*1 = 17 Input: arr[] = {3, 2, 1} Output: 7 Explanation: Lets look at all the rotations, {3, 2, 1} = 3*0 + 2*1 + 1*2 = 4 {2, 1, 3} = 2*0 + 1*1 + 3*2 = 7 {1, 3, 2} = 1*0 + 3*1 + 2*2 = 7 ``` **方法 1 **:此方法讨论**朴素溶液**,该过程花费`O(n^2)`的时间。 解决方案涉及找到每次旋转中数组所有元素的总和,然后确定最大总和值。 * **方法**:一个简单的解决方案是尝试所有可能的旋转。 计算每次旋转的`i * arr[i]`之和,并返回最大和。 * **算法**: 1. 将所有值从 0 旋转到 n 的数组。 2. 计算每次旋转的总和。 3. 检查最大和是否大于当前和,然后更新最大和。 * **实现**: * ## C++ ``` // A Naive C++ program to find maximum sum rotation #include<bits/stdc++.h> using namespace std; // Returns maximum value of i*arr[i] int maxSum(int arr[], int n) {    // Initialize result    int res = INT_MIN;    // Consider rotation beginning with i    // for all possible values of i.    for (int i=0; i<n; i++)    {      // Initialize sum of current rotation      int curr_sum = 0;      // Compute sum of all values. We don't      // acutally rotation the array, but compute      // sum by finding ndexes when arr[i] is      // first element      for (int j=0; j<n; j++)      {          int index = (i+j)%n;          curr_sum += j*arr[index];      }      // Update result if required      res = max(res, curr_sum);    }    return res; } // Driver code int main() {     int arr[] = {8, 3, 1, 2};     int n = sizeof(arr)/sizeof(arr[0]);     cout << maxSum(arr, n) << endl;     return 0; } ``` ## Java ``` // A Naive Java program to find // maximum sum rotation import java.util.*; import java.io.*; class GFG { // Returns maximum value of i*arr[i] static int maxSum(int arr[], int n) { // Initialize result int res = Integer.MIN_VALUE; // Consider rotation beginning with i // for all possible values of i. for (int i = 0; i < n; i++) {     // Initialize sum of current rotation     int curr_sum = 0;     // Compute sum of all values. We don't     // actually rotation the array, but compute     // sum by finding ndexes when arr[i] is     // first element     for (int j = 0; j < n; j++)     {         int index = (i + j) % n;         curr_sum += j * arr[index];     }     // Update result if required     res = Math.max(res, curr_sum); } return res; } // Driver code public static void main(String args[]) {         int arr[] = {8, 3, 1, 2};         int n = arr.length;         System.out.println(maxSum(arr, n)); } } // This code is contributed by Sahil_Bansall ``` ## Python3 ``` # A Naive Python 3 program to find # maximum sum rotation import sys # Returns maximum value of i * arr[i] def maxSum(arr, n):     # Initialize result     res = -sys.maxsize     # Consider rotation beginning with i     # for all possible values of i.     for i in range(0, n):         # Initialize sum of current rotation         curr_sum = 0         # Compute sum of all values. We don't         # acutally rotation the array, but          # compute sum by finding ndexes when          # arr[i] is first element         for j in range(0, n):             index = int((i + j)% n)              curr_sum += j * arr[index]          # Update result if required         res = max(res, curr_sum)     return res  # Driver code arr = [8, 3, 1, 2]  n = len(arr) print(maxSum(arr, n)) # This code is contributed by # Smitha Dinesh Semwal ``` ## C# ``` // A Naive C# program to find // maximum sum rotation using System; class GFG {     // Returns maximum value of i*arr[i]     static int maxSum(int[] arr, int n)     {         // Initialize result         int res = int.MinValue;         // Consider rotation beginning with i         // for all possible values of i.         for (int i = 0; i < n; i++) {             // Initialize sum of current rotation             int curr_sum = 0;             // Compute sum of all values. We don't             // acutally rotation the array, but compute             // sum by finding ndexes when arr[i] is             // first element             for (int j = 0; j < n; j++)              {                 int index = (i + j) % n;                 curr_sum += j * arr[index];             }             // Update result if required             res = Math.Max(res, curr_sum);         }         return res;     }     // Driver code     public static void Main()     {         int[] arr = { 8, 3, 1, 2 };         int n = arr.Length;         Console.WriteLine(maxSum(arr, n));     } } // This code is contributed by vt_m. ``` ## PHP ``` <?php // A Naive PHP program to  // find maximum sum rotation // Returns maximum value // of i*arr[i] function maxSum($arr, $n) { // Initialize result $res = PHP_INT_MIN; // Consider rotation beginning  // with i for all possible  // values of i. for ($i = 0; $i < $n; $i++) {     // Initialize sum of     // current rotation     $curr_sum = 0;     // Compute sum of all values.     // We don't actually rotate      // the array, but compute sum     // by finding indexes when      // arr[i] is first element     for ($j = 0; $j < $n; $j++)     {         $index = ($i + $j) % $n;         $curr_sum += $j * $arr[$index];     }     // Update result if required     $res = max($res, $curr_sum); } return $res; } // Driver code $arr = array(8, 3, 1, 2); $n = sizeof($arr); echo maxSum($arr, $n), "\n"; // This code is contributed by ajit ?> ``` * **输出**: ``` 29 ``` * **复杂度分析**: * **时间复杂度**: `O(n^2)` * **辅助空间**: `O(1)` **方法 2 **:此方法讨论了解决**有效解决方案**的问题,该解决方案可解决`O(n)`时间问题。 在朴素的解决方案中,将为每个旋转计算值。 因此,如果可以在恒定时间内完成操作,那么复杂度将降低。 * **方法**:基本方法是从以前的旋转中计算新旋转的总和。 这样就产生了相似性,其中只有第一个元素和最后一个元素的乘数急剧变化,而其他元素的乘数则增加或减少 1。因此,可以从当前旋转的总和中计算出下一个旋转的总和。 * **算法**: 这个想法是使用先前旋转的值来计算旋转的值。 当数组旋转 1 时,`i * arr[i]`之和发生以下变化。 ``` next_val = curr_val - (cum_sum - arr[i-1]) + arr[i-1] * (n-1); next_val = Value of ∑i*arr[i] after one rotation. curr_val = Current value of ∑i*arr[i] cum_sum = Sum of all array elements, i.e., ∑arr[i]. Lets take example {1, 2, 3}. Current value is 1*0+2*1+3*2 = 8\. Shifting it by one will make it {2, 3, 1} and next value will be 8 - (6 - 1) + 1*2 = 5 which is same as 2*0 + 3*1 + 1*2 ``` 1. `arr[i-1]`的乘数从 0 变为`n-1`,即`arr[i-1] * (n-1)`被加到当前值。 2. 其他项的乘数减 1。即,从`cum_sum`是所有数字之和的当前值减去(`cum_sum – arr[i-1]`)。 * **实现**: ## C++ ``` // An efficient C++ program to compute // maximum sum of i*arr[i] #include<bits/stdc++.h> using namespace std; int maxSum(int arr[], int n) {     // Compute sum of all array elements     int cum_sum = 0;     for (int i=0; i<n; i++)         cum_sum += arr[i];     // Compute sum of i*arr[i] for initial     // configuration.     int curr_val = 0;     for (int i=0; i<n; i++)         curr_val += i*arr[i];     // Initialize result     int res = curr_val;     // Compute values for other iterations     for (int i=1; i<n; i++)     {         // Compute next value using previous          // value in O(1) time         int next_val = curr_val - (cum_sum - arr[i-1])                        + arr[i-1] * (n-1);         // Update current value         curr_val = next_val;         // Update result if required         res = max(res, next_val);     }     return res; } // Driver code int main() {     int arr[] = {8, 3, 1, 2};     int n = sizeof(arr)/sizeof(arr[0]);     cout << maxSum(arr, n) << endl;     return 0; } ``` ## Java ``` // An efficient Java program to compute // maximum sum of i*arr[i] import java.io.*; class GFG {     static int maxSum(int arr[], int n)     {         // Compute sum of all array elements         int cum_sum = 0;         for (int i = 0; i < n; i++)             cum_sum += arr[i];         // Compute sum of i*arr[i] for          // initial configuration.         int curr_val = 0;         for (int i = 0; i < n; i++)             curr_val += i * arr[i];         // Initialize result         int res = curr_val;         // Compute values for other iterations         for (int i = 1; i < n; i++)         {             // Compute next value using previous             // value in O(1) time             int next_val = curr_val - (cum_sum -                           arr[i-1]) + arr[i-1] *                           (n-1);             // Update current value             curr_val = next_val;             // Update result if required             res = Math.max(res, next_val);         }         return res;     }     // Driver code     public static void main(String[] args)     {         int arr[] = {8, 3, 1, 2};         int n = arr.length;         System.out.println(maxSum(arr, n));     } } // This code is contributed by Prerna Saini ``` ## Python3 ``` # An efficient Python 3 program to # compute maximum sum of i * arr[i] def maxSum(arr, n):     # Compute sum of all array elements     cum_sum = 0     for i in range(0, n):         cum_sum += arr[i]      # Compute sum of i * arr[i] for      # initial configuration.     curr_val = 0     for i in range(0, n):         curr_val += i * arr[i]      # Initialize result     res = curr_val      # Compute values for other iterations     for i in range(1, n):         # Compute next value using previous         # value in O(1) time         next_val = (curr_val - (cum_sum - arr[i-1]) +                                     arr[i-1] * (n-1))         # Update current value         curr_val = next_val          # Update result if required         res = max(res, next_val)     return res  # Driver code arr = [8, 3, 1, 2]  n = len(arr) print(maxSum(arr, n)) # This code is contributed by # Smitha Dinesh Semwal ``` ## C# ``` // An efficient C# program to compute // maximum sum of i*arr[i] using System; class GFG {     static int maxSum(int []arr, int n)     {         // Compute sum of all array elements         int cum_sum = 0;         for (int i = 0; i < n; i++)             cum_sum += arr[i];         // Compute sum of i*arr[i] for          // initial configuration.         int curr_val = 0;         for (int i = 0; i < n; i++)             curr_val += i * arr[i];         // Initialize result         int res = curr_val;         // Compute values for other iterations         for (int i = 1; i < n; i++)         {             // Compute next value using previous             // value in O(1) time             int next_val = curr_val - (cum_sum -                       arr[i - 1]) + arr[i - 1] *                                         (n - 1);             // Update current value             curr_val = next_val;             // Update result if required             res = Math.Max(res, next_val);         }         return res;     }     // Driver code     public static void Main()     {         int []arr = {8, 3, 1, 2};         int n = arr.Length;         Console.Write(maxSum(arr, n));     } } // This code is contributed by nitin mittal ``` ## PHP ``` <?php // An efficient PHP program to  // compute maximum sum of i*arr[i] function maxSum($arr, $n) {     // Compute sum of all     // array elements     $cum_sum = 0;     for ($i = 0; $i < $n; $i++)         $cum_sum += $arr[$i];     // Compute sum of i*arr[i]      // for initial configuration.     $curr_val = 0;     for ($i = 0; $i < $n; $i++)         $curr_val += $i * $arr[$i];     // Initialize result     $res = $curr_val;     // Compute values for     // other iterations     for ($i = 1; $i < $n; $i++)     {         // Compute next value using          // previous value in O(1) time         $next_val = $curr_val -                     ($cum_sum - $arr[$i - 1]) +                      $arr[$i - 1] * ($n - 1);         // Update current value         $curr_val = $next_val;         // Update result if required         $res = max($res, $next_val);     }     return $res; } // Driver code $arr = array(8, 3, 1, 2); $n = sizeof($arr); echo maxSum($arr, $n); // This code is contributed by ajit ?> ``` * **输出**: ``` 29 ``` * **复杂度分析**: * **时间复杂度**: `O(n)`。 由于需要从 0 到 n 的一个循环来检查所有旋转,并且当前旋转的总和是根据`O(1)`时间中的先前旋转计算得出的)。 * **辅助空间**: `O(1)`。 由于不需要额外的空间,因此空间复杂度将为`O(1)` **方法 3 **:该方法讨论使用`O(n)`时间中的枢轴的解决方案。 透视方法只能在排序数组或旋转排序数组的情况下使用。 例如:`{1, 2, 3, 4}`或`{2, 3, 4, 1}`,`{3, 4, 1, 2}`等。 * **方法**:假设是排序数组的情况。 如我们所知,对于数组,最大和是将数组按升序排序。 在旋转数组排序的情况下,我们可以旋转数组以使其升序排列。 因此,在这种情况下,需要找到枢轴元素,然后才能计算出最大和。 * **算法**: 1. **查找数组**的枢轴:如果`arr[i] > arr[(i + 1) % n]`,则它是枢轴元素。`(i + 1) % n`用于检查最后一个和第一个元素。 2. 获得枢轴后,可以通过找到与枢轴的差(即乘数)并将其与当前元素相乘,同时计算总和来计算总和 * **实现**: ## C++ ``` // C++ program to find maximum sum of all  // rotation of i*arr[i] using pivot.  #include <iostream> using namespace std;  // fun declaration  int maxSum(int arr[], int n);  int findPivot(int arr[], int n);  // function definition  int maxSum(int arr[], int n)  {      int sum = 0;      int i;      int pivot = findPivot(arr, n);      // difference in pivot and index of      // last element of array      int diff = n - 1 - pivot;      for(i = 0; i < n; i++)      {          sum = sum + ((i + diff) % n) * arr[i];      }      return sum;  }  // function to find pivot  int findPivot(int arr[], int n)  {      int i;      for(i = 0; i < n; i++)      {          if(arr[i] > arr[(i + 1) % n])              return i;      }  }  // Driver code  int main(void)  {      // rotated input array      int arr[] = {8, 3, 1, 2};      int n = sizeof(arr) / sizeof(int);      int max = maxSum(arr, n);      cout << max;      return 0;  }  // This code is contributed by Shubhamsingh10  ``` ## C ``` // C program to find maximum sum of all  // rotation of i*arr[i] using pivot. #include<stdio.h> // fun declaration int maxSum(int arr[], int n);  int findPivot(int arr[], int n); // function definition int maxSum(int arr[], int n)  {     int sum = 0;     int i;     int pivot = findPivot(arr, n);     // difference in pivot and index of      // last element of array     int diff = n - 1 - pivot;      for(i = 0; i < n; i++)     {         sum= sum + ((i + diff) % n) * arr[i];     }     return sum; } // function to find pivot int findPivot(int arr[], int n) {     int i;     for(i = 0; i < n; i++)     {         if(arr[i] > arr[(i + 1) % n])             return i;     } } // Driver code int main(void) {     // rotated input array     int arr[] = {8, 3, 1, 2};      int n = sizeof(arr) / sizeof(int);     int max = maxSum(arr, n);      printf("%d", max);     return 0;  } ``` ## Java ``` // Java program to find maximum sum  // of all rotation of i*arr[i] using pivot. import java.util.*; import java.lang.*; import java.io.*; class GFG { // function definition  static int maxSum(int arr[], int n)  {     int sum = 0;     int i;     int pivot = findPivot(arr, n);     // difference in pivot and index of     // last element of array     int diff = n - 1 - pivot;      for(i = 0; i < n; i++)     {          sum= sum + ((i + diff) % n) * arr[i];     }     return sum; } // function to find pivot static int findPivot(int arr[], int n) {     int i;     for(i = 0; i < n; i++)     {         if(arr[i] > arr[(i + 1) % n])             return i;     }     return 0; } // Driver code public static void main(String args[]) {     // rotated input array     int arr[] = {8, 3, 1, 2};      int n = arr.length;     int max = maxSum(arr,n);      System.out.println(max); } } ``` ## Python3 ``` # Python3 program to find maximum sum of  # all rotation of i*arr[i] using pivot.  # function definition  def maxSum(arr, n) :     sum = 0     pivot = findPivot(arr, n)     # difference in pivot and index      # of last element of array      diff = n - 1 - pivot      for i in range(n) :         sum = sum + ((i + diff) % n) * arr[i];      return sum # function to find pivot  def findPivot(arr, n) :     for i in range(n) :          if(arr[i] > arr[(i + 1) % n]) :             return i;  # Driver code  if __name__ == "__main__" :     # rotated input array      arr = [8, 3, 1, 2]      n= len(arr)      max= maxSum(arr, n)     print(max) # This code is contributed by Ryuga ``` ## C# ``` // C# program to find maximum sum  // of all rotation of i*arr[i] using pivot.  using System; class GFG { // function definition  public static int maxSum(int[] arr, int n) {     int sum = 0;     int i;     int pivot = findPivot(arr,n);     // difference in pivot and index of      // last element of array      int diff = n - 1 - pivot;     for (i = 0;i < n;i++)     {         sum = sum + ((i + diff) % n) * arr[i];     }     return sum; } // function to find pivot  public static int findPivot(int[] arr, int n) {     int i;     for (i = 0; i < n; i++)     {         if (arr[i] > arr[(i + 1) % n])         {             return i;         }     }     return 0;     } // Driver code  public static void Main(string[] args) {     // rotated input array      int[] arr = new int[] {8, 3, 1, 2};     int n = arr.Length;     int max = maxSum(arr,n);     Console.WriteLine(max); } } // This code is contributed by Shrikant13 ``` ## PHP ``` <?php // PHP program to find maximum sum  // of all rotation of i*arr[i] using pivot. // function definition  function maxSum($arr, $n)  { $sum = 0; $pivot = findPivot($arr, $n); // difference in pivot and index of // last element of array $diff = $n - 1 - $pivot;  for($i = 0; $i < $n; $i++) {     $sum = $sum + (($i + $diff) %              $n) * $arr[$i]; } return $sum; } // function to find pivot function findPivot($arr, $n) {     for($i = 0; $i < $n; $i++)     {         if($arr[$i] > $arr[($i + 1) % $n])         return $i;     }     return 0; } // Driver code // rotated input array $arr = array(8, 3, 1, 2);  $n = sizeof($arr); $max = maxSum($arr, $n);  echo $max; // This code is contributed // by Akanksha Rai(Abby_akku) ?> ``` * **输出**: ``` 29 ``` * **复杂度分析**: * **时间复杂度**: `O(n)` 因为只需要一个循环就可以从 0 遍历到 n,从而找到枢轴。 为了找到总和,需要另一个循环,因此复杂度保持为`O(n)`。 * **辅助空间**:`O(1)`。 我们不需要额外的空间,因此辅助空间为`O(1)`