Wednesday, June 16, 2021

Published June 16, 2021 by Anonymous with 0 comment

Maximize score of same-indexed subarrays selected from two given arrays

  

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

}

Adblock test (Why?)


Original page link

Best Cool Tech Gadgets

Top favorite technology gadgets
      edit

0 comments:

Post a Comment