Given an array and three numbers, maximize (x * a[i]) + (y * a[j]) + (z * a[k])

Given an array of n integers, and three integers x, y and z. maximise the value of (x * a[i]) + (y * a[j]) + (z * a[k]) where i ≤ j ≤ k.

Examples :

Input : arr[] = {-1, -2, -3, -4, -5} 
         x = 1 
         y = 2 
         z = -3 
Output: 12
Explanation: The maximized values is 
(1 * -1) + (2 * -1) + ( -3 * -5) = 12 

Input: arr[] = {1, 2, 3, 4, 5} 
       x = 1 
       y = 2  
       z = 3 
Output: 30 
(1*5 + 2*5 + 3*5) = 30



A simple solution is to run three nested loops to iterate through all triplets. For every triplet, compute the required value and keep track of maximum and finally return the same.

An efficient solution is to preocompute values and store them using extra space. The first key observation is i ≤ j ≤ k, so x*a[i] will always be the left maximum, and z*a[k] will always be the right maximum. Create a left array where we store the left maximums for every element. Create a right array where we store the right maximums for every element. Then for every element, calculate the maximum value of the function possible. For any index ind, the maximum at that position will always be (left[ind] + j * a[ind] + right[ind]), find the maximum of this value for every element in the array and that will be your answer.

Below is the implementation of the above approach

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// CPP program to find the maximum value of
// x*arr[i] + y*arr[j] + z*arr[k]
#include <bits/stdc++.h>
using namespace std;
  
// function to maximize the condition
int maximizeExpr(int a[], int n, int x, int y, 
                                        int z)
{
    // Traverse the whole array and compute
    // left maximum for every index. 
    int L[n];
    L[0] = x * a[0];
    for (int i = 1; i < n; i++) 
        L[i] = max(L[i - 1], x * a[i]);
  
    // Compute right maximum for every index. 
    int R[n];
    R[n-1] = z * a[n-1];
    for (int i = n - 2; i >= 0; i--)
        R[i] = max(R[i + 1], z * a[i]);
  
    // Traverse through the whole array to 
    // maximize the required expression.
    int ans = INT_MIN; 
    for (int i = 0; i < n; i++) 
          ans = max(ans, L[i] + y * a[i] + R[i]); 
  
    return ans;
}
      
