Rotate Function in C++

C++Server Side ProgrammingProgramming

Suppose we have Given an array of integers A and let n is the length of array A. Now assume Bk to be an array obtained by rotating the array A, k positions clock-wise. Here the rotation can be defined as −

  • F(k) = 0 * Bk[0] + 1 * Bk[1] + ... + (n-1) * Bk[n-1].

Now find the maximum value of F(0), F(1), ..., F(n-1).

So if the input is like A = [4,3,2,6], then −

  • F(0) = (0 * 4) + (1 * 3) + (2 * 2) + (3 * 6) = 0 + 3 + 4 + 18 = 25

  • F(1) = (0 * 6) + (1 * 4) + (2 * 3) + (3 * 2) = 0 + 4 + 6 + 6 = 16

  • F(2) = (0 * 2) + (1 * 6) + (2 * 4) + (3 * 3) = 0 + 6 + 8 + 9 = 23

  • F(3) = (0 * 3) + (1 * 2) + (2 * 6) + (3 * 4) = 0 + 2 + 12 + 12 = 26

The maximum is 26.

To solve this, we will follow these steps −

  • n := size of array A, if n is 0, then return 0

  • ret := 0, create two arrays of size n, these are right and left

  • left[0] := A[0]

  • for i in range 1 to n – 1

    • left[i] := left[i] + left[i – 1]

    • left[i] := left[i] + A[i]

  • right[n – 1] := A[n – 1]

  • for i in range n – 1 down to 0

    • right[i] := right[i] + right[i + 1]

    • right[i] := right[i] + A[i]

  • rightMul := 0, cnt := n – 1

  • for i in range n – 1 down to 1

    • rightMul := rightMul + A[i] * cnt

    • decrease cnt by 1

  • Make an array x of size n

  • for i in range 0 to n – 2

    • x[i] := rightMul

    • rightMul := rightMul – right[i + 1]

  • leftMul := 0, cnt := 1

  • for i in range 0 down to n – 2

    • leftMul := leftMul + A[i] * cnt

    • increase cnt by 1

  • decrease cnt by 1

  • for i in range n – 1 down to 1

    • x[i] := x[i] + leftMul

    • leftMul := leftMul – A[i – 1] * cnt

    • if i – 2 >= 0, then leftMul := leftMul + left[i – 2]

  • return max of x

Example (C++)

Let us see the following implementation to get better understanding −

 Live Demo

#include <bits/stdc++.h>
using namespace std;
typedef long long int lli;
class Solution {
   public:
   int maxRotateFunction(vector<int>& A) {
      lli n = A.size();
      if(n == 0) return 0;
      lli ret = 0;
      vector <lli>right(n);
      vector <lli> left(n);
      left[0] = A[0];
      for(lli i = 1; i < n; i++){
         left[i] += left[i - 1];
         left[i] += A[i];
      }
      right[n - 1] = A[n - 1];
      for(lli i = n - 2; i >= 0; i--){
         right[i] += right[i + 1];
         right[i] += A[i];
      }
      lli rightMul = 0;
      lli cnt = n - 1;
      for(lli i = n - 1; i > 0; i--){
         rightMul += (A[i] * cnt);
         cnt--;
      }
      vector <lli> x(n);
      for(lli i = 0; i < n - 1; i++){
         x[i] = rightMul;
         rightMul -= right[i + 1];
      }
      lli leftMul = 0;
      cnt = 1;
      for(lli i = 0; i < n - 1; i++){
         leftMul += A[i] * cnt;
         cnt++;
      }
      cnt--;
      for(lli i = n - 1; i >= 1; i--){
         x[i] += leftMul;
         leftMul -= (A[i - 1] * cnt);
         if(i - 2 >= 0) leftMul += left[i - 2];
      }
      ret = INT_MIN;
      for(lli i = 0; i < x.size(); i++) ret = max(ret, x[i]);
      return ret;
   }
};
main(){
   Solution ob;
   vector<int> v = {4,3,2,6};
   cout <<(ob.maxRotateFunction(v));
}

Input

[4,3,2,6]

Output

26
raja
Published on 02-May-2020 08:28:11
Advertisements