Maximize score of same-indexed subarrays selected from two given arrays
Last Updated :
09 Nov, 2021
Given two arrays A[] and B[], both consisting of N positive integers, the task is to find the maximum score among all possible same-indexed subarrays in both the arrays such that the score of any subarray over the range [L, R] is calculated by the maximum of the values (AL*BL + AL + 1*BL + 1 + … + AR*BR) + (AR*BL + AR – 1*BL + 1 + … + AL*BR).
Examples:
Input: A[] = {13, 4, 5}, B[] = {10, 22, 2}
Output: 326
Explanation:
Consider the subarrays {A[0], A[1]} and {B[0], B[1]}. Score of these subarrays can be calculated as the maximum of the following two expressions:
- The value of the expression (A0*B0 + A1*B1) = 13 * 1 + 4 * 22 = 218.
- The value of the expression (A0*B1 + A1*B0) = 13 * 1 + 4 * 22 = 326.
Therefore, the maximum value from the above two expressions is 326, which is the maximum score among all possible subarrays.
Input: A[] = {9, 8, 7, 6, 1}, B[]={6, 7, 8, 9, 1}
Output: 230
Naive Approach: The simplest approach to solve the given problem is to generate all possible corresponding subarray and store all the scores of all subarray generated using the given criteria. After storing all the scores, print the maximum value among all the scores generated.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int currSubArrayScore( int * a, int * b,
int l, int r)
{
int straightScore = 0;
int reverseScore = 0;
for ( int i = l; i <= r; i++) {
straightScore += a[i] * b[i];
reverseScore += a[r - (i - l)]
* b[i];
}
return max(straightScore,
reverseScore);
}
void maxScoreSubArray( int * a, int * b,
int n)
{
int res = 0, start = 0, end = 0;
for ( int i = 0; i < n; i++) {
for ( int j = i; j < n; j++) {
int currScore
= currSubArrayScore(
a, b, i, j);
if (currScore > res) {
res = currScore;
start = i;
end = j;
}
}
}
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;
}
|
Java
import java.io.*;
class GFG{
static int currSubArrayScore( int [] a, int [] b,
int l, int r)
{
int straightScore = 0 ;
int reverseScore = 0 ;
for ( int i = l; i <= r; i++)
{
straightScore += a[i] * b[i];
reverseScore += a[r - (i - l)] * b[i];
}
return Math.max(straightScore, reverseScore);
}
static void maxScoreSubArray( int [] a, int [] b, int n)
{
int res = 0 , start = 0 , end = 0 ;
for ( int i = 0 ; i < n; i++)
{
for ( int j = i; j < n; j++)
{
int currScore = currSubArrayScore(a, b, i, j);
if (currScore > res)
{
res = currScore;
start = i;
end = j;
}
}
}
System.out.print(res);
}
public static void main(String[] args)
{
int A[] = { 13 , 4 , 5 };
int B[] = { 10 , 22 , 2 };
int N = A.length;
maxScoreSubArray(A, B, N);
}
}
|
Python3
def currSubArrayScore(a, b,
l, r):
straightScore = 0
reverseScore = 0
for i in range (l, r + 1 ) :
straightScore + = a[i] * b[i]
reverseScore + = a[r - (i - l)] * b[i]
return max (straightScore,
reverseScore)
def maxScoreSubArray(a, b,
n) :
res = 0
start = 0
end = 0
for i in range (n) :
for j in range (i, n) :
currScore = currSubArrayScore(a, b, i, j)
if (currScore > res) :
res = currScore
start = i
end = j
print (res)
A = [ 13 , 4 , 5 ]
B = [ 10 , 22 , 2 ]
N = len (A)
maxScoreSubArray(A, B, N)
|
C#
using System;
class GFG{
static int currSubArrayScore( int [] a, int [] b,
int l, int r)
{
int straightScore = 0;
int reverseScore = 0;
for ( int i = l; i <= r; i++)
{
straightScore += a[i] * b[i];
reverseScore += a[r - (i - l)] * b[i];
}
return Math.Max(straightScore, reverseScore);
}
static void maxScoreSubArray( int [] a, int [] b, int n)
{
int res = 0;
for ( int i = 0; i < n; i++)
{
for ( int j = i; j < n; j++)
{
int currScore = currSubArrayScore(
a, b, i, j);
if (currScore > res)
{
res = currScore;
}
}
}
Console.Write(res);
}
static public void Main()
{
int [] A = { 13, 4, 5 };
int [] B = { 10, 22, 2 };
int N = A.Length;
maxScoreSubArray(A, B, N);
}
}
|
Javascript
<script>
function currSubArrayScore(a, b, l, r)
{
let straightScore = 0;
let reverseScore = 0;
for (let i = l; i <= r; i++) {
straightScore += a[i] * b[i];
reverseScore += a[r - (i - l)]
* b[i];
}
return Math.max(straightScore,
reverseScore);
}
function maxScoreSubArray(a, b, n) {
let res = 0, start = 0, end = 0;
for (let i = 0; i < n; i++) {
for (let j = i; j < n; j++) {
let currScore
= currSubArrayScore(
a, b, i, j);
if (currScore > res) {
res = currScore;
start = i;
end = j;
}
}
}
document.write(res);
}
let A = [13, 4, 5];
let B = [10, 22, 2];
let N = A.length;
maxScoreSubArray(A, B, N);
</script>
|
Time Complexity: O(N3)
Auxiliary Space: O(1)
Efficient Approach: The above approach can also be optimized by considering every element as the middle point of every possible subarray and then expand the subarray in both directions while updating the maximum score for every value. Follow the steps below to solve the problem:
- Initialize a variable, say res to store the resultant maximum value.
- Iterate over the range [1, N – 1] using the variable mid and perform the following steps:
- Initialize two variables, say score1 and score2 as A[mid]*B[mid] in case of odd length subarray.
- Initialize two variables, say prev as (mid – 1) and next as (mid + 1) to expand the current subarray.
- Iterate a loop until prev is positive and the value of next is less than N and perform the following steps:
- Add the value of (a[prev]*b[prev]+a[next]*b[next]) to the variable score1.
- Add the value of (a[prev]*b[next]+a[next]*b[prev]) to the variable score2.
- Update the value of res to the maximum of score1, score2, and res.
- Decrement the value of prev by 1 and increment the value of next by 1.
- Update the value of score1 and score2 as 0 and set the value of prev as (mid – 1) and next as mid to consider the case of even length subarray.
- After completing the above steps, print the value of res as the result.
Below is the implementation of the above approach:
C++
#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;
}
|
Java
import java.io.*;
class GFG {
static 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 = Math.max(
res, Math.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 = Math.max(res, Math.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 = Math.max(res, Math.max(straightScore,
reverseScore));
prev--;
next++;
}
}
System.out.println(res);
}
public static void main(String[] args)
{
int A[] = { 13 , 4 , 5 };
int B[] = { 10 , 22 , 2 };
int N = A.length;
maxScoreSubArray(A, B, N);
}
}
|
Python3
def maxScoreSubArray(a, b, n):
res = 0
for mid in range (n):
straightScore = a[mid] * b[mid]
reverseScore = a[mid] * a[mid]
prev = mid - 1
next = mid + 1
res = max (res, max (straightScore,
reverseScore))
while (prev > = 0 and 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 - = 1
next + = 1
straightScore = 0
reverseScore = 0
prev = mid - 1
next = mid
while (prev > = 0 and 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 - = 1
next + = 1
print (res)
if __name__ = = '__main__' :
A = [ 13 , 4 , 5 ]
B = [ 10 , 22 , 2 ]
N = len (A)
maxScoreSubArray(A, B, N)
|
C#
using System;
public class GFG{
static 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 = Math.Max(
res, Math.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 = Math.Max(res, Math.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 = Math.Max(res, Math.Max(straightScore,
reverseScore));
prev--;
next++;
}
}
Console.WriteLine(res);
}
static public void Main (){
int [] A = { 13, 4, 5 };
int [] B = { 10, 22, 2 };
int N = A.Length;
maxScoreSubArray(A, B, N);
}
}
|
Javascript
<script>
function maxScoreSubArray(a, b, n) {
let res = 0;
for (let mid = 0; mid < n; mid++) {
let straightScore = a[mid] * b[mid],
reverseScore = a[mid] * a[mid];
let prev = mid - 1, next = mid + 1;
res = Math.max(res, Math.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 = Math.max(res,
Math.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 = Math.max(res,
Math.max(straightScore,
reverseScore));
prev--;
next++;
}
}
document.write(res);
}
let A = [13, 4, 5];
let B = [10, 22, 2];
let N = A.length
maxScoreSubArray(A, B, N);
</script>
|
Time Complexity: O(N2)
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...