// driver program to test the above funcion 
int main() 
{
   int a[] = {-1, -2, -3, -4, -5};
   int n = sizeof(a)/sizeof(a[0]);
   int x = 1, y = 2 , z = -3;
   cout << maximizeExpr(a, n, x, y, z) << endl;
   return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to find the maximum value 
// of x*arr[i] + y*arr[j] + z*arr[k]
import java.io.*;
  
class GFG {
  
    // function to maximize the condition
    static int maximizeExpr(int a[], int n, int x, 
                             int y, int z)
    {
        // Traverse the whole array and compute
        // left maximum for every index. 
        int L[] = new int[n];
        L[0] = x * a[0];
        for (int i = 1; i < n; i++) 
            L[i] = Math.max(L[i - 1], x * a[i]);
  
        // Compute right maximum for every index. 
        int R[] = new int[n];
        R[n - 1] = z * a[n - 1];
        for (int i = n - 2; i >= 0; i--)
            R[i] = Math.max(R[i + 1], z * a[i]);
  
        // Traverse through the whole array to 
        // maximize the required expression.
        int ans = Integer.MIN_VALUE; 
        for (int i = 0; i < n; i++) 
            ans = Math.max(ans, L[i] + y * a[i] +
                                         R[i]); 
  
        return ans;
    }
      
    // driver program to test the above funcion 
    public static void main(String[] args) 
    {
    int a[] = {-1, -2, -3, -4, -5};
    int n = a.length;
    int x = 1, y = 2 , z = -3;
    System.out.println(maximizeExpr(a, n, x, y, z));
    }
}
// This code is contributed by Prerna Saini

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 program to find 
# the maximum value of
# x*arr[i] + y*arr[j] + z*arr[k]
import sys
  
# function to maximize
# the condition
def maximizeExpr(a, n, x, y, z):
  
    # Traverse the whole array 
    # and compute left maximum 
    # for every index. 
    L = [0] * n
    L[0] = x * a[0]
    for i in range(1, n):
        L[i] = max(L[i - 1], x * a[i])
  
    # Compute right maximum
    # for every index. 
    R = [0] * n
    R[n - 1] = z * a[n - 1]
    for i in range(n - 2, -1, -1):
        R[i] = max(R[i + 1], z * a[i])
  
    # Traverse through the whole 
    # array to maximize the
    # required expression.
    ans = -sys.maxsize 
    for i in range(0, n):
        ans = max(ans, L[i] + y * 
                       a[i] + R[i]) 
  
    return ans
  
# Driver Code
a = [-1, -2, -3, -4, -5]
n = len(a)
x = 1
y = 2
z = -3
print(maximizeExpr(a, n, x, y, z))
  
# This code is contributed
# by Smitha

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to find the maximum value 
// of x*arr[i] + y*arr[j] + z*arr[k]
using System;
  
class GFG {
  
    // function to maximize the condition
    static int maximizeExpr(int []a, int n,
                       int x, int y, int z)
    {
          
        // Traverse the whole array and
        // compute left maximum for every
        // index. 
        int []L = new int[n];
        L[0] = x * a[0];
        for (int i = 1; i < n; i++) 
            L[i] = Math.Max(L[i - 1], x * a[i]);
  
        // Compute right maximum for
        // every index. 
        int []R = new int[n];
        R[n - 1] = z * a[n - 1];
        for (int i = n - 2; i >= 0; i--)
            R[i] = Math.Max(R[i + 1], z * a[i]);
  
        // Traverse through the whole array to 
        // maximize the required expression.
        int ans = int.MinValue; 
        for (int i = 0; i < n; i++) 
            ans = Math.Max(ans, L[i] + 
                             y * a[i] + R[i]); 
  
        return ans;
    }
      
    // driver program to test the
    // above funcion 
    public static void Main() 
    {
        int []a = {-1, -2, -3, -4, -5};
        int n = a.Length;
        int x = 1, y = 2 , z = -3;
          
        Console.WriteLine(
              maximizeExpr(a, n, x, y, z));
    }
}
  
// This code is contributed by vt_m.

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// PHP program to find the 
// maximum value of 
// x*arr[i]+ y*arr[j] + z*arr[k]
  
// function to maximize
// the condition
function maximizeExpr($a, $n
                      $x, $y, $z)
{
    // Traverse the whole array 
    // and compute left maximum 
    // for every index. 
    $L = array();
    $L[0] = $x * $a[0];
    for ($i = 1; $i < $n; $i++) 
        $L[$i] = max($L[$i - 1], 
                     $x * $a[$i]);
  
    // Compute right maximum
    // for every index. 
    $R = array();
    $R[$n - 1] = $z * $a[$n - 1];
    for ($i = $n - 2; $i >= 0; $i--)
        $R[$i] = max($R[$i + 1], 
                     $z * $a[$i]);
  
    // Traverse through the whole 
    // array to maximize the 
    // required expression.
    $ans = PHP_INT_MIN; 
    for ($i = 0; $i < $n; $i++) 
        $ans = max($ans, $L[$i] + 
                   $y * $a[$i] + $R[$i]); 
  
    return $ans;
}
      
// Driver Code
$a = array(-1, -2, -3, -4, -5);
$n = count($a);
$x = 1; $y = 2 ; $z = -3;
echo maximizeExpr($a, $n, $x, $y, $z);
  
// This code is contributed by anuj_67.
?>

chevron_right



Output :

12

Time complexity : O(n)
Auxiliary Space : O(n)

This article is contributed by Raj. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.



My Personal Notes arrow_drop_up

Improved By : vt_m, Smitha Dinesh Semwal



Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.