- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

# Program to find value for which given array expression is maximized in Python

Suppose we have two arrays called nums and values, both contains integers and the values of nums are strictly increasing and their lengths are also same. We have to find the 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] values = [-4, 6, 5], then the output will be 16, if we take pick i = 1 and j = 2 we get 6 + 5 + 7 - 2 = 16.

To solve this, we will follow these steps −

ans1 := -inf, ans2 := -inf

for i in range 0 to size of nums - 1, do

ans1 := 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))

## Input

[1, 2, 7], [-4, 6, 5]

## Output

16

- Related Questions & Answers
- C++ program to find permutation for which sum of adjacent elements sort is same as given array
- C++ program to find maximum possible value for which XORed sum is maximum
- Program to find smallest index for which array element is also same as index in Python
- Program to find maximum possible value of an expression using given set of numbers in Python
- Program to find expected value of given equation for random numbers in Python
- Find number from given list for which value of the function is closest to A in C++
- C++ Program to Construct an Expression Tree for a given Prefix Expression
- 8086 program to find the min value in a given array
- Program to find maximum value of k for which we can maintain safe distance in Python
- Python Program to Construct an Expression Tree of a given Expression
- Program to find maximum value at a given index in a bounded array in Python
- Check which element in a masked array is greater than a given value
- Check which element in a masked array is equal to a given value in NumPy
- Write a program in Python to find the index for NaN value in a given series
- Program to find array of length k from given array whose unfairness is minimum in python

Advertisements