Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Program to find value for which given array expression is maximized in Python
Suppose we have two arrays called nums and values, both contain integers where the values of nums are strictly increasing and their lengths are the same. We have to find the maximum value of v for a pair of indices i, j such that i ? j that maximizes v = values[i] + values[j] + nums[j] - nums[i].
So, if the input is like nums = [1, 2, 7] and values = [-4, 6, 5], then the output will be 16. If we pick i = 1 and j = 2 we get 6 + 5 + 7 - 2 = 16.
Algorithm
To solve this problem, we need to rearrange the expression values[i] + values[j] + nums[j] - nums[i] as (values[i] - nums[i]) + (values[j] + nums[j]). This allows us to find the maximum values for each part separately ?
ans1 := -inf,ans2 := -inf-
For
iin range 0 to size ofnums - 1, doans1 := maximum of ans1 and (values[i] - nums[i])ans2 := maximum of ans2 and (values[i] + nums[i])
Return
ans1 + ans2
Example
Let us see the following implementation to get better understanding ?
from math import inf
def solve(nums, values):
ans1 = -inf
ans2 = -inf
for i in range(len(nums)):
ans1 = max(ans1, (values[i] - nums[i]))
ans2 = max(ans2, (values[i] + nums[i]))
return ans1 + ans2
nums = [1, 2, 7]
values = [-4, 6, 5]
print(solve(nums, values))
16
How It Works
The algorithm works by transforming the original expression. Instead of checking all pairs (i, j), we separate the expression into two independent parts:
(values[i] - nums[i])− represents the contribution from the left index(values[j] + nums[j])− represents the contribution from the right index
Since we want to maximize the sum, we find the maximum value for each part independently and add them together.
Step-by-Step Trace
from math import inf
def solve_with_trace(nums, values):
ans1 = -inf
ans2 = -inf
print("Index | values[i] | nums[i] | values[i]-nums[i] | values[i]+nums[i]")
print("------|-----------|---------|------------------|------------------")
for i in range(len(nums)):
diff = values[i] - nums[i]
sum_val = values[i] + nums[i]
print(f" {i} | {values[i]} | {nums[i]} | {diff} | {sum_val}")
ans1 = max(ans1, diff)
ans2 = max(ans2, sum_val)
print(f"\nMaximum (values[i] - nums[i]): {ans1}")
print(f"Maximum (values[i] + nums[i]): {ans2}")
print(f"Result: {ans1} + {ans2} = {ans1 + ans2}")
return ans1 + ans2
nums = [1, 2, 7]
values = [-4, 6, 5]
result = solve_with_trace(nums, values)
Index | values[i] | nums[i] | values[i]-nums[i] | values[i]+nums[i] ------|-----------|---------|------------------|------------------ 0 | -4 | 1 | -5 | -3 1 | 6 | 2 | 4 | 8 2 | 5 | 7 | -2 | 12 Maximum (values[i] - nums[i]): 4 Maximum (values[i] + nums[i]): 12 Result: 4 + 12 = 16
Conclusion
This algorithm efficiently finds the maximum value by transforming the expression into two independent parts. The time complexity is O(n) and space complexity is O(1), making it optimal for this problem.
