#include <bits/stdc++.h>
using namespace std;
  
void maxScoreSubArray(int* a, int* b,
                      int n)
{
    
    int res = 0;
  
    
    for (int mid = 0; mid < n; mid++) {
  
        
        
        int straightScore = a[mid] * b[mid],
            reverseScore = a[mid] * a[mid];
        int prev = mid - 1, next = mid + 1;
  
        
        res = max(res, max(straightScore,
                           reverseScore));
  
        
        
        
        while (prev >= 0 && next < n) {
  
            
            straightScore
                += (a[prev] * b[prev]
                    + a[next] * b[next]);
            reverseScore
                += (a[prev] * b[next]
                    + a[next] * b[prev]);
  
            res = max(res,
                      max(straightScore,
                          reverseScore));
  
            prev--;
            next++;
        }
  
        
        
        straightScore = 0;
        reverseScore = 0;
  
        prev = mid - 1, next = mid;
        while (prev >= 0 && next < n) {
  
            
            straightScore
                += (a[prev] * b[prev]
                    + a[next] * b[next]);
            reverseScore
                += (a[prev] * b[next]
                    + a[next] * b[prev]);
  
            res = max(res,
                      max(straightScore,
                          reverseScore));
  
            prev--;
            next++;
        }
    }
  
    
    cout << res;
}
  
int main()
{
    int A[] = { 13, 4, 5 };
    int B[] = { 10, 22, 2 };
    int N = sizeof(A) / sizeof(A[0]);
    maxScoreSubArray(A, B, N);
  
    return 0;
}
Original page link
Best Cool Tech Gadgets
Top favorite technology gadgets
 
 
 
 
0 comments:
Post a Comment